Exemple #1
0
def insertInTable(dsn, table, columns, vals, TRACE=False):
    """ insert values in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "insert into %s(%s) values(%s)" % (table, columns, vals)
    trace(TRACE, "Insertion: %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception, message:
        conn.rollback()
        error("Cannot insert in database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
Exemple #2
0
def selectWithSQLRequest(dsn, sql, TRACE=False):
    """ select multiple lines in a table with a complete sql """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = sql
    trace(TRACE, "Selection: %s" % req)
    try:
        cursor.execute(req)
        data = cursor.fetchall()
        cursor.close()
    except Exception, message:
        error("Cannot select from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return None
Exemple #3
0
def selectAllInTable(dsn, table, selection, condition='', TRACE=False):
    """ select multiple lines in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "select %s from %s" % (selection, table)
    if condition:
        req += ' where %s' % condition
    trace(TRACE, "Selection: %s" % req)
    try:
        cursor.execute(req)
        data = cursor.fetchall()
        cursor.close()
    except Exception, message:
        error("Cannot select from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return None
Exemple #4
0
def updateTable(dsn, table, updates, condition='', TRACE=False):
    """ update columns in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "update %s set %s" % (table, updates)
    if condition:
        req += ' where %s' % condition
    trace(TRACE, "Update: %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception, message:
        conn.rollback()
        error("Cannot update in database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
Exemple #5
0
def deleteTable(dsn, table, condition='', TRACE=False):
    """ delete a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "delete from %s" % (table)
    if condition:
        req += ' where %s' % condition
    trace("Deletion : %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception, message:
        conn.rollback()
        error("Cannot delete from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
Exemple #6
0
def selectWithSQLRequest(dsn, sql, TRACE=False):
    """ select multiple lines in a table with a complete sql """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = sql
    trace(TRACE, "Selection: %s" % req)
    try:
        cursor.execute(req)
        data = cursor.fetchall()
        cursor.close()
    except Exception as message:
        error("Cannot select from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return None
    conn.close()
    return data
Exemple #7
0
def insertInTable(dsn, table, columns, vals, TRACE=False):
    """ insert values in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "insert into %s(%s) values(%s)" % (table, columns, vals)
    trace(TRACE, "Insertion: %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception as message:
        conn.rollback()
        error("Cannot insert in database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
    conn.commit()
    conn.close()
    return True
Exemple #8
0
def selectAllInTable(dsn, table, selection, condition='', TRACE=False):
    """ select multiple lines in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "select %s from %s" % (selection, table)
    if condition:
        req += ' where %s' % condition
    trace(TRACE, "Selection: %s" % req)
    try:
        cursor.execute(req)
        data = cursor.fetchall()
        cursor.close()
    except Exception as message:
        error("Cannot select from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return None
    conn.close()
    return data
Exemple #9
0
def updateTable(dsn, table, updates, condition='', TRACE=False):
    """ update columns in a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "update %s set %s" % (table, updates)
    if condition:
        req += ' where %s' % condition
    trace(TRACE, "Update: %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception as message:
        conn.rollback()
        error("Cannot update in database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
    conn.commit()
    conn.close()
    return True
Exemple #10
0
def deleteTable(dsn, table, condition='', TRACE=False):
    """ delete a table """
    conn = openConnection(dsn)
    cursor = conn.cursor()
    req = "delete from %s" % table
    if condition:
        req += ' where %s' % condition
    trace(TRACE, "Deletion : %s" % req)
    try:
        cursor.execute(req)
        cursor.close()
    except Exception as message:
        conn.rollback()
        error("Cannot delete from database : %s" % message)
        error("Request was : '%s'" % req)
        conn.close()
        return False
    conn.commit()
    conn.close()
    return True