connect(parameters...) 鍏朵腑鐨勫弬鏁版牸寮忓涓?
dsn 鏁版嵁婧愬悕縐? user 鐢ㄦ埛鍚?鍙? password 瀵嗙爜(鍙? host 涓繪満鍚?鍙? database 鏁版嵁搴撳悕(鍙? 涓句釜渚嬪瓙: connect(dsn='myhost:MYDB',user='guido',password='234$') 鍙堟垨鑰? connect('218.244.20.22','username','password','databasename')
姝ゆ爣鍑嗚瀹氫簡浠ヤ笅鐨勪竴浜涘叏灞鍙橀噺:
apilevel:
琛ㄧず浜咲B-API鐨勭増鏈?鍒?1.0'鍜?2.0'.濡傛灉娌℃湁瀹氫箟,榛樿涓?1.0'
threadsafety:
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.
paramstyle:
鐢ㄤ簬琛ㄧず鍙傛暟鐨勪紶閫掓柟娉?鍒嗕負浠ヤ笅浜旂: '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
榪炴帴瀵硅薄鍖呭惈濡備笅鏂規硶:
娓告爣瀵硅薄鍖呭惈濡備笅灞炴у拰鏂規硶:
瀹氫箟涓浜涘父鐢ㄧ殑鏁版嵁綾誨瀷.浣嗘槸鐩墠鐢ㄤ笉鍒?灝卞厛涓嶅垎鏋?/blockquote>
褰撶劧,鎴戜滑瑕佺煡閬撶殑鏄?榪欎釜鍙槸涓涓爣鍑?涓鑸潵璇存爣鍑嗛噷闈㈠畾涔変簡鐨勪細瀹炵幇,浣嗚繕鏈夊緢澶氱壒瀹氱殑瀹炵幇,鎴戜滑涔熼渶瑕佸幓鎺屾彙鍝簺涓滆タ,涓嶈繃濡傛灉鎴戜滑灝嗚繖浜涙爣鍑嗙殑鎺屾彙浜?閭d箞鎿嶄綔涓鑸殑灝變笉浼氭湁闂浜?
涓嬮潰緇欏嚭鍑犱釜鏁版嵁搴撶浉鍏崇殑緗戝潃
涓嬮潰涓劇殑渚嬪瓙鏄互MSSQL涓烘牱鏉跨殑,浣嗘槸鎹㈡垚鍏朵粬鐨勯┍鍔ㄤ篃涓鏍峰彲浠ュ仛,榪欎釜灝卞拰Perl鐨勬暟鎹簱鎿嶄綔鍗佸垎鐨勭被浼?鍙互璁╂垜浠緢鏂逛究鐨勫疄鐜頒笉鍚屾暟鎹簱涔嬮棿鐨勭Щ妞嶅伐浣?
1. 鏌ヨ鏁版嵁
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])
2. 鎻掑叆鏁版嵁
sql = 'insert into detail values('192.168.0.1', 'www.dns.com.cn') c.execute(sql)
3. ODBC鐨勪竴涓緥瀛?/p>
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 '\nFirst result row:\n ', result[0] # show first result row print '\nDate 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