ocarina/src/core/tools/database.py

122 lines
1.9 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
(
arid INTEGER PRIMARY KEY,
name TEXT UNIQUE
);
CREATE TABLE album
(
alid INTEGER PRIMARY KEY,
artid INTEGER,
name TEXT UNIQUE
);
""")
global vals
vals["files"] = "?"
vals["artist"]= "?,?"
vals["album"] = "?,?,?"
def open():
global db, conn
conn = sqlite3.connect(db)
conn.text_factory = str
def close():
global conn
conn.commit()
conn.close()
conn = None
def init():
global db
db = join(settings.get("ocarina"),"ocarina.db")
dbExists = checkPath(db)
open()
if dbExists == False:
create()
close()
def insert(table, values):
global conn
if conn == None:
open()
global vals
#c = conn.cursor()
#t = (file,)
conn.execute('INSERT OR IGNORE INTO ' + table + ' VALUES(' + vals[table] + ')',values)
#c.close()
def count(table):
global conn
if conn == None:
open()
result = conn.execute('select count(*) from '+ table)
return result.fetchone()[0]
def select(select,table,where=None):
global conn
if conn == None:
open()
statement = 'select '+select+' from '+table
if not (where==None):
statement += ' where '+where
print statement
#return statement
result = conn.execute(statement)
#print result.fetchone()
#return result.fetchone()
return (0,)