no_freedom
no_freedom

Reputation: 1961

Insert data from a Python dictionary to MySQL

I want to insert dictionary data into mysql each key in its own field. I tried following code. But its wrong. Can any one help? I really appreciate. thanks so much :-)

d = {"spam": "1", etc,etc,"egg": "2"}
cols = d.keys()
vals = d.values()

stmt = "INSERT INTO table (%s) VALUES(%s)" % (
",".join(cols), ",".join(len(vals))

Upvotes: 1

Views: 1028

Answers (1)

sharvey
sharvey

Reputation: 8145

Why do you call len(vals) ? Your code should work by simply removing that.

Upvotes: 2

Related Questions