pymysql 插入數(shù)據(jù) 轉(zhuǎn)義處理方式
最近用pymysql把一些質(zhì)量不是很高的數(shù)據(jù)源導(dǎo)入mysql數(shù)據(jù)庫(kù)的時(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ù)庫(kù)插入百萬(wàn)條數(shù)據(jù)
思路:
先創(chuàng)建一個(gè)自定義的數(shù)據(jù)庫(kù)表;
生成一個(gè)列表,列表中的數(shù)據(jù)應(yīng)該和數(shù)據(jù)庫(kù)表中的每一列對(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ù)庫(kù)表
: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è)空列表用來(lái)存儲(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ù)庫(kù)插入
: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ù)庫(kù)連接池組件pymysqlpool詳解
- python使用pymysql實(shí)現(xiàn)操作mysql
- Python使用pymysql從MySQL數(shù)據(jù)庫(kù)中讀出數(shù)據(jù)的方法
- python3使用PyMysql連接mysql數(shù)據(jù)庫(kù)實(shí)例
- flask + pymysql操作Mysql數(shù)據(jù)庫(kù)的實(shí)例
- Python中pymysql 模塊的使用詳解
- pymysql模塊的使用(增刪改查)詳解
- 利用python中pymysql操作MySQL數(shù)據(jù)庫(kù)的新手指南
相關(guān)文章
pandas使用apply多列生成一列數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇pandas使用apply多列生成一列數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-11-11
python實(shí)現(xiàn)字符串加密 生成唯一固定長(zhǎng)度字符串
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)字符串加密,生成唯一固定長(zhǎng)度字符串,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python使用Pandas生成日?qǐng)?bào)的實(shí)現(xiàn)代碼
Pandas是Python中一個(gè)強(qiáng)大的數(shù)據(jù)處理庫(kù),它提供了許多功能強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具,在本文中,我們將介紹Pandas的基本概念和如何使用它生成一個(gè)包含今天到未來(lái)20個(gè)工作日的日期列表的Excel文件,需要的朋友可以參考下2023-11-11
Python設(shè)置在shell腳本中自動(dòng)補(bǔ)全功能的方法
今天小編就為大家分享一篇Python設(shè)置在shell腳本中自動(dòng)補(bǔ)全功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-06-06

