神剑山庄资源网 Design By www.hcban.com
python调用mysql数据库通常通过mysqldb模块,简单说下如何调用
1.安装驱动
目前有两个MySQL的驱动,我们可以选择其中一个进行安装:
1. MySQL-python:是封装了MySQL C驱动的Python驱动;
2.mysql-connector-python:是MySQL官方的纯Python驱动。
这里使用MySQL-python驱动,即MySQLdb模块。
命令行安装
pip install python-mysql
或者在pycharm包中安装
源码安装方式
访问: http://www.lfd.uci.edu/~gohlke/pythonlibs/,下载MySQL_python-1.2.5-cp27-none-win_amd64.whl
将其拷贝到Python安装目录下的Scripts目录下,在文件位置打开cmd,执行pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
验证,python(command line)输入import MySQLdb,没报错,说明安装成功。
测试连接:
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb # 连接数据库 连接地址 账号 密码 数据库 数据库编码 db = MySQLdb.connect("localhost", "root", "123456", "test" , charset="utf8") # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取一条数据库。 data = cursor.fetchone() print "Database version : %s " % data # 关闭数据库连接 db.close()
示例1:
#!/usr/bin/python # coding=utf-8 import MySQLdb import os, sys import json class MysqlDb(object): def __init__(self): self.host = "127.0.0.1" @staticmethod def get_connect(): db = MySQLdb.connect(self.host , "mail_report", "mail_report", "mailawst", charset="utf8") return db def get_mysql_info(self,start_time,end_time): tmp = [] db = self.get_connect() sql = 'select send_time,mail_id,mail_addr,server_domain,server_ip,mail_status from real_mail_log where send_time > "%s" and send_time < "%s" limit 10;' % (start_time,end_time) cursor = db.cursor() cursor.execute(sql) values = cursor.fetchall() for i in values: data = {} data["send_time"] = str(i[0]) data["mail_id"] = str(i[1]) data["mail_addr"]= str(i[2]) data["server_domain"] = str(i[3]) data["server_ip"] = str(i[4]) data["mail_status"]= str(i[5].encode('utf8')) tmp.append(data) data = json.dumps(tmp,ensure_ascii=False) db.close() return data def main(): u = MysqlDb() print u.get_mysql_info('2017-05-01 00:00:02','2017-05-01 00:50:03') if __name__ == '__main__': main()
示例2:
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("localhost", "root", "123456", "test") # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL插入语句 ins_sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('yu', 'jie', 20, 'M', 8000)""" ins_sql1 = 'insert into employee(first_name, last_name, age, sex, income) values (%s, %s, %s, %s, %s)' # SQL查询语句 sel_sql = 'select * from employee where first_name = %s' # SQL更新语句 upd_sql = 'update employee set age = %s where sex = %s' # SQL删除语句 del_sql = 'delete from employee where first_name = %s' try: # 执行sql语句 # insert cursor.execute(ins_sql) cursor.execute(ins_sql1, ('xu', 'f', 20, 'M', 8000)) # select cursor.execute(sel_sql, ('yu',)) values = cursor.fetchall() print values # update cursor.execute(upd_sql, (24, 'M',)) # delete cursor.execute(del_sql, ('xu',)) # 提交到数据库执行 db.commit() except: # 发生错误时回滚 db.rollback() # 关闭数据库连接 db.close()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python MysqlDb模块安装及其使用详解的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]