MINSIK
MINSIK

Reputation: 39

can't insert records in sqlite database by python3

import sqlite3

conn = sqlite3.connect('animelist.sqlite')
cur = conn.cursor()

cur.execute('''DROP TABLE IF EXISTS Lists''')
cur.execute('''CREATE TABLE Lists (Title TEXT, Genre TEXT, Production TEXT, 
Year TEXT)''')

while True:
    a_title = input('Insert title: ')
    a_genre = input('Insert genre: ')
    a_production = input('Insert production: ')
    a_year = input('Insert year: ')

    cur.execute('''INSERT INTO Lists (Title, Genre, Production, Year) VALUES 
    (a_title, a_genre, a_production, a_year)''')

    print('Data is inserted')
    dbexit = input('EXIT (y/n): ')
    if dbexit == 'y':
        break
    elif dbexit == 'n':
        continue

I want to make my animation lists by using python, sqlite

but when I try this script,

it says 'sqlite3.OperationalError: no such column: a_title'

what am I missing?

Upvotes: 0

Views: 356

Answers (1)

Bimal Poudel
Bimal Poudel

Reputation: 1234

You should use parameterize queries.

import sqlite3

conn = sqlite3.connect('animelist.sqlite')
cur = conn.cursor()

cur.execute('''DROP TABLE IF EXISTS Lists''')
cur.execute('''CREATE TABLE Lists (Title TEXT, Genre TEXT, Production TEXT, Year TEXT)''')

a_title = 'Title'
a_genre = 'Genre'
a_production = 'Production'
a_year = 2018

cur.execute('''INSERT INTO Lists (Title, Genre, Production, Year) VALUES(?,?,?,?)''', (a_title, a_genre, a_production, a_year))
conn.commit()

Here, I am not taking user input. But it inserts the data to the table.

Upvotes: 3

Related Questions