#实例化数据库引擎
import win32com.client
engine = win32com.client.Dispatch("DAO.DBEngine.35")
#实例化数据库对象,建立对数据库的连接
db = engine.OpenDatabase(r"c:/temp/mydb.mdb")
rs = db.OpenRecordset("customers")
#可以采用SQL语言对数据集进行操纵
rs = db.OpenRecordset("select * from customers where state = 'OH'")
db.Execute("delete * from customers where balancetype = 'overdue' and name = 'bill'")
#注意,删除的数据不能复原了J
while not rs.EOF:
print rs.Fields("State").Value
rs.MoveNext()
import dbi, odbc # 导入ODBC模块
import time # 标准时间模块
dbc = odbc.odbc( # 打开一个数据库连接
'sample/monty/spam' # '数据源/用户名/密码'
)
crsr = dbc.cursor() # 产生一个cursor
crsr.execute( # 执行SQL语言
"""
SELECT country_id, name, insert_change_date
FROM country
ORDER BY name
"""
)
print 'Column descriptions:' # 显示行描述
for col in crsr.description:
print ' ', col
result = crsr.fetchall() # 一次取出所有的结果
print '/nFirst result row:/n ', result[0] # 显示结果的第一行
print '/nDate conversions:' # 看看dbiDate对象如何?
date = result[0][-1]
fmt = ' %-25s%-20s'
print fmt % ('standard string:', str(date))
print fmt % ('seconds since epoch:', float(date))
timeTuple = time.localtime(date)
print fmt % ('time tuple:', timeTuple)
print fmt % ('user defined:', time.strftime('%d %B %Y', timeTuple))
Column descriptions:
('country_id', 'NUMBER', 12, 10, 10, 0, 0)
('name', 'STRING', 45, 45, 0, 0, 0)
('insert_change_date', 'DATE', 19, 19, 0, 0, 1)
First result row:
(24L, 'ARGENTINA', <DbiDate object at 7f1c80>)
Date conversions:
standard string: Fri Dec 19 01:51:53 1997
seconds since epoch: 882517913.0
time tuple: (1997, 12, 19, 1, 51, 53, 4, 353, 0)
user defined: 19 December 1997
from calldll import odbc
dbc = odbc.environment().connection() # create connection
dbc.connect('sample', 'monty', 'spam') # connect to db
# alternatively, use full connect string:
# dbc.driver_connect('DSN=sample;UID=monty;PWD=spam')
print 'DBMS: %s %s/n' % ( # show DB information
dbc.get_info(odbc.SQL_DBMS_NAME),
dbc.get_info(odbc.SQL_DBMS_VER)
)
result = dbc.query( # execute query & return results
"""
SELECT country_id, name, insert_change_date
FROM country
ORDER BY name
"""
)
print 'Column descriptions:' # show column descriptions
for col in result[0]:
print ' ', col
print '/nFirst result row:/n ', result[1] # show first result row
DBMS: Oracle 07.30.0000
Column descriptions:
('COUNTRY_ID', 3, 10, 0, 0)
('NAME', 12, 45, 0, 0)
('INSERT_CHANGE_DATE', 11, 19, 0, 1)
First result row:
['24', 'ARGENTINA', '1997-12-19 01:51:53']
>>> import win32com.client >>> conn = win32com.client.Dispatch(r'ADODB.Connection') >>> DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:/MyDB.mdb;' >>> conn.Open(DSN)
>>> rs = win32com.client.Dispatch(r'ADODB.Recordset')
>>> rs_name = 'MyRecordset'
>>> rs.Open('[' + rs_name + ']', conn, 1, 3)
>>> flds_dict = {}
>>> for x in range(rs.Fields.Count):
... flds_dict[x] = rs.Fields.Item(x).Name
>>> print rs.Fields.Item(1).Type 202 # 202 is a text field >>> print rs.Fields.Item(1).DefinedSize 50 # 50 Characters
>>> rs.AddNew() >>> rs.Fields.Item(1).Value = 'data' >>> rs.Update()
>>> x = rs.Fields.Item(1).Value >>> print x 'data'
>>> rs.AddNew()
>>> x = rs.Fields.Item('Auto_Number_Field_Name').Value
# x contains the AutoNumber
>>> rs.Fields.Item('Field_Name').Value = 'data'
>>> rs.Update()
>>> oCat = win32com.client.Dispatch(r'ADOX.Catalog') >>> oCat.ActiveConnection = conn >>> oTab = oCat.Tables >>> for x in oTab: ... if x.Type == 'TABLE': ... print x.Name
>>> conn = win32com.client.Dispatch(r'ADODB.Connection')
>>> DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:/MyDB.mdb;'
>>> sql_statement = "INSERT INTO [Table_Name]
([Field_1], [Field_2]) VALUES ('data1', 'data2')"
>>> conn.Open(DSN)
>>> conn.Execute(sql_statement)
>>> conn.Close()
>>> # See example 3 above for the set-up to this >>> rs.MoveFirst() >>> count = 0 >>> while 1: ... if rs.EOF: ... break ... else: ... count = count + 1 ... rs.MoveNext()
>>> rs.Cursorlocation = 3 # don't use parenthesis here
>>> rs.Open('SELECT * FROM [Table_Name]', conn) # be sure conn is open
>>> rs.RecordCount # no parenthesis here either
186
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有