How to query SQLite database using Python

SQLlite uing Python
Accessing SQLite using Python

Simplest way to query SQLite database from Python

This example demonstrates how to access SQLite using Python 3.6 on Linux machine. But its the same for other versions of python, irrespective of the Operating system.

Based on your pip version choose one of the following

$ pip install pysqlite 
$ pip3 install pysqlite
$ pip3.6 install pysqlite 
import sqlite3
conn = sqlite3.connect('./project/your.db')
try:
print("Database opened")
result = conn.execute("select column1, column2 from tablename;")
for val in result:
print(val[0])
print(val[1])
except Exception as e:
print("Error opening db")
finally:
conn.close()
Also : How to import csv into SQLite database.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: