I have 8 kinds of data that I would like to insert into a mysql table through mysql-connector using python.
I have looked at some documents saying that it is better to use
Here i have a solution for you.
let's consider only three columns as example. URL, TITLE and CONTENT. As you have mentioned URL and other are lists of data.
URL=['url1','url2','url3']
TITLE=['title1','title2','title3']
and so on. And you have the same columns in DATABASE table.
cursor_object.execute(query,params)
lets write a query here:
query='''
INSERT INTO scripting(URL, TITLE, CONTENT)
VALUES (%s,%s,%s)'''
and executing the sql command:
cursor_object.execute(query%(URL[i],TITLE[i],CONTENT[i]))
when it is translated following will be the result in first iteration.
'INSERT INTO scripting(url, title)VALUES (url1,title1)' url1 is not the string here when actually translated into mysql query. So you have to change little.
query='''
INSERT INTO scripting(URL, TITLE, CONTENT)
VALUES ("%s","%s","%s")'''
OR
Alternatively you can use dictionary method.
query=''' *query statement* values
(%(url)s,%(title)s,%(content)s)
dictionary={'url':'url1','title':'title1','content':'content1'}
cursor_object.execute(query,dictionary)
for multiple rows:
for i in range len(URL):
cursor_object.execute(query,{'url':URL[i],'title':TITLE[i],'content':CONTENT[i]}
simplest solution was given by @Antti Haapala. Hope you enjoy.