用變量替換表名使用python和mysql連接器
顯示MysqL連接器的異常是告訴您該表在您的數(shù)據(jù)庫中不存在。
另外,您嘗試使用“ MachinePorn”作為參數(shù),但未在查詢中定義它,而是將其硬編碼為“ subredditName”。
我認(rèn)為您應(yīng)該在查詢中將數(shù)據(jù)庫定義為另一個(gè)參數(shù),它將正常運(yùn)行:
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')解決方法
我想動(dòng)態(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í)行相同操作時(shí),對于動(dòng)態(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')
我得到這個(gè)錯(cuò)誤,
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table ’sytykr.subredditname’ doesn’t exist
這使我相信我無法通過這種方式執(zhí)行操作,因此我想問一下如何執(zhí)行該操作,以便最終可以在表中傳遞變量名,而不必每次都對其進(jìn)行硬編碼。
相關(guān)文章:
1. PHP里10個(gè)鮮為人知但卻非常有用的函數(shù)2. Java 并行數(shù)據(jù)處理和性能分析3. Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法4. 詳解python第三方庫的安裝、PyInstaller庫、random庫5. iOS UIScrollView和控制器返回手勢沖突解決方法6. Python搭建Keras CNN模型破解網(wǎng)站驗(yàn)證碼的實(shí)現(xiàn)7. python算的上腳本語言嗎8. 解決python腳本中error: unrecognized arguments: True錯(cuò)誤9. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】10. ajax請求后臺得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹形下拉框的方法
