`
wtx358
  • 浏览: 25528 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

Python 使用 pymssql 连接 MSSQL

 
阅读更多

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

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

 

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, 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 True

cur.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()

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics