![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
# coding=utf-8
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
import adodbapi
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
class DBTestor:
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def __init__ (self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn = None
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def __del__ (self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
try :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn.close()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
except :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
pass
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def connectDB(self, connectString):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn = adodbapi.connect(connectString)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def closeDB(self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn.close()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def fielddict(self, cursor):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
dict = {}
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
i = 0
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
for field in cursor.description:
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
dict[field[0]] = i
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
i += 1
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
return dict
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def testCommand(self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
u " 测试执行SQL命令,及参数、事务 "
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor = self.conn.cursor()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ if exists (select * from sysobjects where id = object_id(N'Demo_Table') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
Drop Table Demo_Table;
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
CREATE TABLE Demo_Table (
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
ID int IDENTITY (1, 1) NOT NULL ,
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
Name varchar(50) NOT NULL Default('')
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
PRIMARY KEY CLUSTERED
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
(
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
[ID ]
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
); """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ INSERT INTO Demo_Table (Name) VALUES (?); """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql, ( " jame " ,))
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ INSERT INTO Demo_Table (Name) VALUES (?); """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql, ( " jame2 " ,))
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ SELECT @@Identity; """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
print " Inserted new record's ID = %s " % cursor.fetchone()[0]
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.close()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
# 默认对数据库进行修改后必须要提交事务,否则关闭数据库时会回滚
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn.commit()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def testQuery(self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
u " 测试查询功能,通过序号和字段名读取数据 "
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor = self.conn.cursor()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute( " SELECT * FROM authors " )
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
try :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
fields = self.fielddict(cursor)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
row = cursor.fetchone()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
while row != None:
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
print " %s: %s %s " % (row[0], row[fields[ ' au_fname ' ]], row[fields[ ' au_fname ' ]])
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
row = cursor.fetchone()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
finally :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.close()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
def testStoreProc(self):
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
u " 测试存储过程功能 "
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor = self.conn.cursor()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ if exists (select * from sysobjects where id = object_id(N'insert_data_demo') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
Drop Procedure insert_data_demo; """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ CREATE PROCEDURE INSERT_DATA_Demo
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
@Name varchar(50),
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
@ID int output
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
AS
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
INSERT INTO Demo_Table (Name) VALUES (@Name);
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
Select @ID = @@Identity; """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
(name, id) = cursor.callproc( " insert_data_demo " , ( " tom " , 0))
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
print " Inserted new record's ID = %i " % id
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
sql = """ SELECT * FROM Demo_Table; """
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.execute(sql)
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
print cursor.fetchall()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
cursor.close()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
self.conn.commit()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
if __name__ == " __main__ " :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test = DBTestor()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test.connectDB( " Provider=SQLOLEDB.1;Persist Security Info=True;Password=;User ID=sa;Initial Catalog=pubs;Data Source=. " )
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
try :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test.testQuery()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test.testCommand()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test.testStoreProc()
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
finally :
![](http://img.e-com-net.com/image/info8/20486711ecd34e998f0a93807eacc85f.gif)
test.closeDB()
1. 因为源代码有中文,所以要在第一行加上
2. adodbapi默认在Connection的__init__里面开启事务BeginTrans(),然后在close方法里面回滚事务RollbackTrans()。因此对数据库进行修改之后要调用Connection.commit()提交事务才会保存数据。这一点与ADO的默认行为不一致,需要注意。
3. cursor.fetch*()方法返回的是tuple,只能通过序号读取字段值,如果要通过字段名访问数据,需要将字段名映射为序号。测试代码里面的
感觉Python DB-API2.0接口使用是很简单和方便的,只不过接口功能稍弱了点,比如没有MoveFirst等功能,不能多次遍历单个结果记录集,而且不直接支持使用字段名访问数据,需要做转换。
1. PEP-0249, Python Database API Specification V 2.0
2. adodbapi 官方网站
3. 数据库连接字符串大全