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

首頁 > 編程 > Python > 正文

python訪問mysql數據庫的實現方法(2則示例)

2019-11-25 16:59:39
字體:
來源:轉載
供稿:網友

本文實例講述了python訪問mysql數據庫的實現方法。分享給大家供大家參考,具體如下:

首先安裝與Python版本匹配的MySQLdb

示例一

import MySQLdbconn=MySQLdb.connect(user='root',passwd='123',db='example')cur=conn.cursor()cur.execute("select id,lastname,firstname, date_format(dob,'%Y-%m-%d %H-%i-%s'),phone from employee")##select username,password, date_format(reg_date,'%Y-%m-%d %H-%i-%s') as date from reg_userfor data in cur.fetchall():  print datacur.close()conn.commit()conn.close()

示例二

import MySQLdbconn = MySQLdb.connect(host='localhost',user='root',passwd='')cursor = conn.cursor()cursor.execute("create database python")cursor.execute('use python')cursor.execute("create table test(id int, content varchar(100))")#插入一條100條數據for i in range(1,100):   cursor.execute("insert into test values(%s,%s)",[i,'haha'])#獲取數據cursor.execute('select * from test')results = cursor.fetchall()for r in results   print rconn.close()

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莎车县| 邯郸县| 海晏县| 冀州市| 鹿泉市| 延津县| 青阳县| 威信县| 屏东县| 修武县| 桐柏县| 信宜市| 乐陵市| 揭阳市| 镇赉县| 东丰县| 阿克| 咸宁市| 永嘉县| 南宫市| 德清县| 普安县| 栖霞市| 织金县| 马关县| 鹤庆县| 鄯善县| 沙雅县| 河西区| 马边| 太谷县| 沙田区| 易门县| 县级市| 澄迈县| 汉阴县| 通道| 安宁市| 保德县| 分宜县| 鄯善县|