Python数据库访问
来源:互联网 发布:淘宝客服容易做吗 编辑:程序博客网 时间:2024/06/14 05:24
本文介绍python操作数据库的方法,并以mysql与sqlite数据库为例,从一个csv文件中读入数据,插入到数据库中,再将数据库中的数据读出,保存到另一个csv文件。
介绍
python 社区制定了操作数据库的标准,所以,我们可以通过统一的接口访问不同的数据库,减少了我们的学习负担。
标准主要定义了两个对象,一个用于管理连接的Connection对象,另一个是用于执行查询的Cursor 对象。
Python 操作数据库的步骤
python 操作数据库的思路如下:
- 导入相应的数据库模块(import sqlite3, MySQLdb)
- 连接数据库(connect),返回一个Connection 对象
- 通过该对象的cursor()成员函数返回一个Cursor 对象
- 通过Cursor对象的execute()方法执行SQL语句
- 如果执行的是查询语句,则通过Cursor 对象的fetchone()等语句获取返回结果
- 关闭Cursor对象(close())
- 关闭Connection对象(close())
插入数据
对于sqlite 数据库,python 自带了sqlite3 模块,直接导入即可,对于mysql 数据库,则需要安装第三方模块Mysql-python 。安装完以后,在程序中导入模块即可。
#!/usr/bin/python#-*- coding: UTF-8 -*-import csvdef main(): DATABASE="sqlite3" #open databases if DATABASE == "sqlite3": import sqlite3 as db conn = db.connect("test") strInsert = "insert into stocks values(?, ?, ?)" else: import MySQLdb as db #conn = db.connect(host="localhost", user="root", passwd="passwd", db="test") conn = db.connect(host="localhost", db="test", read_default_file="~/.my.cnf") strInsert = "insert into stocks values(%s, %s, %s)" #get cursor object cur = conn.cursor() #read CSV file f = open("stock_data") stocks = [] for r in csv.reader(f): stocks.append(r) #create databses cur.execute("create table stocks( symbol text, shares integer, price real)") conn.commit() #execute statements of insert cur.executemany(strInsert, stocks) conn.commit() #close connection cur.close() conn.close()if __name__ == '__main__': main()
mysql数据库有两种连接方式(可能有很多种),一种是通过指定用户名和密码的方式,还有一种是指定read_default_file 参数,关于.my.cnf
文件,可以参考这里。
读取数据
下面执行查询语句,并将结果输出到一个CSV文件。
#!/usr/bin/python#-*- coding: UTF-8 -*-import csvdef main(): DATABASE="mysql" #open databases if DATABASE == "sqlite3": import sqlite3 as db conn = db.connect("test") else: import MySQLdb as db #conn = db.connect(host="localhost", user="root", passwd="passwd", db="test") conn = db.connect(host="localhost", db="stocks", read_default_file="~/.my.cnf") #crate cursor object cur = conn.cursor() f = open("output", 'w') w = csv.writer(f) #read data cur.execute("select * from stocks") #write data to csv file while True: row = cur.fetchone() if not row: break w.writerow(row) f.close() cur.close() conn.close()if __name__ == '__main__': main()
ubuntu安装MySQL-Python出现mysql_config not found错误
在ubuntu 下安装Mysql-Python时,可能出现"mysql_config not found"错误。提示:
EnvironmentError: mysql_config not found
Google后得知mysql_config是属于MySQL开发用的文件,而使用apt-get安装的MySQL是没有这个文件的,于是在包安装器里面寻找
sudo apt-get install libmysqld-devsudo apt-get install libmysqlclient-dev
这两个包安装后问题即可解决。
0 0
- python 访问 sqlite 数据库
- python 访问 sqlite 数据库
- python访问ZODB数据库
- python访问数据库
- Python数据库访问
- python访问MySQL数据库
- 树莓派上python访问数据库
- python访问MySQL数据库
- python访问mysql数据库
- python访问纯真IP数据库
- Python访问SQL Server数据库
- 【python小记】访问mysql数据库
- Python学习笔记 访问数据库
- Python(13):数据库访问
- Python访问SQL Server数据库
- python访问数据库(基于pymysql)
- pyodbc访问数据库(python ODBC访问数据库)
- pyodbc访问数据库(python ODBC访问数据库)
- 【字符串动态规划】最长回文字串+交替字符串
- Mapreduce工作原理及流程
- servlet判断是否异步/ajax 请求
- openstack
- JVM系列一:JVM内存组成及分配
- Python数据库访问
- 一维向量类
- [Android-cordova]获取3.4.0版的Cordova.jar包
- 爬虫scrapy包的依赖问题
- cocos2d-x3.0 XML解析
- Nvelocity模板引擎实现人员的增删改查(一)查询
- 利用java中的Socket实现私聊和公聊的聊天工具,控制台版本的。
- JVM系列 HotSpot VM GC 的种类
- Ubuntu下安装SSH