-2

i have create a mysql table like----> create table test(gender char(1));

my python sudo code is---->

from pymysql import *
g='m'
sql='insert into test values(%s)' %g
cur.execute(sql)
con.commit()
con.close()

but its giving me error ---> (1054, "Unknown column 'm' in 'field list'")

plz help me out to solve it

Varun
  • 3
  • 1
  • 3

2 Answers2

3

The

'insert into test values(%s)' %g

expands to

'insert into test values(m)'

which is clearly not what you want (what is m?)

My recommendation is to use bind parameters:

g = 'm'
sql = 'insert into test values(?)'
cur.execute(sql, g)

For more information, see How to use variables in SQL statement in Python?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

You should try this:

'insert into test () values('%s')' %g ... this is because the variable g is aString` and after the append that you are doing this must look like:

"insert into test () values('m')" and not "insert into test () values(m)"

<>

cheers

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97