· 6 years ago · Jun 30, 2019, 08:40 AM
1list1=['a','b','c']
2list2=['1','2','3']
3
4a | b | c | d | e
5
6strList1=''
7for thing in list1:
8 strList1+=thing+','
9strList1=strList1[:-1]
10
11insert="""insert into tbl_name(%s) values(%s)"""
12cur.execute(insert,(strList1,strList2))
13
14curs.execute("SELECT foo FROM bar WHERE foobar = ?",(some_value,))
15
16import sqlite3
17
18list1=['foo','bar','foobar'] #List of tables
19list2=['First_value','second_value','Third_value'] #List of values
20
21db_conn = sqlite3.connect("test.db") #I used sqlite to test it quickly
22db_curs = db_conn.cursor()
23
24for table in list1: #Create all the tables in the db
25 query = "CREATE TABLE IF NOT EXISTS %s(foo text, bar text,foobar text)" % table
26 db_curs.execute(query)
27db_conn.commit()
28
29for table in list1: #Insert all the values into all the tables
30 query = "INSERT INTO %s VALUES (?,?,?)" % table
31 db_curs.execute(query,tuple(list2))
32db_conn.commit()
33
34for table in list1: #Print all the values out to see if it worked
35 db_curs.execute("SELECT * FROM %s" % table)
36 fetchall = db_curs.fetchall()
37 for entry in fetchall:
38 print entry[0], entry[1],entry[2]
39
40list1=['a','b','c']
41 print ",".join(list1)
42 #a,b,c
43
44insert="""insert into tbl_name(%s) values(%s)"""
45
46insert="""insert into tbl_name(a,b,c) values(%s,%s,%s)"""
47
48insert="""insert into tbl_name (%s,%s,%s) values (%s, %s, %s);"""
49strList1=('a','b','c')
50strList2=(1,2,3)
51curs.execute(insert % (strList1 + strList2))