Python-SiDB: A Less Query SQLite Database Library

SiDB – A Simple Database Library for sqlite3, after writing similar thing for PHP MySQL, since I use Python too I decided to build for it too for my easier coding.

After building and seeing how helpful it is, I decided to share.

What Does SiDB do?

  • In all, its for less SQL Queries, it has method to do most things you need to do with SQL
  • Inserts, updates and deletes data to database with a line of code
  • Easy creating, deleting and renaming of table

Usage

Import SiDB

from SiDB import SiDB</p>
<p>db = SiDB( 'database.db' )

Inserting to Database

db.insert( 'table_name', { 'col1': 'value1', 'col2' : 24, 'col3' : 'value2' } )

This will insert to the table `table_name` with those values for each column

Updating Table

As we can insert, we easily can update too. Below is an example of updating a table

db.update( 'table_name', { 'col1' : 'new_value1', 'col2' : 'new_value3' }, { 'col3' : 'value2' } )

This will update the columns col1, col2 to new values where col3 is equal to value2. When the third parameter is not provided, it updates the whole row of the table

Deleting Table

Same as update, but has to delete row(s)

db.delete( 'table_name', { 'col1' : 'new_value1' } )

That deletes a row that matches where col1 = new_value1. When the second parameter is not provided, it drops the whole rows in a table

Check If Table Exists

print( db.table_exists( 'table_name' ) )

Drop Table

db.drop_table( 'table_name' )

Rename Table

db.rename_table( 'table_name', 'new_name' )

Creating Table

This is a bit the complex part but gonna make it easy

db.create_table( 'table_name', {<br />
	'ID' : {<br />
		'type' : 'INT', 'allow_null' : False, 'primary_key' : True<br />
		},<br />
	'name' : {<br />
		'type' : 'TEXT'<br />
		},<br />
	'phone' : {<br />
		'unique' : True, 'type' : 'INT'<br />
	},<br />
	'date' : {<br />
		'type' : 'DATETIME',<br />
		'default' : 'CURRENT_TIMESTAMP'<br />
	}<br />
})

Above we are creating a table with the name “table_name” and columns ID, name, phone and date. These keys must have a dict value and these are list of available keys

        * type - This specifies the data type of a column
        * allow_null - Boolean, allow null data on column,
        * primary_key - Boolean 
        * unique - boolean
        * default - Default value

And their values can be valid SQL Syntax.

As it extends the sqlite3.Connection class, other sqlite3 methods are still available if you wish to use them.

Get on GitHub

This is all for now, hope it helps…