发布时间:2019-07-06 10:47:13编辑:auto阅读(1766)
dsn 数据源名称
user 用户名(可选)
password 密码(可选)
host 主机名(可选)
database 数据库名(可选)
举个例子:
connect(dsn='myhost:MYDB',user='guido',password='234$')
又或者
connect('218.244.20.22','username','password','databasename')
表示了DB-API的版本,分'1.0'和'2.0'.如果没有定义,默认为'1.0'
0 Threads may not share the module.
1 Threads may share the module, but not connections.
2 Threads may share the module and connections.
3 Threads may share the module, connections and cursors.
用于表示参数的传递方法,分为以下五种:
'qmark' 问号标识风格. e.g '... WHERE name=?'
'numeric' 数字,占位符风格. e.g '... WHERE name=:1'
'named' 命名风格. e.g 'WHERE name=:name'
'format' ANSI C printf风格. e.g '... WHERE name=%s'
'pyformat' Python扩展表示法. e.g '... WHERE name=%(name)s'
StandardError
|__Warning
|__Error
|__InterfaceError
|__DatabaseError
|__DataError
|__OperationalError
|__IntegerityError
|__InternalError
|__ProgrammingError
|__NotSupportedError
定义一些常用的数据类型.但是目前用不到,就先不分析
import MSSQL
db = MSSQL.connect('SQL Server IP', 'username', 'password', 'db_name')
c = db.cursor()
sql = 'select top 20 rtrim(ip), rtrim(dns) from detail'
c.execute(sql)
for f in c.fetchall():
print "ip is %s, dns is %s" % (f[0], f[1])
sql = 'insert into detail values('192.168.0.1', 'www.dns.com.cn')
c.execute(sql)
import dbi, odbc # ODBC modules
import time # standard time module
dbc = odbc.odbc( # open a database connection
'sample/monty/spam' # 'datasource/user/password'
)
crsr = dbc.cursor() # create a cursor
crsr.execute( # execute some SQL
"""
SELECT country_id, name, insert_change_date
FROM country
ORDER BY name
"""
)
print 'Column descriptions:' # show column descriptions
for col in crsr.description:
print ' ', col
result = crsr.fetchall() # fetch the results all at once
print '
First result row:
', result[0] # show first result row
print '
Date conversions:' # play with dbiDate object
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))
-------------------------------output--------------------------------
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
上一篇: Unity3d导入3dMax模型会产生的
下一篇: Python 5.4 定制类
48207
46962
37861
35160
29682
26345
25280
20303
19973
18431
56°
6064°
6774°
6262°
6234°
7357°
6183°
6314°
6786°
6792°