安裝 pymysql
函式說明
connect
建立鏈結
cursor
建立乙個游標
commit
事務提交,如果沒有設為自動提交,則每次操作後必須提交事務,否則操作無效。
rollback
操作出錯時,可以用這個函式回滾到執行事務之前
close
關閉連線
conn = pymysql.connect(host="localhost",
db="test",
user="root",
password="",
port=3306,
charset="utf8"
)
向資料庫中新增一條資訊:
cursor = conn.cursor()
sql_insert = "insert into book(name,price) values ('hhh', 7)"
try:
cursor.execute(sql_insert)
conn.commit()
except exception as e:
conn.rollback()
conn.close()
表結構如下:
connect引數:
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=, # 選擇 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 # 當客戶有多個網路介面,指定乙個連線到主機
完整**:
import pymysql
def getconnection():
conn = pymysql.connect(host="localhost",
db="test",
user="root",
password="",
port=3306,
charset="utf8"
)return conn
def insert():
conn = getconnection()
cursor = conn.cursor()
sql_insert = "insert into book(name,price) values ('hhh', 7)"
try:
cursor.execute(sql_insert)
conn.commit()
except exception as e:
conn.rollback()
conn.close()
def delete():
conn = getconnection()
cursor = conn.cursor()
sql_delete = "delete from book where name = %s"
try:
cursor.execute(sql_delete, ("hhh", ))
conn.commit()
except exception as e:
conn.rollback()
conn.close()
def update():
coon = getconnection()
cursor = coon.cursor()
update_delete = "update book set name = %s where name = 'test2'"
try:
cursor.execute(update_delete, ("update",))
coon.commit()
except exception as e:
coon.rollback()
coon.close()
def select():
coon = getconnection()
cursor = coon.cursor()
sql = "select * from book where name = %s"
try:
count = cursor.execute(sql, ("test",))
except exception as e:
coon.rollback()
print(count)
#insert()
#select()
#delete()
update()
python操作mysql查詢資料
首先需要連線資料庫,然後才查詢出資料。例如下表名字為 sinauser iduse id use name11 db12 2db233 db3class database def init self self.conn mysqldb.connect 連線資料庫 host 連線你要取出資料庫的ip,...
python操作MySQL資料庫
堅持每天學一點,每天積累一點點,作為自己每天的業餘收穫,這個文章是我在吃飯的期間寫的,利用自己零散的時間學了一下python操作mysql,所以整理一下。我採用的是mysqldb操作的mysql資料庫。先來乙個簡單的例子吧 import mysqldb try conn mysqldb.connec...
Python操作Mysql資料庫
coding utf8 author yangjing import mysqldb 查詢。def select host user root password port 3306,db sql connect mysqldb.connect host host,user user,passwd p...