Skip to main content
Tweeted twitter.com/StackCodeReview/status/857806143896342529
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/SQLite>, <http://en.wikipedia.org/wiki/Python_%28programming_language%29>, and <http://en.wiktionary.org/wiki/seperate>).
Source Link

Fetch live data from sqliteSQLite

I' mI'm writing a small pythonPython script. Which which needs to fetch live data from a database (i'mI'm using sqlite3SQLite 3 for now). I may reuse this function for a lot manyof scripts. Can this code be made better?

def fetch_database(database, table, filter_dict, case=None):
    """ 
    Takes filter parameter as a dict
    dict = {'column_name':['filter', 'filter2'], 'column_name2'=[], ..}
    case = wrap around the dictionary key(UPPER, LOWER, etc..), None
    database (with pathname ex: database.db
    table
    column = dict.keys()
    """
   
    conn = sqlite3.connect(database)
    c = conn.cursor()
    filter_dict = dict_format(filter_dict) # A seperateseparate function deletes empty keys from dictionary  
    keys_list = filter_dict.keys()
    statement = 'SELECT * FROM ' + table
    if len(keys_list) > 0:
        statement += ' WHERE '
        for keys in keys_list:
            if case == None:
                key = keys
            else:
                key = case + '(' + keys + ')' 
            temp_data = filter_dict[keys]
            temp_size = len(temp_data)
            if keys_list.index(keys) != 0:
                statement += ' AND '
            if temp_size > 0:
                for data in temp_data:
                    if temp_data.index(data) == 0:
                        statement += key + '="' + data + '"' 
                    else:
                        statement += ' OR ' + key + '="' + data + '"' 
    dataset = c.execute(statement)
    dataset = dataset.fetchall()
    return dataset

Fetch live data from sqlite

I' m writing a small python script. Which needs to fetch live data from a database (i'm using sqlite3 for now). I may reuse this function for lot many scripts. Can this code be made better?

def fetch_database(database, table, filter_dict, case=None):
    """ 
    Takes filter parameter as a dict
    dict = {'column_name':['filter', 'filter2'], 'column_name2'=[], ..}
    case = wrap around the dictionary key(UPPER, LOWER, etc..), None
    database (with pathname ex: database.db
    table
    column = dict.keys()
    """
   
    conn = sqlite3.connect(database)
    c = conn.cursor()
    filter_dict = dict_format(filter_dict) # A seperate function deletes empty keys from dictionary  
    keys_list = filter_dict.keys()
    statement = 'SELECT * FROM ' + table
    if len(keys_list) > 0:
        statement += ' WHERE '
        for keys in keys_list:
            if case == None:
                key = keys
            else:
                key = case + '(' + keys + ')' 
            temp_data = filter_dict[keys]
            temp_size = len(temp_data)
            if keys_list.index(keys) != 0:
                statement += ' AND '
            if temp_size > 0:
                for data in temp_data:
                    if temp_data.index(data) == 0:
                        statement += key + '="' + data + '"' 
                    else:
                        statement += ' OR ' + key + '="' + data + '"' 
    dataset = c.execute(statement)
    dataset = dataset.fetchall()
    return dataset

Fetch live data from SQLite

I'm writing a small Python script which needs to fetch live data from a database (I'm using SQLite 3 for now). I may reuse this function for a lot of scripts. Can this code be made better?

def fetch_database(database, table, filter_dict, case=None):
    """
    Takes filter parameter as a dict
    dict = {'column_name':['filter', 'filter2'], 'column_name2'=[], ..}
    case = wrap around the dictionary key(UPPER, LOWER, etc..), None
    database (with pathname ex: database.db
    table
    column = dict.keys()
    """

    conn = sqlite3.connect(database)
    c = conn.cursor()
    filter_dict = dict_format(filter_dict) # A separate function deletes empty keys from dictionary
    keys_list = filter_dict.keys()
    statement = 'SELECT * FROM ' + table
    if len(keys_list) > 0:
        statement += ' WHERE '
        for keys in keys_list:
            if case == None:
                key = keys
            else:
                key = case + '(' + keys + ')'
            temp_data = filter_dict[keys]
            temp_size = len(temp_data)
            if keys_list.index(keys) != 0:
                statement += ' AND '
            if temp_size > 0:
                for data in temp_data:
                    if temp_data.index(data) == 0:
                        statement += key + '="' + data + '"'
                    else:
                        statement += ' OR ' + key + '="' + data + '"'
    dataset = c.execute(statement)
    dataset = dataset.fetchall()
    return dataset
edited title
Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

How can I make this function better , which fetches Fetch live data from sqlite?

Source Link

How can I make this function better , which fetches data from sqlite?

I' m writing a small python script. Which needs to fetch live data from a database (i'm using sqlite3 for now). I may reuse this function for lot many scripts. Can this code be made better?

def fetch_database(database, table, filter_dict, case=None):
    """ 
    Takes filter parameter as a dict
    dict = {'column_name':['filter', 'filter2'], 'column_name2'=[], ..}
    case = wrap around the dictionary key(UPPER, LOWER, etc..), None
    database (with pathname ex: database.db
    table
    column = dict.keys()
    """
   
    conn = sqlite3.connect(database)
    c = conn.cursor()
    filter_dict = dict_format(filter_dict) # A seperate function deletes empty keys from dictionary  
    keys_list = filter_dict.keys()
    statement = 'SELECT * FROM ' + table
    if len(keys_list) > 0:
        statement += ' WHERE '
        for keys in keys_list:
            if case == None:
                key = keys
            else:
                key = case + '(' + keys + ')' 
            temp_data = filter_dict[keys]
            temp_size = len(temp_data)
            if keys_list.index(keys) != 0:
                statement += ' AND '
            if temp_size > 0:
                for data in temp_data:
                    if temp_data.index(data) == 0:
                        statement += key + '="' + data + '"' 
                    else:
                        statement += ' OR ' + key + '="' + data + '"' 
    dataset = c.execute(statement)
    dataset = dataset.fetchall()
    return dataset