Answer the question
In order to leave comments, you need to log in
How to insert data into sqlite3 db?
How can I insert data into a table with autoincrement, how can I skip this field? When inserting, an error occurs that the data type does not converge because the first field is id and it is int.
import sqlite3
conn = sqlite3.connect("all.db")
cursor = conn.cursor()
def parser(link, separator='|'):
f = open(link)
text = f.read()
text = text.split('\n')
for i in range(len(text)):
text[i] = tuple(text[i].split(separator))
text = tuple(text)
f.close()
print(text)
return text
def create_table(name):
cursor.execute("""CREATE TABLE {0} ({1} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,{2} text,{3} text,{4} text,{5} text)""".format(name[0],name[1],name[2],name[3],name[4],name[5]))
conn.commit()
text=parser('C:\\Users\\sanya\\Documents\\БД\\test.txt')
try:
create_table(text[0])
except:
pass
print(text)
text=text[1:]
text=list(text)
print(text)
cursor.executemany("INSERT INTO Test VALUES (?,?,?,?,?)",text)
cursor.commit()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question