pymysql操作mysql数据库

2019-12-02  本文已影响0人  月夜星空下

一、pymysql操作mysql数据库

pip install pymysql

1.1 pymysql操作数据库的五行拳

  1. 连接数据库
    使用Connect方法连接数据库
pymysql.Connections.Connection(host=None, user=None, password='', database=None, port=0,  charset='')
参数说明:
host – 数据库服务器所在的主机。
user – 登录用户名。
password – 登录用户密码。
database – 连接的数据库。
port – 数据库开放的端口。(默认: 3306)
charset – 连接字符集。
返回值:
        返回连接对象
          
       例子:
link = pymysql.Connect(host='localhost', port=3306, user='root', password='123456', db='zzl', charset='utf8')
  1. 创建游标
cursor = link.cursor()  
   cursor=pymysql.Cursors.DictCursor()  #[{}]
print(cursor.rowcount) #打印受影响行数
 方法                         说明                                      
 close()                    关闭游标                                    
 execute(query, args=None)  执行单条语句,传入需要执行的语句,是string类型;同时可以给查询传入参数,参数可以是tuple、list或dict。执行完成后,会返回执行语句的影响行数。
 fetchone()                 取一条数据                                   
 fetchmany(n)               取多条数据                                   
 fetchall()                 取所有数据                                   
 _executed属性                正在执行的sql语句        
  1. 执行sql语句
# 执行sql语句
sql = 'select * from user1'
# 执行完sql语句,返回受影响的行数
num = cursor.execute(sql)
  1. 获取结果集
result1 = cursor.fetchone()
print(result1)
  1. 关闭连接
cursor.close()
link.close()
sudo chmod +x conndb.py
./conndb.py

1.2 pymysql中事务处理

pymysql默认是没有开启自动提交事务,所以我们如果进行增、删、改,就必须手动提交或回滚事务。

sql = 'delete from user where id=%s' % user_id

# 如果要执行增删改语句的时候,下面的就是固定格式

try:
   cursor.execute(sql)
# 如果全部执行成功,提交事务
   link.commit()
   print(cursor.lastrowid) #获取最后插入记录的自增id号
except Exception as e:
   print(e)
   link.rollback()

1.3 防sql注入

二、封装数据库操作类

2.1.数据库操作类的封装

db.where('username="admin"').table('blog_user').field('username,password').select()
# select usenrame,password from blog_user where username='admin'
# sql = "SELECT {fields} FROM {table} {where} {groupby} {having} {orderby} {limit}".format()

2.2 连贯操作

方法要返回self

2.3 方法的无顺序调用

2.4 字段缓存

三、分页类

分页是通过解析url中page参数(可以自己指定),结合sql语句中limit子句,从数据库中查询每个页面所需数据,具体可以分为以下步骤:

上一篇 下一篇

猜你喜欢

热点阅读