MySQL, python update row if null -
i have database contains 7 fields, field 1 unique , rest not. field 2 , 3 null. trying update database information csv file fill in nulls cannot work. field null or field contain information.
db=mdb.connect('localhost','username','password','db') cur=db.cursor() inputinfo=csv.reader(open('insert.csv','r'),delimiter=',') row in inputinfo: inserthostnames=("""update filesort set hostnames=values(hostnames) hostnames not null""") cur.execute(inserthostnames,row[1]) insertipaddress=("""update filesort set ipaddress=values(ipaddress) ipaddress not null""") it not work , gives me error
traceback (most recent call last): file "test.py", line 57, in <module> inserttaddm() file "test.py", line 47, in inserttaddm cur.execute(inserthostnames,row[1]) file "/usr/lib/python2.7/dist-packages/mysqldb/cursors.py", line 159, in execute query = query % db.literal(args) typeerror: not arguments converted during string formatting edit1:
for row in inputinfo: inserthostnames=("""update filesort set hostnames=hostnames hostnames null""",row) cur.execute(inserthostnames,row[1]) insertipaddress=("""update filesort set ipaddress=ipaddress ipaddress null""",row) cur.execute(insertipaddress,row[2]) this gives error:
typeerror: unsupported operand type(s) %: 'tuple' , 'str' edit 2:
for row in inputinfo: inserthostnames=("""update filesort set hostnames='%s' hostnames null""") cur.execute(inserthostnames,(db.escape_string(row[1]))) #insertipaddress=("""update filesort set ipaddress='%s' ipaddress null""") #cur.execute(insertipaddress,row[2]) db.commit() gives me:
_mysql_exceptions.programmingerror: (1064, "you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'server2'' hostnames null' @ line 1") the apostrophe returned
your execute function contains parameter, row[1] there no place substitution in query string. also, parameters must passed sequence. additionally, if want fill in nulls, where condition should where hostnames null.
further more, since hostnames might contain special characters, might want escape using conn.escape_string
you might want change query to:
inserthostnames=("""update filesort set hostnames='%s' hostnames null""") cur.execute(inserthostnames,(db.escape_string(row[1])))
Comments
Post a Comment