i have python script should insert list sqlite table. appears insert statement not working.
links = ['a', 'b', 'c'] conn = sqlite3.connect('example.db') #create data structure c = conn.cursor() #create table c.execute('''create table if not exists server("sites")''') #insert links table def data_entry(): sites = links c.execute("insert server(sites) values(?)", (sites)) conn.commit() #query database c.execute("select * server") rows = c.fetchall() row in rows: print(row) conn.close
i checked database @ command line "server" table empty:
c:\app\sqlite\sqlite_databases>sqlite3 sqlite version 3.17.0 2017-02-13 16:02:40 enter ".help" usage hints. connected transient in-memory database. use ".open filename" reopen on persistent database. sqlite> .tables server sqlite> select * server ...> ; sqlite>
so doesn't appear list being inserted.
iterate on list_
, execute insert
each item. , call data_entry()
insert data.
import sqlite3 list_ = ['a', 'b', 'c'] #create data structure conn = sqlite3.connect('example.db') c = conn.cursor() #create table c.execute('''create table if not exists server("sites")''') #insert links table def data_entry(): item in list_: c.execute("insert server(sites) values(?)", (item)) conn.commit() data_entry() # ==> call function #query database c.execute("select * server") rows = c.fetchall() row in rows: print(row) conn.close()
Comments
Post a Comment