Giter VIP home page Giter VIP logo

shaknas_database_py's Introduction

shakna's database.py

A modern, sensible and backwards-compatible database API for Python.


A Taste

import sqlite3
import database

def connect():
	d = database.Database(sqlite3, "data.db")

	# Forum data...
	d.execute("""CREATE TABLE IF NOT EXISTS "comments" (
		"thread"	BLOB NOT NULL,
		"content"	BLOB NOT NULL,
		"username"	BLOB NOT NULL,
		"time"	REAL NOT NULL
	);""")

	return d

db = connect()

for row in db.execute("SELECT * from comments"):
	print(row)
	print(row[1])
	print(row.content)
	print(row['content'])

print(db.connection)

API

class database.Database(self, sql_engine, path, row_factory=None, *args, **kwargs)

  • sql_engine should be a DB-API 2.0 compatible interface, like sqlite3, etc. Most Python database libraries will follow this API.

  • If you don't supply a row_factory, then you'll get the magic that lets you access rows by index, string key or attribute. (e.g. row[0] == row['thread'] == row.thread in the above example).

    • An unknown value being accessed will raise a KeyError.
  • If you supply a row_factory, then it'll be installed as per usual.

  • Other arguments are passed to the connect function of sql_engine.

  • When a database.Database goes out of scope, or is deleted, it will check if it needs to do a commit, and then self-close.

  • When accessing a method or attribute, database.Database will check if we have an override, then check the underlying database object, and then a cursor object for that database object. It'll take the first one it finds. (e.g. db.connection will probably come from the cursor object).


License

See the LICENSE file for the legal text.

3-Clause BSD at time of writing.

shaknas_database_py's People

Contributors

shakna-israel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.