Tag Archives: python

Connect to Sqlite3 using python

The below snippet allows you to create an sqlite3 database and then import data to it.

import sqlite3

#this connects and creates the database.db file, in the same location where the script exists

conn = sqlite3.connect(database.db)
c = conn.cursor()

#execute the create table
c.execute('''CREATE TABLE IF NOT EXISTS mytable
(filename text,
language text, 
year text,
summary text,
rating real)''')

#this is the dataset i will save to database as a python list:

data=['firstfile.mov','en','2018','This is a short summary of the video','3.8']

#get the data in the database
c.execute('INSERT INTO mytable VALUES (?,?,?,?,?)', data)

#close the connection
conn.commit()
conn.close()