第二十五天学习:mysql(二)

一、游标
 
游标是系统为用户开设的一个数据缓冲区,存放sql语句的执行结果
用户可以用sql语句逐一从游标中获取记录,并赋予主变量,交由python进一步处理,一组主变量一次只能存放一条记录。
常用方法:
cursor():创建游标对象
close():关闭游标对象
fetchone():得到结果集的下一行
fetchmany([size=curor.arraysize]):得到结果集的下几行
fetchall(): 得到结果集中的剩下的所有行
execute(sql) : 执行一个数据库查询或命令
executemany(sql, args) : 执行多个数据库查询或命令
 
实例:

1

mysql> select * from test;
+-----+
| id  |
+-----+
| 100 |
|   1 |
|   2 |
|   3 |
|   4 |
|   6 |
|   7 |
|  10 |
| 106 |
+-----+

# cat demon3.py 
#!/usr/bin/python
#-*- coding:utf-8 -*-
from mysql_conn import connect_mysql

if __name__ == '__main__':
    sql = 'select * from test;'
    cnx = connect_mysql()
    cus = cnx.cursor()
    try:
        cus.execute(sql)
        result = cus.fetchone()
        print(result)
        result_many = cus.fetchmany(3)
        result_all = cus.fetchall()
        print(result_many)
        print(result_all)
        cus.close()
    except Exception as e:
        cnx.rollback()
        raise e
    finally:
        cus.close()

结果:
# python demon3.py 
(100L,)
((1L,), (2L,), (3L,))
((4L,), (6L,), (7L,), (10L,), (106L,))

  

2

#!/usr/bin/python
#-*- coding:utf-8 -*-
from mysql_conn import connect_mysql

if __name__ == '__main__':
    sql = 'select * from test;'
    sql_many = 'insert into test(id) value (%s);'
    param = []
    for i in xrange(200,210):
        param.append([str(i)])
    print(param)
    cnx = connect_mysql()
    cus = cnx.cursor()
    try:
        cus.execute(sql)
        cus.executemany(sql_many, param)
        result = cus.fetchone()
        print(result)
        result_many = cus.fetchmany(3)
        result_all = cus.fetchall()
        print(result_many)
        print(result_all)
        cus.close()
        cnx.commit()
    except Exception as e:
        cnx.rollback()
        raise e
    finally:
        cus.close()
结果
mysql> select * from test;
+-----+
| id  |
+-----+
| 100 |
|   1 |
|   2 |
|   3 |
|   4 |
|   6 |
|   7 |
|  10 |
| 106 |
| 200 |
| 201 |
| 202 |
| 203 |
| 204 |
| 205 |
| 206 |
| 207 |
| 208 |
| 209 |
+-----+
19 rows in set (0.00 sec)

  

a、先建立mysql连接对象
b、创建游标
c、获取数据

二、mysql连接池
 
使用数据库的连接池技术,来访问数据库达到资源复用的目的
python的数据库连接池包 DBUtils:
DBUtils是一套Python数据库连接池包,并允许对非线程安全的数据库接口进行线程安全包装
DBUtils提供两种外部接口:
* PersistentDB :提供线程专用的数据库连接,并自动管理连接。
* PooledDB :提供线程间可共享的数据库连接,并自动管理连接。
使用pip安装:
Pip install DBUtils
实例:

# cat demon4.py 
#!/usr/bin/python
#-*- coding:utf-8 -*-
import MySQLdb
from DBUtils.PooledDB import PooledDB
db_config = {
        'host' : 'localhost',
        'port' : 3306,
        'user' : 'test',
        'passwd' : '123456',
        'db' : 'python',
        }

pool = PooledDB(MySQLdb, 5, **db_config)
# 5为连接池里的最少连接数
if __name__ == '__main__':
    cnx = pool.connection()
# 以后每次需要数据库连接就是用connection()函数获取连接就好了
    cus = cnx.cursor()
    SQL = 'select * from test;'
    try:
        cus.execute(SQL)
        result = cus.fetchall()
        print(result)
        cus.close()
        cnx.commit()
    except Exception as e:
        raise e
    finally:
        cnx.close()

  

PooledDB的参数:
1. mincached,最少的空闲连接数,如果空闲连接数小于这个数,pool会创建一个新的连接
2. maxcached,最大的空闲连接数,如果空闲连接数大于这个数,pool会关闭空闲连接
3. maxconnections,最大的连接数,
4. blocking,当连接数达到最大的连接数时,在请求连接的时候,如果这个值是True,请求连接的程序会一直等待,直到当前连接数小于最大连接数,如果这个值是False,会报错,
5. maxshared 当连接数达到这个数,新请求的连接会分享已经分配出去的连接
 
连接池对性能的提升表现在:
1.在程序创建连接的时候,可以从一个空闲的连接中获取,不需要重新初始化连接,提升获取连接的速度
2.关闭连接的时候,把连接放回连接池,而不是真正的关闭,所以可以减少频繁地打开和关闭连接

三、设计表结构
 
根据自己的业务需要和属性,设计不同的表结构,设计的表结构:
 
Student
       
字段名
类型
是否为空
主键
描述
StdID
int
学生ID
StdName
varchar(100)
 
学生姓名
Gender
enum('M', 'F')
 
性别
Age
tinyint
 
年龄
         
         
         
Course
       
字段名
类型
是否为空
主键
描述
CouID
int
课程ID
Cname
varchar(50)
 
课程名字
TID
int
 
老师ID
         
         
         
Score
       
字段名
类型
是否为空
主键
描述
SID
int
分数ID
StdID
int
 
学生id
CouID
int
 
课程id
Grade
int
 
分数
 
 
       
Teacher
       
字段名
类型
是否为空
主键
描述
TID
int
老师ID
Tname
varcher(100)
 
老师名字
 

原文地址:https://www.cnblogs.com/yshan13/p/7876510.html