I'm using a text file with a list of int's that I want to read one line at a time and insert into a mysql query (line). It almost works but just reads the same first line over and over.
sample from the input text file:
513121
513017K
513137
515036
513124
510013
error output:
513121
513121, 0
513121
513121, 0
513121
513121, 0
Traceback (most recent call last):
File "Part-Search/inventory.py", line 220, in report
cursor.execute ("SELECT"+" "+line+" "+"description, quantity FROM phppos_items")
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column '513017K' in 'field list'")
my def:
Code:
def report(self):
inv_file = open("Part-Search/inventory.txt", "r")
db= MySQLdb.connect(user="root" , passwd="mypass", db="pos")
while 1:
lines = inv_file.readlines(100)
if not lines:
break
for line in lines:
#print line
cursor = db.cursor ()
#row = cursor.fetchone ()
cursor.execute ("SELECT"+" "+line+" "+"description, quantity FROM phppos_items")
rows = cursor.fetchall ()
for row in rows:
print line+"%s, %s" % (row[0], row[1])
continue
print "Number of rows returned: %d" % cursor.rowcount
cursor.close ()
db.close()
Any help would be appreciated!