关于mysql中的事务、python访问mysql简单步骤、mysql常见操做等内容点击python访问mysql查看。
在前面介绍python访问mysql
的一个典型执行过程当中出现了cursor()
方法,它就是游标。咱们执行sql命令或者获得执行结果都须要使用游标。python
cursor
游标是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力。能够将游标当作一个指针,它能够指定结果中的任何位置,而后容许用户对指定位置的数据进行处理。
创建数据库链接以后,使用cursor
提供的方法执行命令或者返回结果。mysql
上述方法返回结果为受影响的行数。sql
size
条返回结果行。若是size值大于结果行的数量则返回cursor.arraysize条数据。上述方法返回结果为元组tuple
。
示例数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import pymysql def conn_mysql(): db_config={ 'host': '127.0.0.1', 'port': 3306, 'user': 'root', 'password': '123456', 'db': 'mysql' } conn = pymysql.connect(**db_config) return conn cnm = conn_mysql() cus = cnm.cursor() sql = 'select host,user from user' try: cus.execute(sql) print(cus.fetchmany(size=1)) #输出结果:(('localhost', 'mysql.session'),) print(cus.fetchall()) #输出结果:(('localhost', 'mysql.sys'), ('localhost', 'root')) print(cus.fetchone()) #输出结果:None cus.close() #关闭游标对象 cnm.commit() #正确则提交 except Exception as e: cnm.rollback() #错误则回滚 print("错误") finally: cnm.close() #数据库链接须要关闭 |
说明:
执行该sql最后结果为3条,fetchall()
、fetchmany()
和fetchone()
出现位置不一样,则结果不一样。
fetchmany(size=1) 取出结果集中的第一条。此时游标处于第二行开始位置。
fetchall() 取出结果集中的所有,也就是游标当前位置至结束,此时游标处于结果集末尾。
fetchone() 取出结果集中的下一条,此时游标处于末尾,返回None。
executemany说明
executemany()主要是针对一条sql须要执行屡次,每次执行的参数不一样状况。数据量较多时建议使用executemany()方法,速度较快。
下面是executemany()简单示例。编程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', db='test') cus = conn.cursor() sql = 'insert into test(id, name) values(%s, %s)' args = [(1, 'Alice'), (2, 'Bob'), (3, 'Cellary')] try: print(cus.executemany(sql, args)) #输出结果:3 except Exception as e: print("执行出错:{0}".format(e)) finally: cus.close() conn.commit() conn.close() |
python编程中使用pymysql进行数据库的链接及诸如查询/插入/更新等操做,可是每次链接mysql数据库请求时都是独立的额请求,比较浪费资源,并且访问数量达到必定数量时,对mysql性能会产生较大的影响。所以在实际使用过程当中一般会使用数据库的链接池技术来访问数据库,从而实现资源复用。同时也能够保证数据库不会由于链接数过多而形成数据库宕机。
在python中DBUtils
是一套数据库链接池包,提供2种接口。session
下面使用PooledDB示例ide
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import pymysql from DBUtils.PooledDB import PooledDB from DBUtils.PooledDB import PooledDB db_config = { "host": "127.0.0.1", "port": 3306, "user": "root", "passwd": "123456", "db": "test", # "charset": "utf8" } spool = PooledDB(pymysql, 5, **db_config) # 5为链接池里的最少链接数 conn = spool.connection() # 之后每次须要数据库链接就是用connection()函数获取链接 cur = conn.cursor() SQL = "select * from test;" r = cur.execute(SQL) r = cur.fetchall() print(r) cur.close() conn.close() |
常见的mysql操做温习函数
1 2 3 4 5 6 7 8 |
查看全部的库 show databases; 切换数据库 use test 查看库中全部的表 show tables; 查看数据库链接数 show processlist 受权超级用户 grant all privileges on *.* to 'user'@'%' identified by 'password' with grant option; 查询建表语句 show create table student\G 查询表结构 desc student; 查看index show index from student; |
\G
可使结果显示更容易查看。
数据库操做其实就是SQL语句,介绍经常使用的增删改查
语句。性能
1 2 3 4 5 6 7 |
create table student( StdID int not null, StdName varchar(100), Gender enum('M','F'), Age int, Score int ) |
mysql中经常使用数据类型为int
(整型)和varchar
(字符型),当字段取值为固定值时,能够采用枚举类型enum
。
char和varchar区别在于存储数据时,当数据大小小于声明的大小时,varchar
按照实际数据大小存放,char
则会添加空格至声明的数据大小。
varchar(5)存储123,实际存储为123
。
char(5)存储123,实际存储为123
(后面带2空格)。学习
1 2 3 |
insert into student(StdID,StdName,Gender,Age,Score)values(01,'xiaohh','M',18,99); insert into student(StdID,StdName,Score,Age)values(02,'Bob',59,18); insert into student(StdID,StdName,Score)values(03,'Alice',68),(04,'Lisa',88); |
最后一条语句为插入多行的写法。
更新update
1 |
update student set Score=100 where StdID=01 and Score=99; |
查询select
1 2 3 |
select * from student; select StdID,Score from student; select * from student where Score > 59 and Gender='M'; |
删除delete
1 2 3 |
delete from student where Score < 60; truncate table student; drop table student; |
truncate
:清空表中的数据,和不带where条件的delete做用同样。
drop
:删除表结构。
建立索引index
1 2 |
create index idx_std_stID on student(StdID); alter table student add index idx_std_stScore(StdID,Score); |
删除索引index
1 |
alter table student drop index idx_std_stScore; |
经过常见索引能够加快语句查询的速度,相似于字典中的目录。
那么咱们如何肯定语句是否使用索引呢?使用explain+sql语句便可。
explain select StdID from student;
mysql_explain_StdID
explain select Score from student;
mysql_explain_Score
explain select Age from student;
mysql_explain_Age
从字段key
中能够看到索引名称,若为null则表示未使用索引。
备注
对于sql语句及index索引只是介绍基本的语法规则,更多内容之后继续学习。
查询中可能涉及到多表的join
,可能会用到分组group by
,也可能会使用到更多的count()
、order by
等函数。
索引可能会涉及到惟一索引UNIQUE INDEX
、全文索引FULLTEXT INDEX
等内容。