Python 之 MySQLdb
来源:互联网 发布:宣益民院士知乎 编辑:程序博客网 时间:2024/06/14 00:02
mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法。python操作数 据库需要安装一个第三方的模块,在http://mysql-python.sourceforge.net/有下载和文档。
#-*- encoding: gb2312 -*-import os, sys, stringimport MySQLdb# 连接数据库 try: conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')except Exception, e: print e sys.exit()# 获取cursor对象来进行操作cursor = conn.cursor()# 创建表sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"cursor.execute(sql)# 插入数据sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)try: cursor.execute(sql)except Exception, e: print esql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21)try: cursor.execute(sql)except Exception, e: print e# 插入多条sql = "insert into test1(name, age) values (%s, %s)" val = (("李四", 24), ("王五", 25), ("洪六", 26))try: cursor.executemany(sql, val)except Exception, e: print e#查询出数据sql = "select * from test1"cursor.execute(sql)alldata = cursor.fetchall()# 如果有数据返回,就循环输出, alldata是有个二维的列表if alldata: for rec in alldata: print rec[0], rec[1]cursor.close()conn.close()
- Python 之 MySQLdb
- python 之 MySQLdb
- python之MySQLdb模块
- python之mysql连接 MySQLdb
- Python库之MySQLdb介绍
- Python库之MySQLdb介绍
- Python模块学习之MySQLdb
- 关于python之MySQLdb的编码
- centos下安装Python之MySQLdb
- python模块之mysql模块MySQLdb
- Python学习笔记之连接MySQLdb
- python-MYSQLdb
- python mysqldb
- Python MySQLDB
- python mysqldb
- Python--MySQLdb
- Python - MySQLdb
- MySQLdb python
- iOS指南系列:如何解决奔溃问题-深入调试
- 程序员的十年
- exec函数用法总结
- linux 下解压命令大全
- vi 常用命令行
- Python 之 MySQLdb
- Server Application Unavailable出现的原因及解决方案集锦
- maven常用命令介绍
- 快速排序Linux下c 实现
- 得到字符串的区位码
- 【js特效】批量产品图左右切换效果
- 自己写了个table 固定table头部 使表可以滚动 注意稍不注意表头和表体就会出现不对齐的情况
- Android捕获Back键事件
- Redis关键点(持久化)