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

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

python mysql 字段與關(guān)鍵字沖突的解決方式

瀏覽:6日期:2022-07-27 18:25:35

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

示例:

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

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

datas = { ’sign_event’:[ {’id’: 1, ’name’: ’華為mate9發(fā)布會(huì)’ , ’limit’: 100, ’status’: 1, ’address’: ’會(huì)展中心1號(hào)廳’, ’start_time’: ’2017-09-20 14:00:00’,’create_time’:’2017-08-20 14:00:00’}, {’id’: 2, ’name’: ’華為P1000發(fā)布會(huì)’ , ’limit’: 200, ’status’: 1, ’address’: ’會(huì)展中心2號(hào)廳’, ’start_time’: ’2017-09-20 14:00:00’,’create_time’:’2017-08-20 14:00:00’}, {’id’: 3, ’name’: ’IPHONE888發(fā)布會(huì)’ , ’limit’: 300, ’status’: 1, ’address’: ’會(huì)展中心3號(hào)廳’, ’start_time’: ’2017-09-20 14:00:00’,’create_time’:’2017-08-20 14:00:00’}, {’id’: 4, ’name’: ’半壁江山66演唱會(huì)’ , ’limit’: 400, ’status’: 1, ’address’: ’會(huì)展中心4號(hào)廳’, ’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’: ’會(huì)展中心5號(hào)廳’, ’start_time’: ’2017-09-20 14:00:00’,’create_time’:’2017-08-20 14:00:00’}, {’id’: 6, ’name’: ’未命名0000發(fā)布會(huì)’ , ’limit’: 600, ’status’: 1, ’address’: ’會(huì)展中心6號(hào)廳’, ’start_time’: ’2017-09-20 14:00:00’,’create_time’:’2017-08-20 14:00:00’}, ],}

插入語(yǔ)句實(shí)現(xiàn):

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

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

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

def insert_datatable(self, tablename, table_data): keys = {} for key in table_data: # 從數(shù)據(jù)字段中取出列名,列名用反單引號(hào)括起來(lái);--解決列名與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()

補(bǔ)充拓展:python 數(shù)據(jù)庫(kù) % 沖突問(wèn)題解決

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

解決:SQL邏輯中的單個(gè)「%」換為「%%」即可,不錯(cuò)的 trip。

以上這篇python mysql 字段與關(guān)鍵字沖突的解決方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 裕民县| 商南县| 遂平县| 昌黎县| 玉环县| 揭东县| 彭州市| 松潘县| 鄂温| 马山县| 社会| 南宫市| 平罗县| 精河县| 邯郸市| 兴海县| 岳普湖县| 监利县| 曲阜市| 奉贤区| 鹰潭市| 台南县| 商都县| 巴里| 桐柏县| 松滋市| 旬邑县| 南部县| 绥江县| 镇原县| 广宁县| 潞西市| 肇庆市| 丘北县| 涟水县| 长寿区| 天祝| 平山县| 水富县| 通州市| 商都县|