ThanaDaray
ThanaDaray

Reputation: 1693

Error when update table by using MySQLdb

import MySQLdb
import csv
import sys
db = MySQLdb.connect("host","username","password","dbname" )
c = db.cursor()
posfile = 'C:/Users/name/Desktop/textfile.txt'
csv_data_pos = csv.reader(open(posfile, 'rb'))
count_pos = 0
for row_pos in csv_data_pos:
    count_pos = count_pos + 1
    pos_file_update = "UPDATE Sentence SET POS_score = %s WHERE Id = %s"
    c.execute(pos_file_update, (row_pos, count_pos))

I am trying to update row_pos into table but I got error

"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 ') WHERE Id = 1' at line 1"")"          

Did I miss something in the codes? Any advice?

Upvotes: 1

Views: 60

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270785

In this context, row_pos is an array rather than a scalar value. If you want to pass in a particular value from that array, use an index like [0] to retrieve it.

# row_pos is an array representing one row of the 2d array csv_data_pos
# as returned by the csv.reader() call
for row_pos in csv_data_pos:
    count_pos = count_pos + 1
    pos_file_update = "UPDATE Sentence SET POS_score = %s WHERE Id = %s"
    # If there is a specific value from the row_pos array, use its array index
    c.execute(pos_file_update, (row_pos[0], count_pos))

Upvotes: 2

Related Questions