python中mysql字段與關(guān)鍵字沖突怎么辦-創(chuàng)新互聯(lián)

這篇文章主要介紹python中mysql字段與關(guān)鍵字沖突怎么辦,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比烏翠網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式烏翠網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務覆蓋烏翠地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

解決方法:python中把字段名稱用反引號(`),也就是ESC下面~那個按鈕。

示例:

數(shù)據(jù)字段設(shè)計如下截圖所示

待插入數(shù)據(jù):

datas = {
  'sign_event':[
    {'id': 1, 'name': '華為mate9發(fā)布會' , 'limit': 100, 'status': 1, 'address': '會展中心1號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
    {'id': 2, 'name': '華為P1000發(fā)布會' , 'limit': 200, 'status': 1, 'address': '會展中心2號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
    {'id': 3, 'name': 'IPHONE888發(fā)布會' , 'limit': 300, 'status': 1, 'address': '會展中心3號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
    {'id': 4, 'name': '半壁江山66演唱會' , 'limit': 400, 'status': 1, 'address': '會展中心4號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
    {'id': 5, 'name': '金融P222222P上線' , 'limit': 500, 'status': 1, 'address': '會展中心5號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
    {'id': 6, 'name': '未命名0000發(fā)布會' , 'limit': 600, 'status': 1, 'address': '會展中心6號廳', 'start_time': '2017-09-20 14:00:00','create_time':'2017-08-20 14:00:00'},
  ],
}

插入語句實現(xiàn):

1.獲取某個表的所有待插入數(shù)據(jù)

 for tablename,data in datas.items():
     for d in data:
       self.insert_datatable(tablename,d)
 self.close_dataConnetion()

2.每個表的數(shù)據(jù),逐條循環(huán)插入到該表中

 def insert_datatable(self, tablename, table_data):
    keys = {}
    for key in table_data:
      # 從數(shù)據(jù)字段中取出列名,列名用反單引號括起來;--解決列名與mysql關(guān)鍵字沖突
      keys[key] = "`"+str(key)+"`"
      table_data[key] = "'"+str(table_data[key])+"'"
    key = ','.join(keys.values())
    value = ','.join(table_data.values())
 
    sql = "INSERT INTO " + tablename + " ( " + key + " ) VALUES ( " + value +" );"
    with self.connection.cursor() as cursor:
      cursor.execute('SET FOREIGN_KEY_CHECKS=0;') #取消外鍵約束
      cursor.execute(sql)
    self.connection.commit()

補充拓展:python 數(shù)據(jù)庫 % 沖突問題解決

在使用python后臺調(diào)用 MySQL數(shù)據(jù)庫的時候會有 「%」的關(guān)鍵字沖突問題,比如 用Python后端讀取 MySQL 中記錄的邏輯,在 impala端執(zhí)行,其中涉及到模糊匹配的 「%」會報錯

解決:SQL邏輯中的單個「%」換為「%%」即可,不錯的 trip。

以上是“python中mysql字段與關(guān)鍵字沖突怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享名稱:python中mysql字段與關(guān)鍵字沖突怎么辦-創(chuàng)新互聯(lián)
分享路徑:http://www.muchs.cn/article38/pessp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務Google、服務器托管App設(shè)計、網(wǎng)站維護ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)