亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

使用 python 在字符串內(nèi)使用變量進行 SQL 查詢
P粉195402292
P粉195402292 2024-03-22 12:35:15
0
1
965

我想查看用戶輸入的sql數(shù)據(jù)庫world的特定表的所有數(shù)據(jù),下面的代碼將為我提供city表所需的輸出,但我希望表名由用戶輸入并希望制作我的代碼適用于所有情況。

from sqlite3 import connect
import mysql.connector 
mydb=mysql.connector.connect(host="localhost",user="root",password="",database='world')
mycursor=mydb.cursor()
mycursor.execute("select * from city")
for i in mycursor:
    print(i)

我想將 city 聲明為字符串變量

table_name=str(input("enter table name:"))
mycursor=mydb.cursor()
mycursor.execute("select * from table_name")
for i in mycursor:
    print(i)

但我收到錯誤 world.table_name 不存在,我明白。有什么辦法可以解決我的情況嗎?尋求善意的幫助,非常感謝。

P粉195402292
P粉195402292

全部回復(1)
P粉495955986

使用串聯(lián)

table_name=str(input("enter table name:"))
query = "SELECT * FROM " + table_name
mycursor.execute(query)
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板