本篇文章給大家分享的是有關(guān)Python3中怎么使用pymysql數(shù)據(jù)庫操作包,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首先,與數(shù)據(jù)庫建立connection和進(jìn)行操作的原理
(1)通過navicat premium創(chuàng)建testdataset數(shù)據(jù)庫和庫內(nèi)數(shù)據(jù)表test:
CREATE TABLE `test` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(2)在test數(shù)據(jù)表里添加數(shù)據(jù)項
(3)jupyter notebook里連接數(shù)據(jù)庫,并對數(shù)據(jù)庫進(jìn)行操作
import pandas as pd import datetime import pymysql #創(chuàng)建連接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='******', db='testdataset', charset='utf8')#passwd是本地mysql服務(wù)器密碼 conn #Output:<pymysql.connections.Connection at 0x11443e588> #創(chuàng)建游標(biāo) cursor = conn.cursor() cursor #Output:<pymysql.cursors.Cursor at 0x11443e2e8> #執(zhí)行SQL,并返回受影響行數(shù) effect_row = cursor.execute("select * from test") effect_row #Output:4 #獲取剩余結(jié)果的第一行數(shù)據(jù) r1=cursor.fetchone() r1 #Output:(1, '李明', 18) name='王天' age=17 sql="select name,age from test where name='%s' and age='%s'" % (name,age) row_count=cursor.execute(sql) row_1 = cursor.fetchone() print(row_count,row_1) #Output:1 ('王天', 17) conn.commit() cursor.close() conn.close()
以上就是Python3中怎么使用pymysql數(shù)據(jù)庫操作包,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:Python3中怎么使用pymysql數(shù)據(jù)庫操作包-創(chuàng)新互聯(lián)
本文路徑:http://jinyejixie.com/article34/dhddpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站建設(shè)、網(wǎng)站維護、品牌網(wǎng)站制作、搜索引擎優(yōu)化、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容