如何將python的數(shù)據(jù)存儲(chǔ)到mysql數(shù)據(jù)庫(kù)中
一、最基本的準(zhǔn)備
1.1 本地安裝mysql,推薦安裝以下其中之一

1.2 安裝python軟件

二、建立連接
1.1打開(kāi)PyCharm編程軟件

1.2 打開(kāi)mysql軟件,否則連接不上

1.3 在python環(huán)境中下載PyMysql庫(kù)
pip install PyMysql
1.4 連接數(shù)據(jù)庫(kù)
import pymysql.cursors
# 連接數(shù)據(jù)庫(kù)
connect = pymysql.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='123456',
db='test_1',
charset='utf8'
)運(yùn)行上述代碼不報(bào)錯(cuò)則說(shuō)明連接成功,若不放心,我們可建表試試,如果在數(shù)據(jù)庫(kù)中能夠看到所建的表,說(shuō)明連接成功了。
二、創(chuàng)建表格
1.1 在python中創(chuàng)建表格
# 獲取游標(biāo)
cursor = connect.cursor()
#創(chuàng)建表格
sql = "CREATE TABLE test_1(id INTEGER PRIMARY KEY,name TEXT)"
try:
cursor.execute(sql)
connect.commit()
except:
print("表已存在")
print('成功創(chuàng)建表格')打印結(jié)果

1.2 在mysql中查看

如圖所示證明,python與mysql連接成功了
三、Python中MySQL數(shù)據(jù)庫(kù)操作
# 連接數(shù)據(jù)庫(kù)
connect = pymysql.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='123456',
db='note',
charset='utf8'
)
# 獲取游標(biāo)
cursor = connect.cursor()
#刪除表
sql = 'DROP TABLE IF EXISTS student'
cursor.execute(sql)
connect.commit()
print('如果存在表就刪除表格')
#創(chuàng)建表格
sql = "CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)"
try:
cursor.execute(sql)
connect.commit()
except:
print("表已存在")
print('成功創(chuàng)建表格')
# 插入數(shù)據(jù)
sql = "INSERT INTO student VALUES(%d,'%s')"
data = (1, 'student1')
cursor.execute(sql % data)
connect.commit()
print('成功插入', cursor.rowcount, '條數(shù)據(jù)')
# 修改數(shù)據(jù)
sql = "UPDATE student SET name = '%s' WHERE id = %d "
data = ('student2', 1)
cursor.execute(sql % data)
connect.commit()
print('成功修改', cursor.rowcount, '條數(shù)據(jù)')
# 查詢(xún)數(shù)據(jù)
sql = "SELECT * FROM student WHERE id=%d"
data = (1,)
cursor.execute(sql % data)
for row in cursor.fetchall():
print("%s" % str(row))
print('共查找出', cursor.rowcount, '條數(shù)據(jù)')
# 刪除數(shù)據(jù)
sql = "DELETE FROM student WHERE id = %d LIMIT %d"
data = (1, 1)
cursor.execute(sql % data)
connect.commit()
print('成功刪除', cursor.rowcount, '條數(shù)據(jù)')
# 事務(wù)處理
sql_1 = "UPDATE student SET name = name + '1' WHERE id = 1 "
try:
cursor.execute(sql_1)
except Exception as e:
connect.rollback() # 事務(wù)回滾
print('事務(wù)處理失敗', e)
else:
connect.commit() # 事務(wù)提交
print('事務(wù)處理成功', cursor.rowcount)
# 關(guān)閉連接
cursor.close()
connect.close()四、參數(shù)說(shuō)明
pymysql.Connect()參數(shù)說(shuō)明
| 參數(shù) | 說(shuō)明 |
|---|---|
| host(str) | MySQL服務(wù)器地址 |
| port(int) | MySQL服務(wù)器端口號(hào) |
| user(str) | 用戶(hù)名 |
| passwd(str) | 密碼 |
| db(str) | 數(shù)據(jù)庫(kù)名稱(chēng) |
| charset(str) | 連接編碼 |
connection對(duì)象支持的方法
| 方法 | 說(shuō)明 |
|---|---|
| cursor() | 使用該連接創(chuàng)建并返回游標(biāo) |
| commint() | 提交當(dāng)前事務(wù) |
| rollback() | 回滾當(dāng)前事務(wù) |
| close() | 關(guān)閉連接 |
cursor對(duì)象支持的方法
| 方法 | 說(shuō)明 |
|---|---|
| execute(op) | 執(zhí)行一個(gè)數(shù)據(jù)庫(kù)的查詢(xún)命令 |
| fetchone() | 取得結(jié)果集的下一行 |
| fetchmany(size) | 獲取結(jié)果集的下幾行 |
| fetchall() | 獲取結(jié)果集中的所有行 |
| rowcount() | 返回?cái)?shù)據(jù)條數(shù)或影響條數(shù) |
| close() | 關(guān)閉游標(biāo)對(duì)象 |
總結(jié)
到此這篇關(guān)于如何將python的數(shù)據(jù)存儲(chǔ)到mysql數(shù)據(jù)庫(kù)中的文章就介紹到這了,更多相關(guān)python數(shù)據(jù)存儲(chǔ)到mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python pywinauto實(shí)現(xiàn)Windows圖形界面自動(dòng)化控制詳解
pywinauto 作為一款專(zhuān)門(mén)面向 Windows GUI 自動(dòng)化的 Python 庫(kù),可以像人一樣“看懂”界面結(jié)構(gòu),精準(zhǔn)控制窗口、按鈕、菜單、文本框等控件,下面我們就來(lái)看看它的具體使用方法吧2025-10-10
Python簡(jiǎn)單網(wǎng)絡(luò)編程示例【客戶(hù)端與服務(wù)端】
這篇文章主要介紹了Python簡(jiǎn)單網(wǎng)絡(luò)編程,詳細(xì)介紹了客戶(hù)端與服務(wù)端的具體實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
python批量導(dǎo)出導(dǎo)入MySQL用戶(hù)的方法
這篇文章主要介紹了2013-11-11
使用Python手搓一個(gè)Windows注冊(cè)表清理器
這篇文章主要為大家詳細(xì)介紹了如何使用Python手搓一個(gè)Windows注冊(cè)表清理器,本文將從開(kāi)發(fā)到?EXE?打包進(jìn)行全面講解,希望對(duì)大家有所幫助2025-12-12
python 實(shí)現(xiàn)dict轉(zhuǎn)json并保存文件
今天小編就為大家分享一篇python 實(shí)現(xiàn)dict轉(zhuǎn)json并保存文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

