I am trying to insert multiple records to sqlite database. This is my test script.
from datetime import datetime
company = 'fghjk'
keyword = 'awesome'
filename = 'test.txt'
date_found = datetime.now()
conn = sqlite3.connect('C:\\sqlite\\test.db')
c = conn.cursor()
for i in range(0,4):
insert_query = "INSERT into records (company,keyword,filename,date_found) values (?,?,?,?)"
select_query = "SELECT * FROM records"
res = c.fetchall()
for i in res:
for b in i:
conn.execute('''CREATE TABLE records
# (ID INTEGER PRIMARY KEY AUTOINCREMENT,
# company TEXT NOT NULL,
# keyword INT NOT NULL,
# filename CHAR(50),
# date_found DATETIME);''')
The problem looks to be that you're not committing the data to the database.
Each time you open the database, you put in 4 rows, then print them, then let the cursor and connection be destroyed without committing your changes.
conn.commit() after all of your inserts are done will fix this. IIRC, the table creation is automatically committed immediately after it runs, which is why it is saved in the database.