(作者:玛瑙河,转载请注明作者或出处,Python & ADO) 

在Windows系统中,用Python脚本通过ADO来操作数据库是相当有效率的,你犯不着去找各种各样的数据库模块,因为ADO已经为你准备好了一切。有时候一个不太复杂却用简单的SQL查询不能解决的问题,用Python脚本就能很方便的解决,犯不着打开某些强大的IDE并新建一个工程(杀鸡焉用牛刀!),你所需要的工具,仅notepad或其它简单的文本编辑器就足够了。要使用ADO,必须安装pywin32模块。如果您用的是ActivePython,那么系统中已经安装了该模块,否则请先下载并安装pywin32。下面是通过ADO操作MS SQL Server数据库的例子:

 1Python & ADO#-*- coding: utf-8 -*-
 2Python & ADO#以下的例子中假设classmates数据库中有一个名为classmate的表,该表有三个字段:name(必填字段)、Email、address,
 3Python & ADOimport win32com.client
 4Python & ADO
 5Python & ADODSN= r"Provider=SQLOLEDB;UID=xx;PWD=yy@cc;Database=classmates;Server=localhost"
 6Python & ADO#Select的例子
 7Python & ADORecordset = win32com.client.Dispatch(r"ADODB.Recordset")
 8Python & ADORecordset.ActiveConnection = DSN
 9Python & ADORecordset.Source = r"SELECT name, Email, address FROM dbo.classmate"
10Python & ADORecordset.CursorType = 0
11Python & ADORecordset.CursorLocation = 2
12Python & ADORecordset.LockType = 1
13Python & ADORecordset.Open()
14Python & ADOnumRows = 0
15Python & ADOwhile not Recordset.EOF:
16Python & ADO    print r'Name:',Recordset.Fields.Item("name").Value.encode('gbk')
17Python & ADO    if Recordset.Fields.Item("Email").Value != None:
18Python & ADO        print r'  EMail:',Recordset.Fields.Item("Email").Value.encode('gbk')
19Python & ADO    if Recordset.Fields.Item("address").Value != None : 
20Python & ADO        print r'  Address:',Recordset.Fields.Item("address").Value.encode('gbk')
21Python & ADO    numRows+=1
22Python & ADO    Recordset.MoveNext()
23Python & ADOprint 'Total Rows:',numRows
24Python & ADO
25Python & ADO#Insert的例子
26Python & ADOCommandInsert = win32com.client.Dispatch(r"ADODB.Command")
27Python & ADOCommandInsert.ActiveConnection = DSN
28Python & ADOCommandInsert.CommandText = r"INSERT INTO dbo.classmate (name, Email, address)  VALUES ('xx','abc@xyz.com','ABC Street' ) "
29Python & ADOCommandInsert.CommandType = 1
30Python & ADOCommandInsert.CommandTimeout = 0
31Python & ADOCommandInsert.Prepared = true
32Python & ADOCommandInsert.Execute()
33Python & ADO
34Python & ADO#Update的例子
35Python & ADOCommandUpdate = win32com.client.Dispatch(r"ADODB.Command")
36Python & ADOCommandUpdate.ActiveConnection = DSN
37Python & ADOCommandUpdate.CommandText = r"UPDATE dbo.classmate  SET Email='xx@yy.com', address='XX Street'  WHERE name='xx' "
38Python & ADOCommandUpdate.CommandType = 1
39Python & ADOCommandUpdate.CommandTimeout = 0
40Python & ADOCommandUpdate.Prepared = true
41Python & ADOCommandUpdate.Execute()
42Python & ADO
43Python & ADO#Delete的例子
44Python & ADOCommandDelete = win32com.client.Dispatch(r"ADODB.Command")
45Python & ADOCommandDelete.ActiveConnection = DSN
46Python & ADOCommandDelete.CommandText = r"DELETE FROM dbo.classmate  WHERE name = 'xx'"
47Python & ADOCommandDelete.CommandType = 1
48Python & ADOCommandDelete.CommandTimeout = 0
49Python & ADOCommandDelete.Prepared = true
50Python & ADOCommandDelete.Execute()
51Python & ADO

 

 如果您需要操作Access或其他数据库,只需修改DSN和相应的SQL语句即可,比如:DSN=r'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=D:\Test.mdb;'

参考:
http://www.ecp.cc/pyado.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadoobjmod.asp

相关文章:

  • 2021-06-17
  • 2021-11-23
  • 2021-12-12
  • 2021-12-29
  • 2021-07-16
  • 2022-03-04
  • 2021-05-16
  • 2021-11-09
猜你喜欢
  • 2022-01-17
  • 2021-06-18
  • 2021-08-06
  • 2022-12-23
相关资源
相似解决方案