pymssql — 简单的MSSQL数据库访问 Python扩展模块

jopen 11年前

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.

项目主页:http://www.open-open.com/lib/view/home/1383357227292