日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

复习Python DB-API

發布時間:2023/12/10 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 复习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() ?

轉載于:https://www.cnblogs.com/iwss/p/9001094.html

總結

以上是生活随笔為你收集整理的复习Python DB-API的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。