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'
import MSSQLdb = 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的一個例子
import dbi, odbc # ODBC modulesimport time # standard time moduledbc = odbc.odbc( # open a database connection 'sample/monty/spam' # 'datasource/user/password' )crsr = dbc.cursor() # create a cursorcrsr.execute( # execute some SQL """ SELECT country_id, name, insert_change_date FROM country ORDER BY name """ )print 'Column descriptions:' # show column descriptionsfor col in crsr.description: print ' ', colresult = crsr.fetchall() # fetch the results all at onceprint '\nFirst result row:\n ', result[0] # show first result rowprint '\nDate conversions:' # play with dbiDate objectdate = 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