复习Python DB-API
一、python的DB-API
1.Python的DB-API,為大多數(shù)的數(shù)據(jù)庫實現(xiàn)了接口,使用它連接各數(shù)據(jù)庫后,就可以用相同 的方式操作各數(shù)據(jù)庫。
Python DB-API使用流程:
? ? ? ?1. 引入API模塊。 2. 獲取與數(shù)據(jù)庫的連接。 3. 執(zhí)行SQL語句和存儲過程。 4. 關(guān)閉數(shù)據(jù)庫連接。
? ? ? ?2.Python操作mysql
安裝包: MySQLdb 是用于Python鏈接Mysql數(shù)據(jù)庫的接口,它實現(xiàn)了 Python 數(shù)據(jù)庫 API 規(guī)范 V2.0,基于 MySQL C API 上建立的。 pip好像是不支持安裝MySQLdb的,我們可以通過網(wǎng)站下載安裝, 下載地址:https://pypi.python.org/pypi/MySQL-python/1.2.5 分別對應(yīng)有windows和源碼安裝的方法 安裝依賴包: yum install –y python-devel yum install –y mysql-devel yum install –y gcc
? ? ?注:大師兄給推薦了一個連接操作MYsql工具:navicat。 好用
? ? ??Mysql的事物
一般來說,事務(wù)是必須滿足4個條件(ACID): Atomicity(原子性)、Consistency(穩(wěn)定性)、Isolation(隔離性)、Durability(可靠性) 1、事務(wù)的原子性:一組事務(wù),要么成功;要么撤回。 2、穩(wěn)定性 : 有非法數(shù)據(jù)(外鍵約束之類),事務(wù)撤回。 3、隔離性:事務(wù)獨立運行。一個事務(wù)處理后的結(jié)果,影響了其他事務(wù),那么其他事務(wù)會撤回。事務(wù)的100%隔離,需要犧牲速度。 4、可靠性:軟、硬件崩潰后,InnoDB數(shù)據(jù)表驅(qū)動會利用日志文件重構(gòu)修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit選項 決定什么時候吧事務(wù)保存到日志里
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#/
授權(quán)超級用戶: 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; 當(dāng)前是哪個用戶 select user(); 當(dāng)前庫 select database(); 創(chuàng)建庫 create database db1; 創(chuàng)建表 create table t1 (id int, name char(40) adress varchar(30)); char(10) 'aaa ' varchar(10) 'aaa' 查看數(shù)據(jù)庫版本 select version(); 查看mysql狀態(tài) show status; 修改mysql參數(shù) 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查詢的才有 創(chuàng)建普通用戶并授權(quán) 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; 刪除數(shù)據(jù)庫 drop database db1; 修復(fù)表 repair table tb1 [use frm]; 查看權(quán)限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.創(chuàng)建數(shù)據(jù)庫 create database python; 2. 授權(quán)用戶 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") 比較常用的參數(shù)包括: host:數(shù)據(jù)庫主機名.默認(rèn)是用本地主機 user:數(shù)據(jù)庫登陸名.默認(rèn)是當(dāng)前用戶 passwd:數(shù)據(jù)庫登陸的秘密.默認(rèn)為空 db:要使用的數(shù)據(jù)庫名.沒有默認(rèn)值 port:MySQL服務(wù)使用的TCP端口.默認(rèn)是3306,數(shù)字類型 charset:數(shù)據(jù)庫編碼
推薦大家使用函數(shù)的方式: 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. 開啟事務(wù)
# 2. 執(zhí)行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() ?
轉(zhuǎn)載于:https://www.cnblogs.com/iwss/p/9001094.html
總結(jié)
以上是生活随笔為你收集整理的复习Python DB-API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Val编程-套接字
- 下一篇: Python爬虫爬取美剧网站