mysql - Execute query in python -
i have simple table
****************** mid | title 1 | pqr 2 | abc ******************
now code have written append data table is
import mysqldb db = mysqldb.connect("localhost", "root", "a", "project") cursor = db.cursor() sql = "insert project.movie(mid, title) values(%d, %s)" % (21,'aman') cursor.execute(sql)
but above code generates error
traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib/python2.7/dist-packages/mysqldb/cursors.py", line 159, in execute query = query % db.literal(args) typeerror: %d format: number required, not str
i have passed number , not in quotes why there error ?
your real issue here quite not putting quotes around inserted string. izkata remarked, shouldn't doing python-side wise due sql injection. sql-side, entire query must in string format when being executed, must use %s
every field.
your string (instead of %d
) converted sql literal value in case upon executing query.
in case, should writing insertion statement (as seen in docs):
sql = ( "insert project.movie (mid, title) " "values (%s, %s)" ) data = (21,'aman') cursor.execute(sql, data)