Python3.6簡單操作Mysql數(shù)據(jù)庫
本文為大家分享了Python3.6操作Mysql數(shù)據(jù)庫的具體實例,供大家參考,具體內(nèi)容如下
安裝pymysql
參考https://github.com/PyMySQL/PyMySQL/
pip install pymsql
實例一
import pymysql
# 創(chuàng)建連接
# 參數(shù)依次對應服務器地址,用戶名,密碼,數(shù)據(jù)庫
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 執(zhí)行語句返回影響的行數(shù)
effect_row = cursor.execute("select * from course")
print(effect_row)
# 獲取所有數(shù)據(jù)
result = cursor.fetchall()
result = cursor.fetchone() # 獲取下一個數(shù)據(jù)
result = cursor.fetchone() # 獲取下一個數(shù)據(jù)(在上一個的基礎(chǔ)之上)
# cursor.scroll(-1, mode='relative') # 相對位置移動
# cursor.scroll(0,mode='absolute') # 絕對位置移動
# 提交,不然無法保存新建或者修改的數(shù)據(jù)
conn.commit()
# 關(guān)閉游標
cursor.close()
# 關(guān)閉連接
conn.close()
實例二
import pymysql
# 建立連接
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='demo')
# 創(chuàng)建游標
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 插入一條數(shù)據(jù) %s是占位符 占位符之間用逗號隔開
effect_row = cursor.execute("insert into course(cou_name,time) values(%s,%s)", ("Engilsh", 100))
print(effect_row)
conn.commit()
cursor.close()
conn.close()
實例三
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()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python3實現(xiàn)將本地JSON大數(shù)據(jù)文件寫入MySQL數(shù)據(jù)庫的方法
- Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- Python3實現(xiàn)的Mysql數(shù)據(jù)庫操作封裝類
- python3連接MySQL數(shù)據(jù)庫實例詳解
- 在python3環(huán)境下的Django中使用MySQL數(shù)據(jù)庫的實例
- python3操作mysql數(shù)據(jù)庫的方法
- python3.4用函數(shù)操作mysql5.7數(shù)據(jù)庫
- python3使用PyMysql連接mysql數(shù)據(jù)庫實例
- linux下python3連接mysql數(shù)據(jù)庫問題
- python3對接mysql數(shù)據(jù)庫實例詳解
相關(guān)文章
python用pip install時安裝失敗的一系列問題及解決方法
這篇文章主要介紹了python用pip install時安裝失敗的一系列問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Python調(diào)用Jar包的兩種方式小結(jié)
這篇文章主要介紹了Python調(diào)用Jar包的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Android模擬器無法啟動,報錯:Cannot set up guest memory ‘a(chǎn)ndroid_arm’ I
這篇文章主要介紹了Android模擬器無法啟動,報錯:Cannot set up guest memory ‘a(chǎn)ndroid_arm’ Invalid argument的解決方法,通過模擬器ram設(shè)置的調(diào)整予以解決,需要的朋友可以參考下2016-07-07
python處理emoji表情(兩個函數(shù)解決兩者之間的聯(lián)系)
這篇文章主要介紹了python處理emoji表情,主要通過兩個函數(shù)解決兩者之間的聯(lián)系,本文通過實例代碼給大家介紹的非常完美,對python emoji表情的相關(guān)知識感興趣的朋友一起看看吧2021-05-05
python人工智能tensorflow函數(shù)tf.assign使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

