Oct-09-2022, 03:41 AM
(This post was last modified: Oct-09-2022, 02:00 PM by rob101.
Edit Reason: code corrections
)
This is what I have, in one of my functions, and I know it works, so maybe you can adapt it to your needs.
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).
removed. see the code belowthis
c.execute('SELECT * FROM dino WHERE name = dinoName’)should be this (I think)
removed. see the below postsHave 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
>>> 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