I have a python script that should insert a list into a sqlite table. It appears my insert statement is not working.
links = ['a', 'b', 'c']
conn = sqlite3.connect('example.db')
#create a data structure
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry():
sites = links
c.execute("INSERT INTO server(sites) VALUES(?)", (sites))
conn.commit()
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close
I checked the database at command line but the "server" table is empty:
C:\App\sqlite\sqlite_databases>sqlite3
SQLite version 3.17.0 2017-02-13 16:02:40
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .tables
server
sqlite> SELECT * FROM server
...> ;
sqlite>
So it doesn't appear that the list is actually being inserted.
data_entry()and you are missing()on the last line (sholud beconn.close()); also, rename variablelistto something likelist_, but don't use reserved words