python - Insert dictionary keys values into a MySQL database -


i have executed code insert dictionary table in database,

d = {'err': '0', 'tst': '0', 'type o': 'ftp', 'recip': 'admin', 'id': '101', 'origin': 'report', 'type recip': 'smtp', 'date': '2010-01-10 18:47:52'}  db = mysqldb.connect("localhost","admin","password","database") cursor = db.cursor() cursor.execute("""insert mytable(id, err, tst, date, origin, type_o, recip, type_recip) values (%(id)s, %(err)s, %(tst)s, %(date)s, %(origin)s, %(type o)s, %(recip)s, %(type recip)s)""", d)  db.commit() db.close() 

create statement of table:

create table mytable (       `id` tinyint unsigned not null,       `err` tinyint not null,       `tst` tinyint unsigned  not null,       `date` datetime not null,      `origin` varchar(30) not null,      `type_o` varchar(10) not null,      `recip` varchar(30) not null,      `type_recip` varchar(10) not null,      primary key (`id`,`date`) ) engine = innodb;  

but have error, says:

1064, "you have error in sql syntax; check manual corresponds mysql server version... )

be aware of sql injections , use second argument execute inserting query parameters:

cursor.execute("""     insert         table         (name, age, origin, date)     values         (%(name)s, %(age)s, %(origin)s, %(date)s) """, d) 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -