I’m trying to insert dict values into database. But i’m getting error .here is my dictionary. I’m using this code .
`cols = filter_dict.keys()
vals = filter_dict.values()
("INSERT INTO table (%s) VALUES (%s)", [cols, vals])
`
When I only print this query ("INSERT INTO table (%s) VALUES (%s)", [cols, vals]).
I’m getting following output.
('INSERT INTO table (%s) VALUES (%s)', [['cinematography', 'name', 'producer', 'caption', 'studio', 'editing'], ['Venkat Prasad', '100% Love', 'Bunny Vasu', '', '100% Love Poster.jpg', 'Chandra Sekhar T Ramesh,Hari Prasad']])
But When I execute this query I”m getting strange output.
the manual that corresponds to your MySQL server version for the right syntax to use near \'table (("\'cinematography\'", "\'name\
Why execute query adding `\’ to each columns?? Can any one help me? thanks
The backslashes are likely due to your debugger. Are you running your code in the interactive prompt?
Anyway, the actual problems are:
tableis a reserved word. You should puttablein backticks.%sand multiple values.For example:
You will also need to change
(cols, vals)to list the individual values.I’d also strongly suggest that you try to find a better name for your table, preferably one that:
1) describes what sort of data the table contains and
2) isn’t a reserved word