A
A
Alexander Bondarenko2020-02-04 14:33:54
Python
Alexander Bondarenko, 2020-02-04 14:33:54

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()

Lines from the file:
Test|id|first_name|second_name|address|house
lorem|ipsum|dolor|sit|amet
consectetur|adipscing|elit|Phasellus|ultrices
s|d||c|d

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Lyskov, 2020-02-04
@bond_1013

insert into table(column1, column2) values (?,?)'
id skip

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question