简体   繁体   中英

Python SQLite3 search by column value

I am looking for a way to get a row from a database and every value in it. For example, by searching by Name or ItemNumber and the code will retrieve the value for Price or any other column value in the row. I have tried using this code and it returns every row and values for those rows instead of just 1 as I am looking for.

import sqlite3
cursor = sqliteConnection.cursor()
sqlite_select_query = """SELECT 374932 from ItemLookup"""
cursor.execute(sqlite_select_query)
rows = cursor.fetchall()
for row in rows:
    print(row)

I have also used cursor.fetchone() and this returns 374932 provided in the SELECT 374932 from ItemLookup code multiple times. Using fetchmany() with any row[0] or row[1] values displays a tuple out of range error message. I have searched the internet and cannot find an explanation for this. Any help would be appreciated!

SQL queries generally follow the syntax of SELECT field FROM table WHERE condition for example, this is a query for Joe's information: SELECT * FROM ItemLookup WHERE Name = 'Joe'

You can use this to query the information you want based on specific conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM