摘要:安裝和的安裝圖解破解可以看下這個文章鏈接的增刪改查通過提供的模塊實現(xiàn)對數(shù)據(jù)庫的操作,這個地方注意使用的是,的話使用模塊安裝模塊創(chuàng)建連接創(chuàng)建游標修改執(zhí)行,并返回受影響行數(shù)添加執(zhí)行,并返回受影響行數(shù)查詢刪除執(zhí)行,并
安裝Mysql和Navicat for MySQL
mysql的安裝圖解https://jingyan.baidu.com/art...
navicat for mysql破解可以看下這個文章https://www.cnblogs.com/da199...
通過Python提供的pymysql模塊實現(xiàn)對mysql數(shù)據(jù)庫的操作,這個地方注意python3.x使用的是pymysql,python2.x的話使用mysqldb模塊
安裝pymysql模塊:pip install PyMySQL
import pymysql # 創(chuàng)建連接 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="student") # 創(chuàng)建游標 cursor = conn.cursor() # 修改----執(zhí)行SQL,并返回受影響行數(shù) # effect_row = cursor.execute("update user set name=%s,pwd=%s where id=%s", ("aaa","bb", 1)) # 添加----執(zhí)行SQL,并返回受影響行數(shù) # cursor.execute("insert into user (name, pwd) values (%s,%s)", ("lidao","aaa")) # 查詢---- cursor.execute("select * from user") stus = cursor.fetchall() for stu in stus: print("id:%d; name: %s; pwd: %s; " %(stu[0], stu[1], stu[2])) # 刪除---執(zhí)行SQL,并返回受影響行數(shù) cursor.execute("delete from user where id=%s", (2)) # 提交,不然無法保存新建或者修改的數(shù)據(jù) conn.commit() #如果不加這個就手動添加autocommit=True 自動提交 #db=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="123456",db="school",charset="utf8",autocommit=True) # 關閉游標 cursor.close() # 關閉連接 conn.close()自己封裝helper類
import pymysql class dbhelper(): def __init__(self,host,port,user,passwd,db,charset="utf8"): self.host=host self.port=port self.user=user self.passwd=passwd self.db=db self.charset=charset #創(chuàng)建一個鏈接 def connection(self): #1. 創(chuàng)建連接 self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db,charset=self.charset) #2. 創(chuàng)建游標 self.cur = self.conn.cursor() #關閉鏈接 def closeconnection(self): self.cur.close() self.conn.close() #查詢一條數(shù)據(jù) def getonedata(self,sql): try: self.connection() self.cur.execute(sql) result=self.cur.fetchone() self.closeconnection() except Exception: print(Exception) return result #查詢多條數(shù)據(jù) def getalldata(self,sql): try: self.connection() self.cur.execute(sql) result=self.cur.fetchall() self.closeconnection() except Exception: print(Exception) return result #添加/修改/刪除 def executedata(self,sql): try: self.connection() self.cur.execute(sql) self.conn.commit() self.closeconnection() except Exception: print(Exception)
封裝好了以后,后續(xù)用到mysql的操作的地方都可以直接使用,栗子如下:
from mysqlhelper import * db=dbhelper(host="127.0.0.1", port=3306, user="root", passwd="123456", db="school",charset="utf8") result=db.getalldata("select * from class") print(result)
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://www.ezyhdfw.cn/yun/41117.html
摘要:現(xiàn)在,對接文件已經(jīng)到位接下來測試數(shù)據(jù)庫,如果還沒安裝或者安裝了還沒配置,移步數(shù)據(jù)庫配置,完成數(shù)據(jù)庫的安裝和配置或者在官方下載,安裝官方手冊安裝。 ArthurSlog SLog-3 Year·1 Guangzhou·China July 9th 2018 showImg(https://segmentfault.com/img/remote/1460000016093266?w=2...
摘要:本次分享將介紹如何在中使用庫實現(xiàn)數(shù)據(jù)庫的讀寫。提供了工具包及對象關系映射工具,使用許可證發(fā)行。模塊實現(xiàn)了與不同數(shù)據(jù)庫的連接,而模塊則使得能夠操作數(shù)據(jù)庫。 ??本次分享將介紹如何在Python中使用Pandas庫實現(xiàn)MySQL數(shù)據(jù)庫的讀寫。首先我們需要了解點ORM方面的知識。 ORM技術 ??對象關系映射技術,即ORM(Object-Relational Mapping)技術,指的是把關...
摘要:用選擇要操作的數(shù)據(jù)庫,然后通過指針就可以操作這個數(shù)據(jù)庫了。這樣就在這個數(shù)據(jù)庫中創(chuàng)建了一個名為的表這是查看表的方式。樹欲靜而風不止,小偷在行動。所以,要特別提醒諸位注意。 通過python操作數(shù)據(jù)庫的行為,除了能夠完成前面兩講中的操作之外(當然,那是比較常用的),其實任何對數(shù)據(jù)庫進行的操作,都能夠通過python-mysqldb來實現(xiàn)。 建立數(shù)據(jù)庫 在《用python操作數(shù)據(jù)庫(1)...
閱讀 3272·2021-11-10 11:36
閱讀 3221·2021-11-02 14:39
閱讀 1820·2021-09-26 10:11
閱讀 5162·2021-09-22 15:57
閱讀 1762·2021-09-09 11:36
閱讀 2125·2019-08-30 12:56
閱讀 3554·2019-08-30 11:17
閱讀 1772·2019-08-29 17:17