六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

分享一個純 Python 完成的 MySQL 客戶端設置庫

[摘要]PyMySQL 是一個純 Python 實現的 MySQL 客戶端操作庫,支持事務、存儲過程、批量執行等。PyMySQL 遵循 Python 數據庫 API v2.0 規范,并包含了 pure-Py...
PyMySQL 是一個純 Python 實現的 MySQL 客戶端操作庫,支持事務、存儲過程、批量執行等。PyMySQL 遵循 Python 數據庫 API v2.0 規范,并包含了 pure-Python MySQL 客戶端庫。

安裝

pip install PyMySQL

創建數據庫連接

import pymysql

connection = pymysql.connect(host='localhost',
                             port=3306,
                             user='root',
                             password='root',
                             db='demo',
                             charset='utf8')

參數列表:

參數描述
host數據庫服務器地址,默認 localhost
user用戶名,默認為當前程序運行用戶
password登錄密碼,默認為空字符串
database默認操作的數據庫
port數據庫端口,默認為 3306
bind_address當客戶端有多個網絡接口時,指定連接到主機的接口。參數可以是主機名或IP地址。
unix_socketunix 套接字地址,區別于 host 連接
read_timeout讀取數據超時時間,單位秒,默認無限制
write_timeout寫入數據超時時間,單位秒,默認無限制
charset數據庫編碼
sql_mode指定默認的 SQL_MODE
read_default_fileSpecifies my.cnf file to read these parameters from under the [client] section.
convConversion dictionary to use instead of the default one. This is used to provide custom marshalling and unmarshaling of types.
use_unicodeWhether or not to default to unicode strings. This option defaults to true for Py3k.
client_flagCustom flags to send to MySQL. Find potential values in constants.CLIENT.
cursorclass設置默認的游標類型
init_command當連接建立完成之后執行的初始化 SQL 語句
connect_timeout連接超時時間,默認 10,最小 1,最大 31536000
sslA dict of arguments similar to mysql_ssl_set()'s parameters. For now the capath and cipher arguments are not supported.
read_default_groupGroup to read from in the configuration file.
compressNot supported
named_pipeNot supported
autocommit是否自動提交,默認不自動提交,參數值為 None 表示以服務器為準
local_infileBoolean to enable the use of LOAD DATA LOCAL command. (default: False)
max_allowed_packet發送給服務器的最大數據量,默認為 16MB
defer_connect是否惰性連接,默認為立即連接
auth_plugin_mapA dict of plugin names to a class that processes that plugin. The class will take the Connection object as the argument to the constructor. The class needs an authenticate method taking an authentication packet as an argument. For the dialog plugin, a prompt(echo, prompt) method can be used (if no authenticate method) for returning a string from the user. (experimental)
server_public_keySHA256 authenticaiton plugin public key value. (default: None)
db參數 database 的別名
passwd參數 password 的別名
binary_prefixAdd _binary prefix on bytes and bytearray. (default: False)

執行 SQL

  • cursor.execute(sql, args) 執行單條 SQL

    # 獲取游標
    cursor = connection.cursor()
    
    # 創建數據表
    effect_row = cursor.execute('''
    CREATE TABLE `users` (
      `name` varchar(32) NOT NULL,
      `age` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    ''')
    
    # 插入數據(元組或列表)
    effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%s, %s)', ('mary', 18))
    
    # 插入數據(字典)
    info = {'name': 'fake', 'age': 15}
    effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%(name)s, %(age)s)', info)
    
    connection.commit()
  • executemany(sql, args) 批量執行 SQL

    # 獲取游標
    cursor = connection.cursor()
    
    # 批量插入
    effect_row = cursor.executemany(
        'INSERT INTO `users` (`name`, `age`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE age=VALUES(age)', [
            ('hello', 13),
            ('fake', 28),
        ])
    
    connection.commit()

注意:INSERT、UPDATE、DELETE 等修改數據的語句需手動執行connection.commit()完成對數據修改的提交。

獲取自增 ID

cursor.lastrowid

查詢數據

# 執行查詢 SQL
cursor.execute('SELECT * FROM `users`')

# 獲取單條數據
cursor.fetchone()

