python-mysqldb - Python insert MySQL _mysql_exceptions.ProgrammingError: (1064,
PHP中文网
PHP中文网 2017-04-17 15:49:21
0
2
892
数据库结构
Field Type Null Key Default Extra
+---------------+-------------+------+-----+---------+-------+
Device_Name varchar(24) YES NULL
IP_Address varchar(24) YES NULL
DRAC varchar(24) YES NULL
Support_Group varchar(24) YES NULL
Domain varchar(24) YES NULL
OU varchar(24) YES NULL
IEM varchar(24) YES NULL
OS varchar(24) YES NULL
OS_Version varchar(24) YES NULL
Environment varchar(24) YES NULL
Risk varchar(24) YES NULL
Function varchar(24) YES NULL
App varchar(24) YES NULL
Owner varchar(24) YES NULL
Owner_mail varchar(24) YES NULL
Country varchar(24) YES NULL
City varchar(24) YES NULL
Site varchar(24) YES NULL
Location varchar(24) YES NULL
Category varchar(24) YES NULL
Manufacturer varchar(24) YES NULL
Model varchar(24) YES NULL
Rack varchar(24) YES NULL
Power varchar(24) YES NULL
Serial varchar(24) YES NULL
Warranty varchar(24) YES NULL
Decomm_Comm varchar(24) YES NULL
Project varchar(24) YES NULL
Manager varchar(24) YES NULL
Comm_data varchar(24) YES NULL

代码

!/bin/python

import os
import xlrd
import MySQLdb
import re

DB_HOST='127.0.0.1'
DB_USER='root'
DB_PASS='Jun06jun'
DB_NAME='list'
db=MySQLdb.connect(host=DBHOST,user=DBUSER,passwd=DBPASS,db=DBNAME)
cursor=db.cursor()

a=[]
os.chdir('/home/ran/Desktop')
data=xlrd.open_workbook('list.xlsx')
table=data.sheets()[0]
nrows=table.nrows

for i in range(nrows):

    c = table.row_values(i)
    print len(c)
    for i_row in range(len(c)):
            if c[i_row] == '': c[i_row] = "NULL"
            sql="insert into JLL values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) % (c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9],c[10],c[11],c[12],c[13],c[14],c[15],c[16],c[17],c[18],c[19],c[20],c[21],c[22],c[23],c[24],c[25],c[26],c[27],c[28],c[29])"
            cursor.execute(sql)

Excel 中某一列数据
AUCFD317-Joey
x.x.x.x
VMWare
Joey
Linux
N/A
N/A
Linux
Linux (MLOS)- RHEL 5 64bit Patch 3
Production
NULL
ePO Server
McAfee GTI Proxy Appliance 2.0.0
x
x
x
x
x
Server Room
Server-Virtual
VMware, Inc.
VMware Virtual Platform
NULL
NULL
Vmware
N/A - VM
NULL
NULL
NULL
NULL

发生的错误
mysqlexceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' at line 1")

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
黄舟

Error:

sql="insert into JLL values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) % (c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9],c[10],c[11],c[12],c[13],c[14],c[15],c[16],c[17],c[18],c[19],c[20],c[21],c[22],c[23],c[24],c[25],c[26],c[27],c[28],c[29])"

Correct:

sql="insert into JLL values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9],c[10],c[11],c[12],c[13],c[14],c[15],c[16],c[17],c[18],c[19],c[20],c[21],c[22],c[23],c[24],c[25],c[26],c[27],c[28],c[29])

Refer to @displaynone’s simplified version, Python has so many syntactic sugars

sql="insert into JIL values(%s)" % (','.join('"' + str(item) + '"' for item in c))
大家讲道理

Because the string is not assigned correctly, I will post a simplified version by the way.

c = [1 + i for i in range(29)]
sql = "insert into JLL value s(" + ','.join('%s' for i in range(29)) + ")"
finall = sql % tuple(c)

Simpler version (why bother with %s haha):

sql = "insert into JLL value s"+str(tuple(c))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template