Python Forum
How to use the LIKE clause in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use the LIKE clause in Python
#6
This is what I have, in one of my functions, and I know it works, so maybe you can adapt it to your needs.
removed. see the code below
this
c.execute('SELECT * FROM dino WHERE name = dinoName’)
should be this (I think)
removed. see the below posts
Have a read of:

SQLite - SELECT Query



To add...

Maybe it would help you if you had my db script, rather than just a snippet, so here you go and I hope it's of some use to you (or anyone else, for that matter).

import sqlite3 as db
import os.path

path = '/home/rob/Desktop/python/sqlite/' #clearly, this will not work for you, so change it

#===========Database Functions===========#
def process_file(file, path = path):
    filename = os.path.join(path, file)
    try:
        f = open(filename, 'r')
    except FileNotFoundError:
        return file, 0
    else:
        f.close()
        return file, 1

def db_file(file_name):
    print(f"Testing connection...")
    database, test = process_file(file_name)
    if test:
        conn = db.connect(database)
        cur = conn.cursor()
        if conn:
            print (f"Connection to {database} established.")
            conn.close()
            print (f"Connection to {database} closed.")
    else:
        print(f"\nAlert!\n{database} database file not found.")
    print("Connection test complete.\n")
    return file_name, test

def get_table(database):
    items = ''
    if database:
        conn = db.connect(database)
        cur = conn.cursor()
        if conn:
            table = "table"
            data = cur.execute(f'SELECT name FROM sqlite_master where type=:table', {'table':table})
            tables = data.fetchall() # retuns a list of tuples
            if tables:
                table = ((tables[0])[0]) # get the name of the first (or only) table
                data = cur.execute(f'SELECT `_rowid_`,* FROM {table} ORDER BY `_rowid_` ASC LIMIT 0, 50000')
                items = data.fetchall()
            else:
                print(f"The {database} does not have any tables.")
                table = ""
            # house keeping
            cur.close()
            conn.close()
        if items:
            return items
    else:
        print("No database file found.")
        return database

def file_test(name):
    db_list = ['test.db','menu.db'] #add the db file names to this list. 'test.db' can be removed, if not needed
    ext = name.find('.')
    if ext < 0:
        name += '.db'
    if name in db_list:
        file_name, test = db_file(name)
        if not test:
            print(f"\nAlert!\nA new database file will be created.\n")
    else:    
        print(f"\nUnknown database file requested.\nIf {name} is a new database,\nyou'll need to add it\nto the file_test db_list\n")
        name = ""
    return name, #return a tuple, not a string
    
#=======End of database functions========#

file_name = input("Data base file name: ")

# file_test MUST be run first!
database = file_test(file_name)
table_object = get_table(database[0]) # returns a list of tuples

if table_object:
    print(table_object)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
How to use the LIKE clause in Python - by Columbo - Oct-08-2022, 04:15 PM
RE: How to use the LIKE clause in Python - by Yoriz - Oct-08-2022, 04:24 PM
RE: How to use the LIKE clause in Python - by rob101 - Oct-09-2022, 03:41 AM
RE: How to use the LIKE clause in Python - by Yoriz - Oct-09-2022, 08:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SQL Query is not executing WHERE clause hammer 7 2,449 Nov-15-2021, 01:44 PM
Last Post: hammer
  How does this if clause work? Pedroski55 3 2,351 Jun-10-2021, 06:31 AM
Last Post: Gribouillis
  can i raise an exception in a try clause? Skaperen 14 5,919 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  pass captured value from input() to where clause metro17 5 3,360 Sep-09-2019, 05:24 AM
Last Post: metro17
  finally clause Skaperen 6 3,977 Jun-02-2019, 09:02 PM
Last Post: snippsat
  if clause fails DeadCthulhuWaitsDreaming 10 4,896 Apr-07-2019, 09:19 PM
Last Post: DeadCthulhuWaitsDreaming
  how to code in Python "where" clause of SAS FelixS 2 2,865 Mar-26-2019, 04:59 PM
Last Post: FelixS
  break Statements and else Clause on Loops Phytonnewbie 2 2,853 Mar-02-2018, 09:50 AM
Last Post: buran
  My else clause doesn't work (general help) NMW 10 8,111 Jul-17-2017, 01:07 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020