def update(entry, ktable):
    
    params = ''
    for col in ktable.columns:
        value = escapeSql(getattr(entry, col.name))
        params += col.name + '=\'' + value + '\', '
    params = params[:-2]
    
    sql = "UPDATE %s SET %s WHERE id = ?" % (ktable.name,
                                             params)
    cursor = conn.cursor()
    cursor.execute(sql, (entry.id, ))
    
    """
    Update values in the kindex table too    
    First, remove index entries associated with this entry 
    """
    cursor.execute('DELETE FROM kindex WHERE ktable_id = ? AND row_id = ?', (ktable.id, entry.id))
    """
    Then insert new values in the kindex table     
    """
    for col in ktable.columns:
        value = escapeSql(getattr(entry, col.name))
        sql = 'INSERT INTO kindex(ktable_id, row_id, contents) VALUES(?, ?, ?)' 
        cursor.execute(sql, (ktable.id, entry.id, value))
    
    conn.commit()
def add(entry, ktable):
    
    entryColumns = ''
    entryValues = ''
    for col in ktable.columns:
        entryColumns += col.name + ', '
        value = escapeSql(getattr(entry, col.name))
        entryValues += '\'' + value + '\', '
    #remove last ', ' from both strings
    entryColumns = entryColumns[:-2]
    entryValues = entryValues[:-2]
    
    sql = 'INSERT INTO %s(%s) VALUES(%s)' % (ktable.name,
                                             entryColumns,
                                             entryValues)
    cursor = conn.cursor()
    cursor.execute(sql)
    entry.id=cursor.lastrowid
    
    """
    Insert values in the kindex table too    
    """
    for col in ktable.columns:
        value = escapeSql(getattr(entry, col.name))
        sql = 'INSERT INTO kindex(ktable_id, row_id, contents) VALUES(?, ?, ?)' 
        cursor.execute(sql, (ktable.id, entry.id, value))
    
    conn.commit()
def indexTable(ktable, readCursor, writeCursor):
    
    
    rs = readCursor.execute('SELECT * FROM %s' % (ktable.name, ))
    while True:
        row = rs.fetchone()
        if row is None:
            break
        rowId = row[0]
        for i in range(1, len(row)):
            if row[i]:
                sql = 'INSERT INTO kindex(ktable_id, row_id, contents) VALUES(%d, %d, \'%s\')' % (ktable.id, rowId, escapeSql(row[i]))
                writeCursor.execute(sql)