Python Forum

Full Version: sqlite3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to insert my data into my table using variables. For example i want to insert data into table using ( data = input ("") )
What should i do?

import sqlite3 as sq3

conn = sq3.connect("Hacker.db")
#conn.execute('''CREATE TABLE Hack
# (ID INT PRIMARY KEY NOT NULL,
# NAME TEXT NOT NULL,
# AGE INT NOT NULL);''')
conn.execute("INSERT INTO Hack (ID,NAME,AGE) VALUES (1, 'PAUL', 17) ");
conn.execute("INSERT INTO Hack (ID,NAME,AGE) VALUES (2, 'PAUL', 17) ")
cursor = conn.execute("SELECT ID,NAME,AGE from Hack")
for row in cursor:
    print ("ID = " + str(row[0]) )
    print ("NAME = " + str(row[1]))
    print ("AGE = " + str(row[2]))
conn.close()
you need to commit after the insert in order to preserve the changes

https://docs.python.org/3/library/sqlite3.html
Also you need to fetch records after select is executed
import sqlite3 as sq3
conn = sq3.connect("Hacker.db")
conn.execute('''CREATE TABLE Hack
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL);''')
conn.execute("INSERT INTO Hack (ID,NAME,AGE) VALUES (1, 'PAUL', 17) ");
conn.execute("INSERT INTO Hack (ID,NAME,AGE) VALUES (2, 'PAUL', 17) ")
conn.commit();
cursor = conn.execute("SELECT ID,NAME,AGE from Hack")
for row in cursor:
    print ("ID = " + str(row[0]) )
    print ("NAME = " + str(row[1]))
    print ("AGE = " + str(row[2]))
conn.close()
So? Do you have any questions? I see you still don't use cursor.fetchall()