如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中
一、最基本的準備
1.1 本地安裝mysql,推薦安裝以下其中之一
1.2 安裝python軟件
二、建立連接
1.1打開PyCharm編程軟件
1.2 打開mysql軟件,否則連接不上
1.3 在python環(huán)境中下載PyMysql庫
pip install PyMysql
1.4 連接數(shù)據(jù)庫
import pymysql.cursors # 連接數(shù)據(jù)庫 connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='test_1', charset='utf8' )
運行上述代碼不報錯則說明連接成功,若不放心,我們可建表試試,如果在數(shù)據(jù)庫中能夠看到所建的表,說明連接成功了。
二、創(chuàng)建表格
1.1 在python中創(chuàng)建表格
# 獲取游標 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ù)庫操作
# 連接數(shù)據(jù)庫 connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='note', charset='utf8' ) # 獲取游標 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ù)') # 查詢數(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ù)說明
pymysql.Connect()參數(shù)說明
參數(shù) | 說明 |
---|---|
host(str) | MySQL服務(wù)器地址 |
port(int) | MySQL服務(wù)器端口號 |
user(str) | 用戶名 |
passwd(str) | 密碼 |
db(str) | 數(shù)據(jù)庫名稱 |
charset(str) | 連接編碼 |
connection對象支持的方法
方法 | 說明 |
---|---|
cursor() | 使用該連接創(chuàng)建并返回游標 |
commint() | 提交當前事務(wù) |
rollback() | 回滾當前事務(wù) |
close() | 關(guān)閉連接 |
cursor對象支持的方法
方法 | 說明 |
---|---|
execute(op) | 執(zhí)行一個數(shù)據(jù)庫的查詢命令 |
fetchone() | 取得結(jié)果集的下一行 |
fetchmany(size) | 獲取結(jié)果集的下幾行 |
fetchall() | 獲取結(jié)果集中的所有行 |
rowcount() | 返回數(shù)據(jù)條數(shù)或影響條數(shù) |
close() | 關(guān)閉游標對象 |
總結(jié)
到此這篇關(guān)于如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中的文章就介紹到這了,更多相關(guān)python數(shù)據(jù)存儲到mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch 實現(xiàn)計算 kl散度 F.kl_div()
這篇文章主要介紹了pytorch 實現(xiàn)計算 kl散度 F.kl_div(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Tensorflow加載與預處理數(shù)據(jù)詳解實現(xiàn)方法
讀取大型數(shù)據(jù)集并對其進行有效預處理可能對其他深度學習庫來說很難實現(xiàn),但是TensorFlow借助Data API很容易實現(xiàn):只需創(chuàng)建一個數(shù)據(jù)集對象,并告訴它如何從何處獲取數(shù)據(jù)以及如何對其進行轉(zhuǎn)換2022-11-11tensorflow實現(xiàn)對張量數(shù)據(jù)的切片操作方式
今天小編就為大家分享一篇tensorflow實現(xiàn)對張量數(shù)據(jù)的切片操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python使用execjs模塊運行JavaScript代碼
在編程中,Python和JavaScript是兩種使用廣泛的編程語言,本文將深入探索如何通過execjs模塊在Python中運行JavaScript代碼,有需要的可以參考一下2025-03-03Pytorch矩陣乘法(torch.mul() 、 torch.mm() 和torch.m
在深度學習和神經(jīng)網(wǎng)絡(luò)的世界里,矩陣乘法是一項至關(guān)重要的操作,本文主要介紹了Pytorch矩陣乘法,包含了torch.mul() 、 torch.mm() 和torch.matmul()的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-03-03pytorch+sklearn實現(xiàn)數(shù)據(jù)加載的流程
這篇文章主要介紹了pytorch+sklearn實現(xiàn)數(shù)據(jù)加載,之前在訓練網(wǎng)絡(luò)的時候加載數(shù)據(jù)都是稀里糊涂的放進去的,也沒有理清楚里面的流程,今天整理一下,加深理解,也方便以后查閱,需要的朋友可以參考下2022-11-11