-
Notifications
You must be signed in to change notification settings - Fork 0
SQLite
Salman Shah edited this page Mar 20, 2018
·
1 revision
- The given set of commands can get the user to interact with the SQLite Database using the Python Command Line Interface.
There is no need to install sqlite3 module. It is included in the standard library.
- These commands are necessary to be executed before running any SQLite Query.
import sqlite3
db = sqlite3.connect("file::memory:?cache=shared", check_same_thread=False)
cur = db.cursor()- These commands are to be executed if you want to know the columns in a particular table.
cursor = cur.execute('SELECT * FROM rides;')
names = list(map(lambda x: x[0], cursor.description))
print(names)This will list the columns that are there in the table rides
- These commands are to be executed if you want to fetch all the rows in a particular table.
cursor = cur.execute('SELECT * FROM rides;')
rides = cursor.fetchall()
print(rides)Alternatively you can also use cursor.fetchone() in case you need to fetch only one record.
- These set of commands are to be executed when a new row has to be added to a particular table.
cycle = cur.execute("INSERT INTO cycles VALUES(1, 'Hrishi') ;")
db.commit()
- These set of commands are run to delete a particular row in the table.
mydata = cur.execute("DELETE FROM rides WHERE id=6")
db.commit()