网站群建设原则,洛阳哪家网站做的好,平台搭建是什么,工作纪律和生活纪律研讨发言材料一、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/?id44df41885afc36e471a836ddba4f9876typenote#/ 授权超级用户 grant all privileges on *.* to tangnanbing% identified by 1qazWSX 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 user210.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 passwordPASSWORD(newpwd) WHERE userusername ; 查询 select count(*) from mysql.user; select * from mysql.db; select * from mysql.db where host like 10.0.%; 插入 update db1.t1 set nameaaa where id1; 清空表 truncate table db1.t1; 删除表 drop table db1.t1; 删除数据库 drop database db1; 修复表 repair table tb1 [use frm]; 查看权限show grants for rootlocalhost; 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; connMySQLdb.connect(host192.168.48.128,userxiang,passwd123456,dbpython,charsetutf8) 比较常用的参数包括: 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. commitconn pymysql.connect(host192.168.48.136, port3306, userxiang, passwdxiang, dbtest)cus conn.cursor()sql select * from test2;cus.execute(sql)result cus.fetchall()print(result)cus.close()conn.close()案例二 import pymysqlclass 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() 转载于:https://www.cnblogs.com/iwss/p/9001094.html