Python Forum
sqlite3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: sqlite3 (/thread-4967.html)



sqlite3 - kingram - Sep-13-2017

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



RE: sqlite3 - buran - Sep-13-2017

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


RE: sqlite3 - rajeev1729 - Sep-14-2017

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



RE: sqlite3 - buran - Sep-14-2017

So? Do you have any questions? I see you still don't use cursor.fetchall()