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

首頁 > 編程 > Python > 正文

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

2020-01-04 17:52:07
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了python訪問mysql數據庫的實現方法,結合實例形式分析了兩種Python操作MySQL數據庫的相關技巧,需要的朋友可以參考下
 

本文實例講述了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程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南陵县| 尉犁县| 安西县| 互助| 稻城县| 弥勒县| 乌审旗| 开阳县| 衡阳县| 泸州市| 枣强县| 永城市| 利津县| 启东市| 邳州市| 疏附县| 云南省| 泊头市| 东台市| 临朐县| 迁西县| 长宁县| 郓城县| 台南县| 牟定县| 图木舒克市| 沙湾县| 神池县| 崇明县| 桐乡市| 卓资县| 汽车| 富宁县| 邹城市| 苏尼特右旗| 柳河县| 黔西县| 抚宁县| 承德县| 承德县| 花莲市|