复习&Python DB-API

一、python的DB-API

  1.Python的DB-API,为大多数的数据库实现了接口,使用它连接各数据库后,就可以用相同 的方式操作各数据库。

  Python DB-API使用流程:

       1. 引入API模块。 2. 获取与数据库的连接。 3. 执行SQL语句和存储过程。 4. 关闭数据库连接。

       2.Python操作mysql

  安装包: MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的。 pip好像是不支持安装MySQLdb的,我们可以通过网站下载安装, 下载地址:https://pypi.python.org/pypi/MySQL-python/1.2.5 分别对应有windows和源码安装的方法 安装依赖包: yum install –y python-devel yum install –y mysql-devel yum install –y gcc

     注:大师兄给推荐了一个连接操作MYsql工具:navicat。 好用

      Mysql的事物

  一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性)、Consistency(稳定性)、Isolation(隔离性)、Durability(可靠性) 1、事务的原子性:一组事务,要么成功;要么撤回。 2、稳定性 : 有非法数据(外键约束之类),事务撤回。 3、隔离性:事务独立运行。一个事务处理后的结果,影响了其他事务,那么其他事务会撤回。事务的100%隔离,需要牺牲速度。 4、可靠性:软、硬件崩溃后,InnoDB数据表驱动会利用日志文件重构修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit选项 决定什么时候吧事务保存到日志里

  mysql> show variables like 'auto%';

   +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | auto_increment_increment | 1 | | auto_increment_offset | 1 | | autocommit | ON | | automatic_sp_privileges | ON | +--------------------------+-------+

    

  3、Mysql的常用操作

  https://note.youdao.com/share/?id=44df41885afc36e471a836ddba4f9876&type=note#/

  授权超级用户: grant all privileges on *.* to 'tangnanbing'@'%' identified by '1qaz@WSX' with grant option;  查看库: show databases;   查看都有哪些库 show databases;   查看某个库的表 use db; show tables \G;   查看表的字段 desc tb;   查看建表语句 show create table tb;   当前是哪个用户 select user();   当前库 select database();   创建库 create database db1;   创建表 create table t1 (id int, name char(40) adress varchar(30)); char(10) 'aaa ' varchar(10) 'aaa'   查看数据库版本 select version();   查看mysql状态 show status;   修改mysql参数 show variables like 'max_connect%'; set global max_connect_errors = 1000;   查看mysql队列 show processlist; select * from information_schema.processlist where info is not null; sleep的可以忽略,qurey查询的才有 创建普通用户并授权 grant all on *.* to databases1.user1 identified by '123456'; grant all on db1.* to 'user2'@'10.0.2.100' identified by '111222'; grant all on db1.* to 'user3'@'%' identified by '231222';insert into tb1 (id,name) values(1,'aming'); 更改密码 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user='username' ; 查询 select count(*) from mysql.user; select * from mysql.db; select * from mysql.db where host like '10.0.%'; 插入 update db1.t1 set name='aaa' where id=1; 清空表 truncate table db1.t1; 删除表 drop table db1.t1; 删除数据库 drop database db1; 修复表 repair table tb1 [use frm]; 查看权限show grants for root@'localhost'; echo "select user,host,password from mysql.user" |mysql -uroot -plingxiangxiang mysql -uroot -p1234556 -e "select user,host,password into outfile '/home/mysql/1.txt' from mysql.user;" ;

  4.Mysql的连接

  1.创建数据库 create database python; 2. 授权用户 grant all privileges on *.* to xiang@’%’ identified by ‘123456’; flush privilege; conn=MySQLdb.connect(host="192.168.48.128",user="xiang",passwd="123456",db="python",charset="utf8") 比较常用的参数包括: host:数据库主机名.默认是用本地主机 user:数据库登陆名.默认是当前用户 passwd:数据库登陆的秘密.默认为空 db:要使用的数据库名.没有默认值 port:MySQL服务使用的TCP端口.默认是3306,数字类型 charset:数据库编码

  推荐大家使用函数的方式: def connect_mysql(): db_config = { 'host': '192.168.48.128', 'port': 3306, 'user': 'xiang', 'passwd': '123456', 'db': 'python', 'charset': 'utf8' } cnx = MySQLdb.connect(**db_config) return cnx

案例一:

import pymysql


# 1. 开启事务
# 2. 执行sql语句(update100, insert1000, alter10)
# 3. commit;


conn = pymysql.connect(host="192.168.48.136", port=3306, user="xiang", passwd="xiang", db="test")
cus = conn.cursor()
sql = "select * from test2;"

cus.execute(sql)
result = cus.fetchall()
print(result)
cus.close()
conn.close()


案例二:
import pymysql



class TestMysql(object):
def __init__(self):
self.dbConfig = {
"host": "192.168.48.136",
"port": 3306,
"user": "xiang",
"passwd": "xiang",
"db": "test"
}
conn = pymysql.connect(**self.dbConfig)
self.a = conn


def select(self):
print("select")

def update(self):
print("update")

if __name__ == '__main__':
conn = TestMysql()
 
posted @ 2018-05-07 09:46  wss_student  阅读(715)  评论(0编辑  收藏  举报