pymssql — 簡單的MSSQL數據庫訪問 Python擴展模塊

jopen 11年前發布 | 39K 次閱讀 pymssql Python開發

pymssql 是 Python 語言的擴展模塊,提供從Python腳本訪問Microsoft SQL Servers。使用C API 代替 ODBC。它兼容 Python DB-API 2.0 。支持 Linux, *BSD, Solaris, Mac OS X 和 Windows 操作系統。

import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase')
cur = conn.cursor()
cur.execute('CREATE TABLE persons(id INT, name VARCHAR(100))')
cur.executemany("INSERT INTO persons VALUES(%d, %s)", \
    [ (1, 'John Doe'), (2, 'Jane Doe') ])
conn.commit()  # you must call commit() to persist your data if you don't set autocommit to True

cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe') row = cur.fetchone() while row: print "ID=%d, Name=%s" % (row[0], row[1]) row = cur.fetchone()

if you call execute() with one argument, you can use % sign as usual

(it loses its special meaning).

cur.execute("SELECT * FROM persons WHERE salesrep LIKE 'J%'")

conn.close()

You can also use iterators instead of while loop. Iterators are DB-API extensions, and are available since pymssql 1.0.</pre>

項目主頁:http://www.baiduhome.net/lib/view/home/1383357227292

</span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!