python3操作pymsql模組

2021-09-03 00:09:30 字數 4919 閱讀 4091

pymysql是python中操作mysql的模組。

pip3 install pymysql
也可以使用pycharm這個ide工具來安裝pymysql這個模組。

**如下:

#!/usr/bin/env python

#_*_coding:utf-8_*_

import pymysql

#建立連線

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

#建立游標

cursor=conn.cursor()

#執行mysql語句,並返回執行的結果

res=cursor.execute("select name from db1")

#列印執行的結果

print(res)

#把要執行的語句提交,否則無法儲存新建或者修改資料

conn.commit()

#關閉游標

cursor.close()

#關閉連線

conn.close()

執行結果為:

4
因為從db1.db1這張表中檢索到四條資料,所以返回的值為4

需要注意的是,查詢過程中存在中文的話,連線需要新增**"charset='utf-8'"**,否則中文會顯示亂碼
**如下:

#!/usr/bin/env python

#_*_coding:utf-8_*_

import pymysql

#建立連線

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

#建立游標

cursor=conn.cursor()

#執行mysql語句

cursor.execute("select name from db1")

#獲取所有的執行結果

res=cursor.fetchall()

#列印獲取到的執行結果

print(res)

#提交要執行的mysql指令

conn.commit()

#關閉游標

cursor.close()

#關閉連線

conn.close()

執行結果為:

(('xiaoming',), ('xiaobing',), ('xiaoyong',), ('xiaojian',))
可以看到,返回的結果是乙個元組型別的資料.

還可以在建立游標的時候,使用選項來指定返回的結果為哪種資料型別:

cursor=conn.cursor(cursor=pymysql.cursors.dictcursor)
使用這個指令可以把返回的結果變成字典型別。

在獲取執行的結果時,可以指定獲取的結果的條數,可以使用的選項如下:

fetchone()          取得檢索結果的一條資料

fetchmany(n) 取得檢索結果的n條資料

fetchall() 取得檢索結果的所有資料

需要注意的是,與讀取檔案時的指標類似.如果在同一段**中,先使用fetchone()獲取檢索結果的第一條資料,

然後再使用fetchmany(2)的話,指標會在檢索結果的當前位置向後讀取執行結果,而不會從頭開始重新讀取檢索的結果.

**如下:

#!/usr/bin/env python

#_*_coding:utf-8_*_

import pymysql

#建立連線

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

#建立游標

cursor=conn.cursor(cursor=pymysql.cursors.dictcursor)

#執行mysql語句,並返回執行的結果

cursor.execute("select name from db1")

#取執行結果的第一條資料,並列印

res1=cursor.fetchone()

print("this is first result:",res1)

#從剩下的執行結果中再取兩條資料,並列印

res2=cursor.fetchmany(2)

print("this is second result:",res2)

#再從剩下的資料中取所有的資料,並列印

res3=cursor.getchall()

print("this is third result:",res3)

#提交要執行的命令

conn.commit()

#關閉游標

cursor.close()

#關閉連線

conn.close()

執行結果如下:

this is first result: 

this is second result: [, ]

this is third result:

第一次取第一行的檢索結果,第二次取兩行的時候,第三次取剩下的所有的結果.

因為資料表中只有四條記錄,而第一次已經取走一行了,

第二次從第一次取得的結果又向後繼續取兩個值,所以最後取所有的值時,只剩下乙個,所以第三次取得乙個值。

在建立游標的時候,指定了返回的資料型別為字典型別,所以返回的結果是字典資料型別。

在使用fetch時,按照順序進行取得資料,可以使用cursor.scroll(num,mode)來移動游標位置

mode指定位置,是相對當前位置,還是絕對位置

num指定移動的位數,正數向後移動,負數向前移動

例如:

cursor.scroll(1,mode="relative")        #相對於當前的指標位置取乙個值

cursor.scroll(2,mode="absolute") #在當前的絕對位置取乙個值

"relative"與"absolute"的區別,看下面兩段**:

第一段**:

#!/usr/bin/env python

#_*_coding:utf-8_*_

import pymysql

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

cursor=conn.cursor()

cursor.execute("select * from db1")

cursor.fetchone()

cursor.scroll(1,mode="relative")

print(cursor.fetchone())

conn.commit()

cursor.close()

conn.close()

執行結果如下:

(3, 'xiaoyong', 'xiaoyong')
第二段**:

#!/usr/bin/env python

#_*_coding:utf-8_*_

import pymysql

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

cursor=conn.cursor()

cursor.execute("select * from db1")

cursor.fetchone()

cursor.scroll(1,mode="absolute")

print(cursor.fetchone())

conn.commit()

cursor.close()

conn.close()

執行結果如下:

(2, 'xiaobing', 'xiaobing')
**如下:

#!/usr/bin/env python

# _*_coding:utf-8_*_

import pymysql

conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="",db="db1")

cursor=conn.cursor()

cursor.execute('insert into db1(name,password) values("xiaofei","xiaofei")')

conn.commit()

cursor.close()

conn.close()

res=cursor.lastrowid

print(res)

執行結果為:

5
資料表db1中本來有四條資料,現在新增一條,其id為自增型別,所以返回結果為5

python3的檔案操作

python的檔案操作和php的檔案很類似 file物件使用 open 函式來建立,open的引數 r表示讀,w寫資料,在寫之前先清空檔案內容,a開啟並附加內容,開啟檔案之後記得關閉 下表列出了 file 物件常用的函式 序號方法及描述 file.close 關閉檔案。關閉後檔案不能再進行讀寫操作。...

python3進行excel操作

只要有需求,就會找出解決問題的方法 pip install xlrd 讀取 pip install xlwt 寫入 首先先初始化 import xlwt excel xlwt.workbook encoding utf 8 建立excel sheet excel.add sheet member 建...

Python 3 操作json 檔案

json 是一種輕量級的資料交換格式。易於人閱讀和編寫,同時也易於機器解析和生成。一般表現形式是乙個無序的 鍵值對 的集合。資料 官方文件 python操作json的其他方式 1.將字串轉化為json串 dumps import json a foo bar result json.dumps a ...