Python操作MySQL
2014-04-10 17:52
281 查看
1. 如果操作系统是ubuntu,则直接sudo apt-get install python-mysqldb,安装完成之后可以在python解释器中测试一下,输入python如下代码:import MySQLdb,如果不报错,则证明安装成功。
2. 用python建立数据库
3. 创建表,插入更新一条数据,插入多条数据
4. 查询数据,删除数据
说明:charset属性根据需要自行指定,否则中文出现乱码现象。
参考文献:
[1] MySQLdb User's Guide: http://mysql-python.sourceforge.net/MySQLdb.html
[2] 用Python操作Mysql: http://www.iteye.com/topic/573092
[3] python操作MySQL数据库: http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html
[4] Python操作mysql(增删改查): http://www.oschina.net/code/snippet_1382328_27194
2. 用python建立数据库
import MySQLdb try: # 建立和数据库系统的连接 conn = MySQLdb.connect(host='localhost', user='root', passwd='root', port=3306) # 获取操作游标 cursor = conn.cursor() # 执行SQL,创建一个数据库 cursor.execute('create database python') # 关闭连接,释放资源 cursor.close() conn.close() except MySQLdb.Error, e: print 'Mysql Error %d: %s' % (e.args[0], e.args[1])
3. 创建表,插入更新一条数据,插入多条数据
import MySQLdb try: # 建立和数据库系统的连接 conn = MySQLdb.connect(host='localhost', user='root', passwd='root', port=3306) # 获取操作游标 cursor = conn.cursor() # 执行SQL,创建一个数据库 cursor.execute('create database if not exists python') # 选择数据库 conn.select_db('python') # 执行SQL,创建一个数据表 cursor.execute('create table test(id int, info varchar(100))') value = [1, 'hi python'] # 插入一条记录 cursor.execute('insert into test values(%s, %s)', value) values = [] # 生成插入参数值 for i in range(20): values.append((i, 'Hello mysqldb, I am recoder' + str(i))) # 插入多条记录 cursor.executemany('insert into test values(%s, %s)', values) # 更新数据 cursor.execute('update test set info='Hello mysqldb, I am pythoner' where id=3') # 提交事务 conn.commit() # 关闭连接,释放资源 cursor.close() conn.close() except MySQLdb.Error, e: print 'Mysql Error %d: %s' % (e.args[0], e.args[1])
4. 查询数据,删除数据
import MySQLdb try: conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='python', port=3306) cursor = conn.cursor() count = cursor.execute('select * from test') print '总共有%s条记录' % count # 获取一条记录,每条记录做为一个元组返回 print '只获取一条记录:' result = cursor.fetchone() print result print 'ID: %s info: %s' % result # 获取五条记录,注意由于之前执行了fetchone(),所以游标已经指到第二条记录,即从第二条开始的所有记录 print '只获取五条记录:' results = cursor.fetchmany(5) for r in results: print r # 删除一条记录 cursor.execute('delete from test where id=0') print '获取所有结果:' # 重置游标位置,0为偏移量,mode=absolute | relative,默认为relative cursor.scroll(0, mode='absolute') # 获取所有结果 results = cursor.fetchall() for r in results: print r conn.commit() cursor.close() conn.close() except MySQLdb.Error, e: print 'Mysql Error %d: %s' % (e.args[0], e.args[1])
说明:charset属性根据需要自行指定,否则中文出现乱码现象。
参考文献:
[1] MySQLdb User's Guide: http://mysql-python.sourceforge.net/MySQLdb.html
[2] 用Python操作Mysql: http://www.iteye.com/topic/573092
[3] python操作MySQL数据库: http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html
[4] Python操作mysql(增删改查): http://www.oschina.net/code/snippet_1382328_27194
相关文章推荐
- python操作mysql方法和常见问题
- python操作MySQL的几点经验之谈
- 操作 Python爬虫数据存储MySQL【3】爬取信息
- Python 操作mysql
- python操作excel导入数据到mysql
- python操作mysql范例(增删查改)
- python文件读写并使用mysql批量插入示例分享(python操作mysql)
- Mysql入门笔记(0):python操作mysql数据库(增,删,改,查)
- Python的MySQL操作
- MySQL-python操作数据库
- 使用Python对MySQL进行相关操作
- Python全栈•数据库(二)⁃MySQL库操作
- [Python]操作Mysql
- python 操作MySQL指南
- Python操作MySQL乱码问题解决
- python操作mysql
- python,操作mysql
- 使用python操作MySQL
- 何用Python操作MySQL
- Python中操作mysql的pymysql模块详解