MySQL problem

wes weston wweston at att.net
Thu Mar 17 11:45:57 EST 2005


Lad wrote:
> I have the following
> program( only insert a record)
> ################
> import MySQLdb
> conn = MySQLdb.connect (host = "localhost",user = "", passwd =
> "",db="dilynamobily")
> cursor = conn.cursor ()
> cursor.execute("""CREATE TABLE produkt1 (
>   id int(10) unsigned NOT NULL auto_increment,
>   MyNumber varchar(30) NOT NULL default '',
>   PRIMARY KEY  (id))
>   """)
> 
> #MyValue=111
> cursor.execute ("""INSERT INTO produkt1
> (MyNumber)
> 	VALUES(111)
> 	""")
> #################
> It works. But If I change the program like the following ( only use a
> variable MyValue in INSERT statement it does not work.
> ##########THIS DOES NOT WORK########
> import MySQLdb,re,string
> conn = MySQLdb.connect (host = "localhost",user = "", passwd =
> "",db="dilynamobily")
> cursor = conn.cursor ()
> cursor.execute("""CREATE TABLE produkt1 (
>   id int(10) unsigned NOT NULL auto_increment,
>   MyNumber varchar(30) NOT NULL default '',
>   PRIMARY KEY  (id))
>   """)
> 
> MyValue=111
> cursor.execute ("""INSERT INTO produkt1
> (MyNumber)
> 	VALUES(MyValue)
> 	""")
> #################
> Program says
> OperationalError: (1054, "Unknown column 'MyValue' in 'field list'")
> Where is a problem. Thanks for help
> Lad.
> 

Lad,
   Try

str = "INSERT INTO produkt1 (MyNumber) VALUES(%d)" % (MyNumber)
cursor.execute(str)

wes




More information about the Python-list mailing list