SQLite

sqlite3-DB-API 2.0 interface for SQLite databases https://docs.python.org/2/library/sqlite3.html

A minimal SQLite shell for experiments

#Inserting data
import sqlite3 as lite
import sys

con = lite.connect('test.db')

with con:
    cur = con.cursor()    
    cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
    cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
    cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
    cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
    cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
    cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
    cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
    cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
    cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")

#Retrieving data
import sqlite3 as lite
import sys
con = lite.connect('test.db')
with con:    
    cur = con.cursor()    
    cur.execute("SELECT * FROM Cars")
    rows = cur.fetchall()
    for row in rows:
        print (row)

The fetchall() method gets all records. It returns a result set. Technically, it is a tuple of tuples. Each of the inner tuples represent a row in the table.

dbconn = sqlite3.connect('mem.db')
cur = dbconn.cursor()
cur.execute("SELECT TestTime as 'st [timestamp]' FROM TestTime")
rows = cur.fetchall()
print (rows[0])

Note that it's definitely "[timestamp]", [timestamp] is silently ignored, and .fetchone also won't convert the types.

results matching ""

    No results matching ""