#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pymysql
import chardet
class DBPymysql():
"""pymysql模块链接操作数据库"""
def __init__(self,ip='localhost',port=3306,dbname='test',user='root',password='123456',charset='utf8'):
"""初始化charset='utf8'"""
self.ip = ip
self.port = port
self.dbname = dbname
self.user = user
self.psw = password
self.charset = charset
def insertOperate(self):
"""数据库插入操作"""
# 创建链接
conn = pymysql.connect(host=self.ip,port=self.port,user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
# conn = pymysql.connect(host=self.ip,port=int(self.port),user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
try:
# 游标
cursor = conn.cursor()
cursor.execute("set names 'utf8'")
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #配置结果集为字典形式
sql='insert into test (id,name) values (%s,%s)'
name = "李四".encode('utf8')
print(chardet.detect(name))
res=cursor.execute(sql,('6',name))
print(res)
conn.commit()
except:
print("数据插入异常")
pass
finally:
cursor.close() # 关闭游标
conn.close() # 关闭链接
# return res
def queryOperate(self):
"""数据库查询操作queryInfo"""
# 创建链接
conn = pymysql.connect(host=self.ip,port=self.port,user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
res = ""
try:
# 游标
cursor = conn.cursor() #默认结果集为元组形式
# cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #配置结果集为字典形式
# sql="select * from user where id=%s and name=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
sql="select * from test"
# sql="select * from user where id=%s and name=%s"
# id = "10"
# name = "hahaa"
# res1=cursor.execute(sql,[id,name]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
res1=cursor.execute(sql)
# res = cursor.fetchone()
# res2=cursor.fetchone() #会接着上一次的查询记录结果继续往下查询
# res3=cursor.fetchone()
# res4=cursor.fetchmany(2) #查询两条记录会以元组套小元组的形式进行展示
res5=cursor.fetchall()
except:
print("数据插入异常")
pass
finally:
cursor.close() # 关闭游标
conn.close() # 关闭链接
return res5
def updateOperate(self):
"""数据库更新操作"""
# 创建链接
conn = pymysql.connect(host=self.ip,port=self.port,user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
# conn = pymysql.connect(host=self.ip,port=int(self.port),user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
try:
# 游标
cursor = conn.cursor()
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #配置结果集为字典形式
sql='update user set name=%s where id=%s'
name = "李四".encode('utf-8')
res=cursor.execute(sql,[name,"13"])
print(res)
conn.commit()
except:
print("更新数据异常")
pass
finally:
cursor.close() # 关闭游标
conn.close() # 关闭链接
def deleteOperate(self):
"""数据库删除操作"""
# 创建链接
conn = pymysql.connect(host=self.ip,port=self.port,user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
# conn = pymysql.connect(host=self.ip,port=int(self.port),user=self.user,password=self.psw,database=self.dbname,charset=self.charset)
try:
# 游标
cursor = conn.cursor()
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #配置结果集为字典形式
sql='delete from user where id = %s'
res=cursor.execute(sql,["12"])
conn.commit()
except:
print("删除数据异常")
pass
finally:
cursor.close() # 关闭游标
conn.close() # 关闭链接
if __name__ == '__main__':
dbp = DBPymysql(ip="localhost",port=3306,dbname="test1",user="root",password="******",charset='utf8')
dbp.insertOperate()
res = dbp.queryOperate()
print(res)
# dbp.updateOperate()
# dbp.deleteOperate()
# name = "李四".encode('utf-8')
# mychar = chardet.detect(name)
# bianma = mychar['encoding']
# print(bianma)