ocarina/src/core/tools/database.py

135 lines
2.0 KiB
Python

# This is a simple test plugin, to make sure everything is working
__author__="bjschuma"
__date__ ="$Dec 27, 2009 9:58:50 PM$"
global name, app, type, path, opt
name = "database"
app = "ocarina"
type = "core"
path = ""
opt = []
import settings
from bt.file import *
import sqlite3
#global conn, c
#conn = None
#c = None
global db
global conn
global vals
db = None
conn = None
vals = dict()
# Collect all information for what to insert
# Build into a large statement, then insert
# This will speed things up
# Reconnect to sqlite at every run, to avoid thread problems
def create():
global conn
conn.executescript("""
CREATE TABLE files
(
path TEXT PRIMARY KEY
);
CREATE TABLE artist
(
id INTEGER PRIMARY KEY,
name TEXT UNIQUE
);
CREATE TABLE album
(
id INTEGER PRIMARY KEY,
name TEXT UNIQUE
);
CREATE TABLE library
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
path TEXT UNIQUE
);
""")
global vals
vals["files"] = "?"
vals["artist"]= "?,?"
vals["album"] = "?,?"
vals["library"] = "?,?,?"
def open():
global db, conn
conn = sqlite3.connect(db)
conn.text_factory = str
def close():
global conn
conn.commit()
conn.close()
conn = None
def commit():
global conn
if conn == None:
return
conn.commit()
def init():
global db
db = join(settings.get("ocarina"),"ocarina.db")
dbExists = checkPath(db)
open()
if dbExists == False:
create()
close()
def execute(statement, values=None):
global conn
if conn == None:
open()
if values == None:
return conn.execute(statement)
else:
return conn.execute(statement,values)
def insert(table, values):
global vals
execute('INSERT OR IGNORE INTO ' + table + ' VALUES(' + vals[table] + ')',values)
def count(table):
return execute('select count(*) from ' + table).fetchone()[0]
def select(select,table,where=None):
statement = 'select '+select+' from '+table
if not (where==None):
statement += ' where '+where
#print statement
try:
result = execute(statement)
return result
except:
print statement
return None