# 獲取前N條數據
cursor.fetchmany(3)

# 獲取所有數據
cursor.fetchall()

游標控制

所有的數據查詢操作均基于游標,我們可以通過cursor.scroll(num, mode)控制游標的位置。

cursor.scroll(1, mode='relative') # 相對當前位置移動
cursor.scroll(2, mode='absolute') # 相對絕對位置移動

設置游標類型

查詢時,默認返回的數據類型為元組,可以自定義設置返回類型。支持5種游標類型:

  • Cursor: 默認,元組類型

  • DictCursor: 字典類型

  • DictCursorMixin: 支持自定義的游標類型,需先自定義才可使用

  • SSCursor: 無緩沖元組類型

  • SSDictCursor: 無緩沖字典類型

無緩沖游標類型,適用于數據量很大,一次性返回太慢,或者服務端帶寬較小時。源碼注釋:

Unbuffered Cursor, mainly useful for queries that return a lot of data, or for connections to remote servers over a slow network.

Instead of copying every row of data into a buffer, this will fetch rows as needed. The upside of this is the client uses much less memory, and rows are returned much faster when traveling over a slow network
or if the result set is very big.

There are limitations, though. The MySQL protocol doesn't support returning the total number of rows, so the only way to tell how many rows there are is to iterate over every row returned. Also, it currently isn't possible to scroll backwards, as only the current row is held in memory.

創建連接時,通過 cursorclass 參數指定類型:

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='root',
                             db='demo',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)

也可以在創建游標時指定類型:

cursor = connection.cursor(cursor=pymysql.cursors.DictCursor)

事務處理

  • 開啟事務

connection.begin()

  • 提交修改

connection.commit()

  • 回滾事務

connection.rollback()

防 SQL 注入

  • 轉義特殊字符
    connection.escape_string(str)

  • 參數化語句
    支持傳入參數進行自動轉義、格式化 SQL 語句,以避免 SQL 注入等安全問題。

# 插入數據(元組或列表)
effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%s, %s)', ('mary', 18))

# 插入數據(字典)
info = {'name': 'fake', 'age': 15}
effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%(name)s, %(age)s)', info)

# 批量插入
effect_row = cursor.executemany(
    'INSERT INTO `users` (`name`, `age`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE age=VALUES(age)', [
        ('hello', 13),
        ('fake', 28),
    ])

參考資料

  • Python中操作mysql的pymysql模塊詳解

  • Python之pymysql的使用

相關推薦:

python實現telnet客戶端的方法

MemCached的PHP客戶端操作類二

數據庫mysql視頻教程

以上就是分享一個純 Python 實現的 MySQL 客戶端操作庫的詳細內容,更多請關注php中文網其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。




主站蜘蛛池模板: 日本高清网 | 香蕉久久夜色精品国产小说 | 伊人色婷婷综在合线亚洲 | 日本叼嗨| 中文字幕在线乱人伦 | 亚洲欧美另类综合 | 日韩 欧美 亚洲 国产 | 日韩伦理一区二区三区 | 伊人二区 | 亚洲视频免费在线观看 | 日本亚洲最大的色成网站www | 天天综合天天添夜夜添狠狠添 | 日韩中文字幕免费在线观看 | 怡春院日本一区二区久久 | 日本一本高清视频 | 午夜剧场黄 | 日韩大片免费在线观看 | 桃花综合久久久久久久久久网 | 日本一区欧美 | 日韩中文字幕免费版 | 小屁孩cao大人免费网站 | 日韩国产成人精品视频 | 亚洲伦理在线观看 | 人妖女天堂视频在线96 | 亚洲天堂影视 | 伊人久久综合影院首页 | 青青动漫 | 中文字幕视频不卡 | 婷婷色综合成人成人网小说 | 欧美综合亚洲图片综合区 | 欧美一级淫片aaaaaaa视频 | 性欧美人 | 午夜免费片| 天天射天天干天天色 | 午夜三级黄色片 | 亚洲精品欧洲精品 | 香蕉在线观看999 | 午夜91视频 | 速度与激情9完整版免费观看 | 午夜在线不卡 | 桃色app福利 |