pymysql 插入數(shù)據(jù) 轉(zhuǎn)義處理方式
最近用pymysql把一些質(zhì)量不是很高的數(shù)據(jù)源導(dǎo)入mysql數(shù)據(jù)庫的時(shí)候遇到一點(diǎn)問題,主要是遇到像 \ 這樣的具有特殊意義的字符時(shí)比較難處理。這里有一個(gè)解決方案
基本環(huán)境
python3
pymysql
linux
問題描述
插入(查詢)數(shù)據(jù)時(shí)遇到一些特殊字符會(huì)使得程序中斷。操作失敗。比如 \這樣的轉(zhuǎn)義字符
解決方案
插入(查詢)之前用 connection.escape(str)處理一下即可
代碼示例
import pymongo sql_pattern = "select * from my_collection where name = %s" #注意,這里直接用%s,不要給%s加引號(hào),因?yàn)楹竺孓D(zhuǎn)移過后會(huì)自動(dòng)加引號(hào) name = "xxx\xxx" name = connection.escape(name) sql = sql_pattern%name print(sql) # select * from my_collection where name = 'xxx\\xxx' with connection.cursor() as cursor: try: cursor.execute(sql) except: print(sql) pass for r in cursor: print(r)
補(bǔ)充拓展:利用 pymysql 往數(shù)據(jù)庫插入百萬條數(shù)據(jù)
思路:
先創(chuàng)建一個(gè)自定義的數(shù)據(jù)庫表;
生成一個(gè)列表,列表中的數(shù)據(jù)應(yīng)該和數(shù)據(jù)庫表中的每一列對(duì)應(yīng);
利用cursor.executemany 批量插入列表中的數(shù)據(jù)。
注意點(diǎn):
批量添加數(shù)據(jù)時(shí),數(shù)據(jù)格式必須list[tuple(),tuple(),tuple()] 或者tuple(tuple(),tuple(),tuple())
代碼解析:
# -*- coding: utf-8 -*- # Author:benjamin import pymysql # 創(chuàng)建連接 conn = pymysql.connect(host='192.168.214.128', port=3306, user='root', passwd='ben123', db='db2') # 創(chuàng)建游標(biāo) cursor = conn.cursor() def createTable(): ''' 創(chuàng)建數(shù)據(jù)庫表 :return: ''' try: sql = ''' create table mytable ( nid int not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, extra text )engine=innodb default charset=utf8 ''' cursor.execute(sql) conn.commit() print('create table ok!') except Exception as e: print(e) def myList(value): ''' 生成一個(gè)列表,[('admin1', 'admin1qq.com', 'hahaadmin1'),...] :param value: 自定義的數(shù)據(jù)量 :return: new_list ''' new_list = [] # 新建一個(gè)空列表用來存儲(chǔ)元組數(shù)據(jù) for i in range(1, value + 1): name = 'admin'+ str(i) email = name + '@qq.com' extra = 'I am '+ name tup = (name,email,extra) # 構(gòu)造元組 new_list.append(tup) # [(),(),()...] print("*"*5+"generate list ok"+"*"*5) return new_list def myInsert(newList): ''' 數(shù)據(jù)庫插入 :param newList: 傳入的列表數(shù)據(jù) :return: ''' try: sql = "insert into mytable(name,email,extra) values(%s,%s,%s)" # 要插入的數(shù)據(jù) cursor.executemany(sql,newList) # 執(zhí)行插入數(shù)據(jù) conn.commit() cursor.close() conn.close() print('insert ok') except Exception as e: print(e) if __name__ == '__main__': # 創(chuàng)建數(shù)據(jù)表 createTable() # 選擇要插入的數(shù)據(jù)量 value = 1000000 # 定義數(shù)據(jù)量 newList = myList(value) myInsert(newList)
以上這篇pymysql 插入數(shù)據(jù) 轉(zhuǎn)義處理方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python中操作mysql的pymysql模塊詳解
- Python中模塊pymysql查詢結(jié)果后如何獲取字段列表
- Python MySQL數(shù)據(jù)庫連接池組件pymysqlpool詳解
- python使用pymysql實(shí)現(xiàn)操作mysql
- Python使用pymysql從MySQL數(shù)據(jù)庫中讀出數(shù)據(jù)的方法
- python3使用PyMysql連接mysql數(shù)據(jù)庫實(shí)例
- flask + pymysql操作Mysql數(shù)據(jù)庫的實(shí)例
- Python中pymysql 模塊的使用詳解
- pymysql模塊的使用(增刪改查)詳解
- 利用python中pymysql操作MySQL數(shù)據(jù)庫的新手指南
相關(guān)文章
Pandas中的loc與iloc區(qū)別與用法小結(jié)
loc函數(shù):通過行索引 “Index” 中的具體值來取行數(shù)據(jù)(如取"Index"為"A"的行)而iloc函數(shù):通過行號(hào)來取行數(shù)據(jù)(如取第二行的數(shù)據(jù)),這篇文章介紹Pandas中的loc與iloc區(qū)別與用法,感興趣的朋友一起看看吧2024-01-01使用grpc實(shí)現(xiàn)golang后端和python服務(wù)間通信
gRPC是Google 開發(fā)的高性能、開源的遠(yuǎn)程過程調(diào)用(RPC)框架,本文主要為大家詳細(xì)介紹了如何使用grpc實(shí)現(xiàn)golang后端和python服務(wù)間通信,感興趣的可以了解下2024-03-03python利用OpenCV2實(shí)現(xiàn)人臉檢測(cè)
這篇文章主要為大家詳細(xì)介紹了python利用OpenCV2實(shí)現(xiàn)人臉檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12pytorch實(shí)現(xiàn)查看當(dāng)前學(xué)習(xí)率
這篇文章主要介紹了pytorch實(shí)現(xiàn)查看當(dāng)前學(xué)習(xí)率,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python實(shí)現(xiàn)的Iou與Giou代碼
今天小編就為大家分享一篇python實(shí)現(xiàn)的Iou與Giou代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01pytorch_pretrained_bert如何將tensorflow模型轉(zhuǎn)化為pytorch模型
這篇文章主要介紹了pytorch_pretrained_bert將tensorflow模型轉(zhuǎn)化為pytorch模型的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06