import sqlite3 code example

Example 1: sqlite python connection

import sqlite3

try:
    sqliteConnection = sqlite3.connect('SQLite_Python.db')
    cursor = sqliteConnection.cursor()
    print("Database created and Successfully Connected to SQLite")

    sqlite_select_Query = "select sqlite_version();"
    cursor.execute(sqlite_select_Query)
    record = cursor.fetchall()
    print("SQLite Database Version is: ", record)
    cursor.close()

except sqlite3.Error as error:
    print("Error while connecting to sqlite", error)
finally:
    if (sqliteConnection):
        sqliteConnection.close()
        print("The SQLite connection is closed")

Example 2: sqlite execute from file

sqlite> .mode column							-- results as columns (optional)
sqlite> .header on								-- columns name are displayed (optional)
sqlite> .read c:/sqlite/commands.sql			-- executes sql code in commands.sql (text file)
sqlite3 auction.db < commands.sql				-- Shell version
sqlite3 my_db.db 'SELECT * FROM some_table;'	-- Shell direct query in my_db.db

Tags:

Css Example