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

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

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

瀏覽:83日期:2022-08-07 15:15:08
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解決用變量替換表名使用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 編程
相關文章:
主站蜘蛛池模板: 周口市| 南木林县| 上栗县| 秭归县| 青田县| 福清市| 会理县| 婺源县| 揭西县| 体育| 东兰县| 沈阳市| 阿合奇县| 资中县| 三河市| 库尔勒市| 昂仁县| 青龙| 陈巴尔虎旗| 大同市| 佛教| 赤水市| 郧西县| 隆回县| 永济市| 枞阳县| 深州市| 大埔县| 甘肃省| 会宁县| 万山特区| 沁源县| 开化县| 瑞昌市| 博罗县| 东丰县| 佛冈县| 忻城县| 福海县| 利川市| 湖口县|