Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù)
本文為大家分享了Python3.6操作Mysql數(shù)據(jù)庫(kù)的具體實(shí)例,供大家參考,具體內(nèi)容如下
安裝pymysql
參考https://github.com/PyMySQL/PyMySQL/
pip install pymsql
實(shí)例一
import pymysql
# 創(chuàng)建連接
# 參數(shù)依次對(duì)應(yīng)服務(wù)器地址,用戶名,密碼,數(shù)據(jù)庫(kù)
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執(zhí)行語(yǔ)句返回影響的行數(shù)
effect_row = cursor.execute("select * from course")
print(effect_row)
# 獲取所有數(shù)據(jù)
result = cursor.fetchall()
result = cursor.fetchone() # 獲取下一個(gè)數(shù)據(jù)
result = cursor.fetchone() # 獲取下一個(gè)數(shù)據(jù)(在上一個(gè)的基礎(chǔ)之上)
# cursor.scroll(-1, mode='relative') # 相對(duì)位置移動(dòng)
# cursor.scroll(0,mode='absolute') # 絕對(duì)位置移動(dòng)
# 提交,不然無(wú)法保存新建或者修改的數(shù)據(jù)
conn.commit()
# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉連接
conn.close()
實(shí)例二
import pymysql
# 建立連接
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 插入一條數(shù)據(jù) %s是占位符 占位符之間用逗號(hào)隔開
effect_row = cursor.execute("insert into course(cou_name,time) values(%s,%s)", ("Engilsh", 100))
print(effect_row)
conn.commit()
cursor.close()
conn.close()
實(shí)例三
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3實(shí)現(xiàn)將本地JSON大數(shù)據(jù)文件寫入MySQL數(shù)據(jù)庫(kù)的方法
- Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫(kù)操作示例
- Python3實(shí)現(xiàn)的Mysql數(shù)據(jù)庫(kù)操作封裝類
- python3連接MySQL數(shù)據(jù)庫(kù)實(shí)例詳解
- 在python3環(huán)境下的Django中使用MySQL數(shù)據(jù)庫(kù)的實(shí)例
- python3操作mysql數(shù)據(jù)庫(kù)的方法
- python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫(kù)
- python3使用PyMysql連接mysql數(shù)據(jù)庫(kù)實(shí)例
- linux下python3連接mysql數(shù)據(jù)庫(kù)問(wèn)題
- python3對(duì)接mysql數(shù)據(jù)庫(kù)實(shí)例詳解
相關(guān)文章
python用pip install時(shí)安裝失敗的一系列問(wèn)題及解決方法
這篇文章主要介紹了python用pip install時(shí)安裝失敗的一系列問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
使用PyTorch實(shí)現(xiàn)MNIST手寫體識(shí)別代碼
今天小編就為大家分享一篇使用PyTorch實(shí)現(xiàn)MNIST手寫體識(shí)別代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python調(diào)用Jar包的兩種方式小結(jié)
這篇文章主要介紹了Python調(diào)用Jar包的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Android模擬器無(wú)法啟動(dòng),報(bào)錯(cuò):Cannot set up guest memory ‘a(chǎn)ndroid_arm’ I
這篇文章主要介紹了Android模擬器無(wú)法啟動(dòng),報(bào)錯(cuò):Cannot set up guest memory ‘a(chǎn)ndroid_arm’ Invalid argument的解決方法,通過(guò)模擬器ram設(shè)置的調(diào)整予以解決,需要的朋友可以參考下2016-07-07
Linux上Miniconda的安裝的實(shí)現(xiàn)步驟
Miniconda是一個(gè)輕量級(jí)、免費(fèi)且開源的跨平臺(tái)軟件包管理系統(tǒng),本文主要介紹了Linux上Miniconda的安裝的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
python處理emoji表情(兩個(gè)函數(shù)解決兩者之間的聯(lián)系)
這篇文章主要介紹了python處理emoji表情,主要通過(guò)兩個(gè)函數(shù)解決兩者之間的聯(lián)系,本文通過(guò)實(shí)例代碼給大家介紹的非常完美,對(duì)python emoji表情的相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05
python人工智能tensorflow函數(shù)tf.assign使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

