Skip to main content

SmartNinja SQL - a simple SQLite wrapper.

Project description

SmartNinja SQL

About

SmartNinja SQL is a simple wrapper for SQLite database (support for others might be added in the future).

The purpose of this tool is to simplify connecting to SQLite and getting relevant data from it.

Important: This is not an object-relational mapper (ORM). You still need to write SQL queries.

Installation

This is a pip package:

pip install smartninja-sql

The package does not have any dependencies. It is compatible with Python 3 only.

Usage

Take a look at this basic usage example:

from smartninja_sql.sqlite import SQLiteDatabase
	
db = SQLiteDatabase()
	
db.query("""CREATE TABLE IF NOT EXISTS User (
            id integer PRIMARY KEY AUTOINCREMENT, 
            name text NOT NULL, 
            age integer);""")
	
db.print_tables(verbose=True)

If you open the connection with a database like this: db = SQLiteDatabase(), the database will be created in-memory only. When the program will finish, the database will be lost.

If you want a persistent database, enter a database name:

db = SQLiteDatabase(name="my_database.sqlite")

If this database does not yet exist, Python will create it for you. If it does exist, it will connect to it.

Print table data (name & fields)

This method prints the data about the tables in a database. By default the verbose parameter is False - in this case the method prints the names of the tables only:

# same result in both cases
db.print_tables()
db.print_tables(verbose=False)

But if you set verbose as True, the method will also print out all the field names in each table and their respective types:

db.print_tables(verbose=True)

Queries

Database queries are done using the .query() method:

# create table
db.query("""CREATE TABLE IF NOT EXISTS User (
            id integer PRIMARY KEY AUTOINCREMENT, 
            name text NOT NULL, 
            age integer);""")

db.print_tables(verbose=True)

# alter (edit) table
db.query("ALTER TABLE User ADD email TEXT;")
db.print_tables(verbose=True)  # the table has a new field: email

# insert data into a table
db.query("INSERT INTO User (name, age, email) VALUES ('Matt', 31, 'matt@example.org')")
db.query("INSERT INTO User (name, age, email) VALUES ('Nina', 25, 'nina@example.org')")

# get data (select) and print it
print(db.query("SELECT * FROM User;"))
print(db.query("SELECT * FROM User WHERE age=25;"))
print(db.query("SELECT * FROM User WHERE age=26;"))

If you're familiar with the sqlite3 library, the query() method returns the .fetchall() result.

Connection and cursor

The SmartNinja SQL wrapper is designed to simplify work with the sqlite3 library. But if you'd want to access the connection and cursor variables directly, you definitely can:

connection = db.conn
cursor = db.cursor

But you most likely won't have to.

Closing the cursor and connection

You can simply close the cursor and connection with a single method:

db.close()

TODO

  • tests
  • CI pipeline

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smartninja-sql-0.1.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file smartninja-sql-0.1.tar.gz.

File metadata

  • Download URL: smartninja-sql-0.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.1

File hashes

Hashes for smartninja-sql-0.1.tar.gz
Algorithm Hash digest
SHA256 aed4ad0186f92e1480093eb6d8cc3c2a64d7927782d73dca14a331aadc50a2d2
MD5 a04d93cc552c27fc503eb139bceae80c
BLAKE2b-256 3f0cf83f5f7e1fc3edac0c40a0a283efbf8d5f8b1cc851f1904c0a35e182f2e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page