国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

新手實現Python mysql數據庫增刪改查

2019-11-08 03:05:03
字體:
來源:轉載
供稿:網友

先說點題外話,偷懶了一個多月了,今天正式回歸,今年要多學習點知識,一定要找個碼農的工作

首先要安裝python MySQLdb模塊,直接安裝就行了 32位:https://pypi.python.org/pypi/MySQL-python/1.2.5 64位:http://arquivos.victorjabur.com/python/modules/MySQL-python-1.2.3.win-amd64-py2.7.exe 2.連接測試import MySQLdb 分別為地址,用戶名,密碼,數據庫名稱db = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("SELECT VERSION()")data = cursor.fetchone()3測試查詢

import MySQLdbdb = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("SELECT * from usertable")row = int(cursor.rowcount)print rowdata=cursor.fetchall()for da in data: print 'userid=%s ,username=%s' %dadb.close()

結果 4 userid=2 ,username=shui userid=3 ,username=cao userid=4 ,username=zhao userid=5 ,username=zhao

4.測試添加,更新,刪除 默認是開啟事務的,必須提交后才能更新到數據庫中。

import MySQLdbdb = MySQLdb.connect("localhost","root","112233","jike" )cursor = db.cursor()cursor.execute("insert into usertable(userid,username) values(5,'zhao')")print cursor.rowcountcursor.execute("update usertable set username='cao' where userid=3")print cursor.rowcountcursor.execute("delete from usertable where userid=1")print cursor.rowcountdb.commit()db.close()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 个旧市| 文山县| 长春市| 左贡县| 水富县| 鹤壁市| 明星| 舟山市| 宜宾市| 景宁| 张家口市| 长海县| 汝州市| 牡丹江市| 鄂伦春自治旗| 连州市| 德州市| 阜宁县| 特克斯县| 右玉县| 怀远县| 英超| 金华市| 新平| 庆元县| 会同县| 芮城县| 花莲市| 额敏县| 新邵县| 前郭尔| 平安县| 大厂| 柳州市| 新巴尔虎左旗| 西林县| 博野县| 定南县| 双流县| 阳原县| 二手房|