Aimy

from core.util import

database

The database.Database class provides DBMS service in a wrapper that I hope will work with any DBMS that supports python's standard set of methods. The class was originally developed using mysql, then the sqllite3 was added. At one point, both worked well but it's been years since I've had a mysql database running so it may need some updating. The point is that it provides a good chance for interchangability.

The Database class is undergoing some updates. More complete documentation will follow the release of core version 0.0018. For now, here are some simple examples.

Simple Open

There are several ways to create and use a Database object. The first is very simple.

from core.util import database

# Create/open a database by passing a file path.
db = database.Database(path='mydata.db', mod='sqlite3')
db.open()

# populate the table
db.execute('create table test (a,b,c)')
db.execute('insert into test values (1,2,3)')
db.commit()

# select the data
c = db.execute('select * from test')
c.fetchone()

Config Dict

If it's more convenient, you can pass a dict object with config.

conf = {
	"path" : "mydata.db",
	"mod" : "sqlite3"
}

from core.util import database
db = database.Database(conf)

Config With Arguments

If you want to open your database using additional arguments, use the config dict's "args" key.

conf = {
	"path" : "mydata.db",
	"args" : [11], # set timeout to 11
	"mod" : "sqlite3"
}

from core.util import database
db = database.Database(conf)
print db.path

Open Using Config File

You can also specify a config file instead that contains a JSON representation of the config dict. Remember to specify the path in your conf file or as a keyword argument.

db-config.json
{
	"path": "mydata.db",
	"args": [11],
	"mod": "sqlite3"
}	
from core.util import database
db = database.Database("db-config.json", path="mydata.db")
print db.path

db.open()
db.execute('insert into test values (1,2,3)')
db.commit()
c = db.execute('select * from test')
c.fetchall()