Попытка сравнить строку с SQL столбцом - PullRequest
0 голосов
/ 29 марта 2020
con = lite.connect(db)  # connects to the database
cur = con.cursor()
for item in self.button_array:
    if item.isChecked():
        productName = item.text()
custCommand = '''SELECT COUNT(*) FROM Customers'''  # counts the number of rows in the table Customers
cur.execute(custCommand)
newCustomerID = cur.fetchall()[0][0] + 1000 # gives the order an CustomerID that is +1
print(newCustomerID)
con.close()
print(productName)
con = lite.connect(db)
cur = con.cursor()
findProductID = '''SELECT ProductID FROM Products WHERE ProductName = ''' + productName #searches for ProductID from the database and compares it to the product name
cur.execute(findProductID)
resultProductID = cur.fetchall()
print(resultProductID)
con.close()

Я пытаюсь получить его, чтобы он сравнивал столбец ProductName (просто столбец с именами цветов в нем) в таблице с текстом меток для цветов, но он продолжает придумывать следующее:

<class 'sqlite3.OperationalError'> no such column: Tulips <traceback object at 0x03FAA4E0>

(Тюльпаны - это всего лишь пример имеющегося цветка)

Поэтому кажется, что текст должен быть именем столбца, а не элементом в столбце, и я ' Я не очень уверен, как заставить это работать, я извиняюсь за плохое объяснение, но я действительно не знаю, что еще делать, так как я не настолько опытен с этим.

Править : Итак, я попытался поместить оператор в исполнение и использовать 'LIKE?' вместо этого

con = lite.connect(db)
            cur = con.cursor()
            findProductID = '''SELECT ProductID FROM Products WHERE ProductName = LIKE ?''' #searches for ProductID from the database and compares it to the product name
            cur.execute('''SELECT ProductID FROM Products WHERE ProductName = LIKE ?''',(productName))
            resultProductID = cur.fetchall()
            print(resultProductID)
            con.close()

Но в итоге получилось следующее:

<class 'sqlite3.OperationalError'> near "?": syntax error <traceback object at 0x0356A2D8>

1 Ответ

0 голосов
/ 31 марта 2020
findProductID = '''SELECT ProductID FROM Products WHERE ProductName = "{}"'''.format(productName)
            cur.execute(findProductID)
            resultProductID = cur.fetchall()
            print(resultProductID)
            con.close()
            #the final part of inserting into the table
            con = lite.connect(db)
            cur = con.cursor()
            command = '''INSERT INTO Entries(entryID, ProductID, CustomerID) VALUES ("{}", "{}", "{}")'''.format(newOrderID,resultProductID,newCustomerID)

, так что я сделал это с кодом, и он, кажется, работает, он сохраняет как "[('9003',)]", но это, вероятно, не будет слишком сложно исправить

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...