PythonでMySQLサーバにアクセスし、簡単なselect文を実行したいのですが、
下記の様なエラーが発生してしまいます。
やりたいこととしては、
「store_name = str(input("What is the store name? "))」でユーザ名を取得、
受け取ったユーザ名を使ってselect文を発行。
エラー文
What is the store name? amazon parameterized query failed You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '" + ?' at line 1 MySQL connection is closed
下記が実行ファイルになります。
mysql_select.py
1import mysql.connector 2 3store_name = str(input("What is the store name? ")) 4 5try: 6 connection = mysql.connector.connect(host='localhost', user='username', password='pasword', database='db') 7 8 cursor = connection.cursor(prepared=True) 9 sql_query = """select * from sample_tbl where store_name = " + %s""" 10 tuple1 = (store_name) 11 cursor.execute(sql_query, tuple1) 12 13 for tbl in cursor: 14 print(tbl) 15 16 connection.commit() 17 18 19except mysql.connector.Error as error: 20 print("parameterized query failed {}".format(error)) 21finally: 22 if connection.is_connected(): 23 cursor.close() 24 connection.close() 25 print("MySQL connection is closed")
sample_tbl
1mysql> select * from sample_tbl; 2+----+------------+------------+--------------+--------------+-------+---------------------+ 3| id | date | store_name | product_name | product_type | price | created_at | 4+----+------------+------------+--------------+--------------+-------+---------------------+ 5| 1 | 2022-09-14 | Amazon | Echo show | gadget | 56.7 | 2022-09-14 10:06:04 | 6| 3 | 2022-09-21 | Google | Pixel 6 Pro | gadget | 68000 | 2022-09-21 23:10:48 | 7+----+------------+------------+--------------+--------------+-------+---------------------+
大変お手数ではございますが、解決の為ヒントなどがあれご教示頂けますと幸いです。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/10 15:05