pymysql 插入數(shù)據(jù) 轉義處理方式
最近用pymysql把一些質(zhì)量不是很高的數(shù)據(jù)源導入mysql數(shù)據(jù)庫的時候遇到一點問題,主要是遇到像 \ 這樣的具有特殊意義的字符時比較難處理。這里有一個解決方案
基本環(huán)境
python3
pymysql
linux
問題描述
插入(查詢)數(shù)據(jù)時遇到一些特殊字符會使得程序中斷。操作失敗。比如 \這樣的轉義字符
解決方案
插入(查詢)之前用 connection.escape(str)處理一下即可
代碼示例
import pymongo sql_pattern = "select * from my_collection where name = %s" #注意,這里直接用%s,不要給%s加引號,因為后面轉移過后會自動加引號 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)
補充拓展:利用 pymysql 往數(shù)據(jù)庫插入百萬條數(shù)據(jù)
思路:
先創(chuàng)建一個自定義的數(shù)據(jù)庫表;
生成一個列表,列表中的數(shù)據(jù)應該和數(shù)據(jù)庫表中的每一列對應;
利用cursor.executemany 批量插入列表中的數(shù)據(jù)。
注意點:
批量添加數(shù)據(jù)時,數(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)建游標
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):
'''
生成一個列表,[('admin1', 'admin1qq.com', 'hahaadmin1'),...]
:param value: 自定義的數(shù)據(jù)量
:return: new_list
'''
new_list = [] # 新建一個空列表用來存儲元組數(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) # 構造元組
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ù) 轉義處理方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python中操作mysql的pymysql模塊詳解
- Python中模塊pymysql查詢結果后如何獲取字段列表
- Python MySQL數(shù)據(jù)庫連接池組件pymysqlpool詳解
- python使用pymysql實現(xiàn)操作mysql
- Python使用pymysql從MySQL數(shù)據(jù)庫中讀出數(shù)據(jù)的方法
- python3使用PyMysql連接mysql數(shù)據(jù)庫實例
- flask + pymysql操作Mysql數(shù)據(jù)庫的實例
- Python中pymysql 模塊的使用詳解
- pymysql模塊的使用(增刪改查)詳解
- 利用python中pymysql操作MySQL數(shù)據(jù)庫的新手指南
相關文章
使用grpc實現(xiàn)golang后端和python服務間通信
gRPC是Google 開發(fā)的高性能、開源的遠程過程調(diào)用(RPC)框架,本文主要為大家詳細介紹了如何使用grpc實現(xiàn)golang后端和python服務間通信,感興趣的可以了解下2024-03-03
pytorch_pretrained_bert如何將tensorflow模型轉化為pytorch模型
這篇文章主要介紹了pytorch_pretrained_bert將tensorflow模型轉化為pytorch模型的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

