国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

用變量替換表名。使用python和mysql連接器

瀏覽:3日期:2022-08-07 09:02:46
如何解決用變量替換表名。使用python和mysql連接器?

顯示MysqL連接器的異常是告訴您該表在您的數據庫中不存在。

另外,您嘗試使用“ MachinePorn”作為參數,但未在查詢中定義它,而是將其硬編碼為“ subredditName”。

我認為您應該在查詢中將數據庫定義為另一個參數,它將正常運行:

def dataEntry(subreddit, _title, _post_url, _imageURL): cnx = MysqL.connector.connect(**config) c = cnx.cursor() insert = cnx.escape_string('INSERT INTO MachinePorn (subreddit, title, post_url, imageURL) VALUES (%s, %s, %s, %s)') data_value = (subreddit, _title, _post_url, _imageURL) c.execute(insert, data_value) cnx.commit() c.close() cnx.close()dataEntry('fake', 'fake', 'fake', 'fake')解決方法

我想動態更改插入數據的表的變量名。

這目前有效,

def dataEntry(subreddit,_title,_post_url,_imageURL): cnx = mysql.connector.connect(**config) c = cnx.cursor() insert = ('''INSERT INTO FoodPorn (subreddit,title,post_url,imageURL) VALUES (%s,%s,%s)''') data_value = (subreddit,_imageURL) c.execute(insert,data_value) cnx.commit() c.close() cnx.close()dataEntry('fake','fake','fake')

但是,當我嘗試對表名(在這種情況下為“ FoodPorn”)執行相同操作時,對于動態表(如本例中的MachinePorn),

def dataEntry(subreddit,_imageURL): cnx = mysql.connector.connect(**config) c = cnx.cursor() insert = ('''INSERT INTO subredditName (subreddit,%s)''') data_value = ('MachinePorn',subreddit,'fake')

我得到這個錯誤,

mysql.connector.errors.ProgrammingError: 1146 (42S02): Table ’sytykr.subredditname’ doesn’t exist

這使我相信我無法通過這種方式執行操作,因此我想問一下如何執行該操作,以便最終可以在表中傳遞變量名,而不必每次都對其進行硬編碼。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 天长市| 鄯善县| 科尔| 晴隆县| 南通市| 利川市| 长岭县| 宁津县| 汝南县| 阿克陶县| 织金县| 类乌齐县| 东阿县| 确山县| 明水县| 科技| 比如县| 平谷区| 连城县| 成武县| 阿坝县| 施秉县| 东阳市| 广饶县| 平利县| 报价| 祁门县| 化隆| 广州市| 安顺市| 麻栗坡县| 方山县| 莱芜市| 嘉义市| 青川县| 武乡县| 张家港市| 布尔津县| 洪湖市| 正安县| 精河县|