samedi 20 février 2016

Python Sqlite Escape String Select Column

I'm trying to find a way to insert a value into my Sqlite query for selecting a column.

What I have working now is;

def ChangeItemQuantity(self, item_name, incrament_quantity):
    try:
        # Change given item quantity in database
        self.c.execute('''
             SELECT quantity
             FROM items
             WHERE itemName=?
             ''',(item_name,))
        current_quantity = self.c.fetchone()
        new_quantity = current_quantity[0] + incrament_quantity
        self.c.execute('''
             UPDATE items
             SET quantity = ?
             WHERE itemName=?
             ''',(new_quantity, item_name))
        self.conn.commit()

This works for changing a value in the quantity column, but I would like to reuse this method for changing the value in another column also, alertLevel.

So I would like to pass in the column name, something like this;

def ChangeItemQuantity(self, column_name, item_name, incrament_quantity):
    try:
        self.c.execute('''
             SELECT ?
             FROM items
             WHERE itemName=?
             ''',(column_name, item_name))

I've also tried;

        self.c.execute('''
             SELECT {}
             FROM items
             WHERE itemName={}
             '''.format(column_name, item_name))

Thank you for all your help.

Aucun commentaire:

Enregistrer un commentaire