Python与数据库

  本来想用下MySQLdb这个包来连接数据库,没想到这包还有没出适合Python3.5.1的版本,安装过程中提示:  

C:UsersMelody>pip install mysqldb
Collecting mysqldb
  Could not find a version that satisfies the requirement mysqldb (from versions: )
No matching distribution found for mysqldb

    只好转投PyMySQL,使用下来也很好用,文档例子在https://pypi.python.org/pypi/PyMySQL

    顺便把DB API总结下:

    原文档地址:https://www.python.org/dev/peps/pep-0249/

    Connection 支持方法

.close()  #关闭连接
.commit()  #提交当前事务
.rollback ()  #回滚当前事务
.cursor ()  #使用该连接创建并返回cursor

   Cursor支持方法:

.callproc ( procname [, parameters ] )   #执行存储过程
.close ()   #关闭对象
.execute ( operation [, parameters ])   #执行一个数据库语句
.executemany ( operation , seq_of_parameters )    #使用参数执行多条数据库语句 
.fetchone ()   #获取结果集的下一行
.fetchmany ([ size=cursor.arraysize ])   #获取结果集的下几行
.fetchall ()   #获取结果集剩下所有行

    Cursor属性:

.description 
.rowcount   #最近一次执行execute返回数据行数或影响行数

  

原文地址:https://www.cnblogs.com/Hebe/p/5117203.html