久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

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

我認為您應(yīng)該在查詢中將數(shù)據(jù)庫定義為另一個參數(shù),它將正常運行:

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')解決方法

我想動態(tài)更改插入數(shù)據(jù)的表的變量名。

這目前有效,

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')

但是,當(dāng)我嘗試對表名(在這種情況下為“ FoodPorn”)執(zhí)行相同操作時,對于動態(tài)表(如本例中的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

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

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 陆良县| 视频| 江川县| 邻水| 苍溪县| 乡宁县| 隆安县| 奉新县| 洪泽县| 涡阳县| 河南省| 灵石县| 安龙县| 彭阳县| 崇仁县| 东光县| 永福县| 嘉黎县| 金寨县| 正宁县| 紫金县| 灌阳县| 来宾市| 夏河县| 新野县| 静海县| 麟游县| 赫章县| 宁德市| 交口县| 德令哈市| 雷波县| 岢岚县| 中方县| 孟村| 从化市| 西华县| 平泉县| 大英县| 乌海市| 侯马市|