当前位置: 代码迷 >> SQL >> Python 施用 pymssql 连接 MSSQL
  详细解决方案

Python 施用 pymssql 连接 MSSQL

热度:85   发布时间:2016-05-05 13:05:48.0
Python 使用 pymssql 连接 MSSQL

pymssql 是 Python 语言用来连接微软 SQL SERVER 数据库的轻量级类库,实现了 Python DB API 2.0 。
项目主页:http://pymssql.sourceforge.net/

一个简单的示例代码如下:

?

import pymssqlconn = 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, xinos.king)", \    [ (1, 'John Doe'), (2, 'Jane Doe') ])conn.commit()  # you must call commit() to persist your data if you don't set autocommit to Truecur.execute('SELECT * FROM persons WHERE salesrep=xinos.king', 'John Doe')row = cur.fetchone()while row:    print "ID=%d, Name=xinos.king" % (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()

?

  相关解决方案