Python Forum
[Solved]Help with search statement-SQLite & Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved]Help with search statement-SQLite & Python
#1
Hello,

I have a function that takes in a user's input->stores it in a python variable called 'userQuery' and I would like to take the variable 'userQuery and use it in a select statement to search for parts within my database'

So how do I implement the 'userQuery' variable into the select statement so I can search for parts?

Thanks in advance.

My function right now:
#---------------------------------------------------------------
#                     Search Inventory Items 
#---------------------------------------------------------------
def searchInventory():

    userQuery = input('Item Name: ')

    #Connect to the inventory database (inventory.db)
    connection = sqlite3.connect("inventory.db")
    cursor = connection.cursor()
 
    cursor = connection.cursor()
    #Display all records in the Items Table:
    cursor.execute('select * from items where Name like %', userQuery, '%')
    
    #Get all info from the Items Table
    result = cursor.fetchall()

    #Print Results/Inventory in rows
    print('\n--------------------')
    print('Search Results')
    print('--------------------')
    for row in result:
        print('\n')
        print('--------------------')
        print("Item ID: ", row[0])
        print("Item Name: ", row[1])
        print("Item Quantity: ", row[2])
        print("Item Price: $", row[3])
        print("Item Sell Price: $", row[4])
        print("Item Description: ", row[5])
        print("Item Category: ", row[6])
        print("Item Location: ", row[7])
        print("Last Updated: ", row[10])
        print('--------------------')

    #Close the connection to the database
    connection.close()
    #Go to Admin Menu
    adminMenu()
Reply
#2
Nevermind, I got it.

 #Display all records in the Items Table:
    cursor.execute("select * from items WHERE Name LIKE'%'||?||'%'",(userQuery,))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help with data analysing with python and sqlite Hardcool 2 404 Jan-30-2024, 06:49 AM
Last Post: Athi
  python sqlite autoincrement in primary column janeik 6 1,228 Aug-13-2023, 11:22 AM
Last Post: janeik
  [Solved]Help with SQLite Login Form Extra 7 1,978 Jun-25-2022, 10:53 AM
Last Post: deanhystad
  Help with subtracting values using SQLite & Python Extra 10 3,513 May-10-2022, 08:36 AM
Last Post: ibreeden
  Help With Python SQLite Error Extra 10 15,314 May-04-2022, 11:42 PM
Last Post: Extra
  Python Sqlite georgebijum 0 1,076 May-04-2022, 10:12 AM
Last Post: georgebijum
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,210 Jun-29-2020, 08:51 PM
Last Post: buran
  how to use items combobox in table name sqlite in python hampython 1 2,719 May-24-2020, 02:17 AM
Last Post: Larz60+
  SQLite Query in Python rowyourboat 2 2,837 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  SQLite Query multiple column search whacky7 6 8,460 Apr-01-2019, 09:29 PM
Last Post: whacky7

Forum Jump:

User Panel Messages

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