Unable to insert values into a table using MySQL in Python -


i want update table values created in python script in mysql database. here line problem:

dbcursor.execute("insert solution (problemname, tourlength, date, author, algorithm, runningtime, tour) values ('%s', '%d', '%s', '%s', '%s', '%d', '%s')" % (sys.argv[1], bestdist, "foo bar", "curdate()", "greedy + 2-opt", timeallowed, bestroute)) dbconnection.commit() 

it generating error:

pymysql.err.internalerror: (1136, "column count doesn't match value count @ row 1") 

i've tried googling problem , searching website solution, none of ones i've found match mine. fixes have not solved problem because of this. not sure can @ point. can see, of column names match values , formatted variables.

what causing problem?

create table if not exists solution (   solutionid int(11) not null auto_increment,   problemname varchar(32) not null,   tourlength double not null,   date date default null,   author varchar(32) default null,   algorithm varchar(32) default null,   runningtime int(11) default null,   tour mediumtext not null,   constraint spk primary key (solutionid),   constraint solpname foreign key (problemname) references problem (name) on delete cascade ); 

the problem in formatting sql query % operator. in particular, need escape parameters , curdate() shouldn't quoted.

that said, it's better rely on db driver interpolate query parameters. that, pass them tuple cursor.execute() this:

sql = (     "insert solution (problemname, tourlength, date, author, algorithm, "     " runningtime, tour) values (%s, %s, curdate(), %s, %s, %s, %s)") print(sql) dbcursor.execute(sql, (sys.argv[1], bestdist, "foo bar", "greedy + 2-opt",                        timeallowed, bestroute)) 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -