1

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))

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks I want to insert single quote around values. How can I add ?
Probably not the prefered way, but you can do: VALUES(\"%s\") ... % '", "'.join(vals);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.