Python操做MySQL数据库

Python DB API

  • PythonDB API:Python访问数据库的同一接口规范: python

  • http://www.python.org/dev/peps/pep-0249 mysql

  • PythonDB API包含的内容 ios

    • 数据库链接对象:connection sql

    • 数据库交互对象:cursor 数据库

    • 数据库异常类:exceptions windows

  • 使用Python DB API访问数据库流程 数组

    1. 开始 服务器

    2. 建立connection 网络

    3. 获取cursor 函数

    4. 增删改查

    5. 关闭cursor

    6. 关闭connections

    7. 结束


Python MySQl开发环境

MySQL-python

这里注意:尝试安装屡次出错

windows解决方案:easy_install MySQL-python    或者     pip install MySQL-python
Linux解决方案:apt-get install python-dev    或者     yum install python-devel

python3 能够安装使用pymysql,此处留坑:

参考文献:https://wiki.openstack.org/wiki/PyMySQL_evaluation



DB API-数据库链接对象-connectios

  • 链接对象:创建python客户端与数据库的网络链接

  • 建立方法:MySQLdb.Connect(参数)

conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='dbname')
    #    参数类型说明
    #    host:字符串,MySQL服务器地址
    #    port:数字,MySQL服务器端口号
    #    user:字符串,用户名
    #    passwd:
    #    db:字符串,数据库名称
    #    chaset:链接编码,中文用UTF-8

  • connections支持方法:

    • cursor()使用该链接建立并返回游标

    • commit()提交当前事物

    • rollback()回滚当前事物

    • close()关闭链接



DB API-数据库游标对象 cursor

  • 游标对象:用于执行查询和获取结果

  • cursor对象支持的方法:

    • execute(op[,args])    执行一个数据库查询和命令 ,将结果从数据库获取到客户端   select,update,insert    

    • fetchone()    取得结果集的下一行    featch*方法:移动rownumber(相似于数组下标),返回数据

    • fetchmany(size)    获取结果集的下几行

    • fetchall()    获取结果集的剩下的全部行

    • rowcount    最近一次execute返回去数据的行数或影响行数

    • close()    关闭游标对象



示例代码:

#-*- encoding: utf8-*-
import os, sys, string
import 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 e

sql = "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()


下面贴一下经常使用的函数:

而后,这个链接对象也提供了对事务操做的支持,标准的方法
commit() 提交
rollback() 回滚

cursor用来执行命令的方法:
callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
execute(self, query, args):执行单条sql语句,接收的参数为sql语句自己和使用的参数列表,返回值为受影响的行数
executemany(self, query, args):执行单挑sql语句,可是重复执行参数列表里的参数,返回值为受影响的行数
nextset(self):移动到下一个结果集

cursor用来接收返回值的方法:
fetchall(self):接收所有的返回结果行.
fetchmany(self, size=None):接收size条返回结果行.若是size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.
fetchone(self):返回一条结果行.
scroll(self, value, mode='relative'):移动指针到某一行.若是mode='relative',则表示从当前所在行移动value条,若是 mode='absolute',则表示从结果集的第一行移动value条.

相关文章
相关标签/搜索