Try   HackMD

Python SQLite API

tags: Python Notes

Importing SQLite in Python

import sqlite3 from sqlite3 import Error

Getting connection

# after importing conn = sqlite3.connect(db_name) # db_name is the name of database

Executing a SQLite statement

# after connecting c = conn.cursor() # this is essential before executing any statement c.execute(stmt) # stmt is a full SQLite statement

Executing a SQLite prepared Statement

# after connecting c.execute(pstmt, values) # pstmt is a SQLite prepared statement # value is a tupple

Comminting changes

When to commit: edit, delete, alter table
When not needed: query, creating table

# after executing conn.commit()

Closing connecion

Always close connection after using. Do not try to use after colsing.

# use database here # after using conn.close() # do not use database here

Error handing

try: # connetion, cursor, execution except Error as e: print(e) finally: #close connection
Made with :hearts: by Kavyas
Learn SQLite on https://www.sqlitetutorial.net/