This is convenient when you want to execute SQL many times and try it because you do not need to delete the table.
import sqlite3
#conn = sqlite3.connect('test_sqlite.db')
conn = sqlite3.connect(':memory')
curs = conn.cursor()
curs.execute('CREATE TABLE persons(id INTEGER PRIMARY KEY AUTOINCREMENT,name STRING)')
curs.execute('INSERT INTO persons(name) values("Mike")')
curs.execute('INSERT INTO persons(name) values("Nancy")')
curs.execute('INSERT INTO persons(name) values("Jun")')
conn.commit()
curs.execute('SELECT * FROM persons')
print(curs.fetchall())
curs.close()
conn.close()
Execution result:
[(1, 'Mike'), (2, 'Nancy'), (3, 'Jun')]
Recommended Posts