Answer the question
In order to leave comments, you need to log in
How to properly write data to two SQLite DB tables?
Good afternoon!
Please tell me how to make requests in this case. There are two lists:
a = [('1', '2', '3')]
b = [('a', 'b', 'c'), ('x', 'y', 'z' )]
There are two tables in the database:
table_a:
id | value1 | value2 | value 3 - here id is the primary key with autoincrement
table_b:
id | value1 | value2 | value 3
Accordingly, I make requests:
query_a = "INSERT INTO table_a (value1, value2, value 3) VALUES (?, ?, ?)"
query_b = "INSERT INTO table_b (value1, value2, value 3) VALUES (?, ?, ?)"
cur.executemany(query_a, a)
cur.executemany(query_b, b)
Answer the question
In order to leave comments, you need to log in
If I understand the problem correctly,
query_a = "INSERT INTO table_a (value1, value2, value 3) VALUES (?, ?, ?)"
cur.execute(query_a, a[0])
last_id = cur.lastrowid
query_a = "INSERT INTO table_a (value1, value2, value 3) VALUES (?, ?, ?)"
cur.executemany(query_a, a)
cur.execute('SELECT last_insert_rowid()')
last_id = cur.fetchone()[0]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question