PyMySQL 使用筆記

2021-08-03 17:02:30 字數 3379 閱讀 9755

標籤: 資料庫 python

pymysql.connections.connection(self,

host=none, # 要連線的主機位址

user=none, # 用於登入的資料庫使用者

password='', # 密碼

database=none, # 要連線的資料庫

port=0, # 埠,一般為 3306

unix_socket=none, # 選擇是否要用unix_socket而不是tcp/ip

charset='', # 字元編碼

sql_mode=none, # default sql_mode to use.

read_default_file=none, # 從預設配置檔案(my.ini或my.cnf)中讀取引數

conv=none, # 轉換字典

use_unicode=none, # 是否使用 unicode 編碼

client_flag=0, # custom flags to send to mysql. find potential values in constants.client.

cursorclass='pymysql.cursors.cursor'>, # 選擇 cursor 型別

init_command=none, # 連線建立時執行的初始語句

connect_timeout=10, # 連線超時時間,(default: 10, min: 1, max: 31536000)

ssl=none, # a dict of arguments similar to mysql_ssl_set()'s parameters.for now the capath and cipher arguments are not supported.

read_default_group=none, # group to read from in the configuration file.

compress=none, # 不支援

named_pipe=none, # 不支援

no_delay=none, #

autocommit=false, # 是否自動提交事務

db=none, # 同 database,為了相容 mysqldb

passwd=none, # 同 password,為了相容 mysqldb

local_infile=false, # 是否允許載入本地檔案

max_allowed_packet=16777216, # 限制 `local data infile` 大小

defer_connect=false, # don't explicitly connect on contruction - wait for connect call.

auth_plugin_map={}, #

read_timeout=none, #

write_timeout=none,

bind_address=none # 當客戶有多個網路介面,指定乙個連線到主機

)

函式

說明cursor(cursor = none)建立乙個游標

commit()事務提交,如果沒有設為自動提交,則每次操作後必須提交事務,否則操作無效。

rollback()操作出錯時,可以用這個函式回滾到執行事務之前

close()關閉連線

- 乙個例子

import pymysql.cursors

config =

# 連線資料庫

connection = pymysql.connect(**config)

函式

說明callproc(procname, args=())執行乙個過程

execute(query,args=none)執行一條sql語句,返回受影響的行數。若args是列表,用%s做佔位符,若是字典,用%(name)s

executemany(query,args)對乙個操作執行多個資料,如一次插入多條資料

fetchall()取出操作返回的所有的行

fetchone()取出一行

fetchmany(size=none)取出 size 行

close()關閉這個游標物件

-游標型別類名

說明cursor

預設型別,查詢返回list

dictcursor

與cursor不同的地方是,查詢返回dict,包括屬性名

sscursor

查詢不會返回所有的行,而是按需求返回

ssdictcursor

差別同前兩個

#cursor 查詢返回

(10000, 't恤男短袖', 28, decimal('89.00'), 300)

#dictcursor 查詢返回

#!python3

#-*- coding: utf-8 -*-

import pymysql.cursors # 好像import這個模組就可以了

config =

connection = pymysql.connect(**config) # 連線資料庫

try:

with connection.cursor() as cursor:

sql = 'select * from commodity where price > 100 order by price'

count = cursor.execute(sql) # 影響的行數

print(count)

result = cursor.fetchall() # 取出所有行

for i in result: # 列印結果

print(i)

connection.commit() # 提交事務

except:

connection.rollback() # 若出錯了,則回滾

finally:

connection.close()

Vim 使用筆記

set hlsearch set nohlsearch 搜尋後清除上次的加亮 nohl nohlsearch 拷貝 很有用的一句話,規定了格式選項,讓它換行不自動空格 set formatoptions tcrqn set fo r set noautoindent 再 shift insert 正...

xemacs使用筆記

xemacs使用筆記 xemacs emacs的下一代,由lucid原創 from debian參考手冊.由於不知道什麼時候刪掉了emacs的乙個重要檔案.每次都沒法安裝好.突然發現了xemacs,於是決定使用看看.本人還是菜鳥,僅供交流 我使用的ubuntu系統,所以就直接apt get inst...

TreeView使用筆記

treeview由節點構成,建樹通過對treeview.items屬性進行操作。items是乙個ttreenodes物件,這是乙個ttreenode集。一 針對ttreenodes,也就是 treeview.items,有這些屬性 1 count,節點個數。2 item index 通過index得...