python操作sqlserver

import pymssql
#连接sqlserver语法
db=pymssql.connect('10.224.201.27','sa','987644Server','ekp-bond')
cur=db.cursor()
cur.execute('select * from fc_bond_info')
n=1
for i in cur.fetchall():
    if n>100:
        break
    else:
        print(i)
        n+=1
db.close()




import pymssql
#连接sqlserver语法
db=pymssql.connect('10.224.201.27','sa','987644Server','ekp-bond')
cur=db.cursor()
cur.execute('select * from fc_bond_info')
n=1
for i in cur.fetchall():
    if n>100:
        break
    else:
        print(i)
        n+=1




如果你可以使用另一种语法:with 来避免手动关闭cursors和connection连接

import pymssql 
server = "187.32.43.13"    # 连接服务器地址
user = "root"         # 连接帐号
password = "1234"      # 连接密码
with pymssql.connect(server, user, password, "你的连接默认数据库名称") as conn:
    with conn.cursor(as_dict=True) as cursor:   # 数据存放到字典中
        cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
        for row in cursor:
            print("ID=%d, Name=%s" % (row['id'], row['name']))调用存储过程:

with pymssql.connect(server, user, password, "tempdb") as conn:
    with conn.cursor(as_dict=True) as cursor:
        cursor.execute("""
        CREATE PROCEDURE FindPerson
            @name VARCHAR(100)
        AS BEGIN
            SELECT * FROM persons WHERE name = @name
        END
        """)
        cursor.callproc('FindPerson', ('Jane Doe',))
        for row in cursor:
            print("ID=%d, Name=%s" % (row['id'], row['name']))



2.使用_mssql连接sql server数据库并实现操作

import _mssql
# 创建连接
conn = _mssql.connect(server='SQL01', user='user', password='password', \
    database='mydatabase')
print(conn.timeout)
print(conn.login_timeout)

# 创建table
conn.execute_non_query('CREATE TABLE persons(id INT, name VARCHAR(100))')
# insert数据
conn.execute_non_query("INSERT INTO persons VALUES(1, 'John Doe')")
conn.execute_non_query("INSERT INTO persons VALUES(2, 'Jane Doe')")
# 查询操作
conn.execute_query('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
for row in conn:
    print "ID=%d, Name=%s" % (row['id'], row['name'])
#查询数量count()
numemployees = conn.execute_scalar("SELECT COUNT(*) FROM employees")
# 查询一条数据
employeedata = conn.execute_row("SELECT * FROM employees WHERE id=%d", 13)
# 带参数查询的几个例子:
conn.execute_query('SELECT * FROM empl WHERE id=%d', 13)
conn.execute_query('SELECT * FROM empl WHERE name=%s', 'John Doe')
conn.execute_query('SELECT * FROM empl WHERE id IN (%s)', ((5, 6),))
conn.execute_query('SELECT * FROM empl WHERE name LIKE %s', 'J%')
conn.execute_query('SELECT * FROM empl WHERE name=%(name)s AND city=%(city)s', \
    { 'name': 'John Doe', 'city': 'Nowhere' } )
conn.execute_query('SELECT * FROM cust WHERE salesrep=%s AND id IN (%s)', \
    ('John Doe', (1, 2, 3)))
conn.execute_query('SELECT * FROM empl WHERE id IN (%s)', (tuple(xrange(4)),))
conn.execute_query('SELECT * FROM empl WHERE id IN (%s)', \
    (tuple([3, 5, 7, 11]),))
#关闭连接
conn.close()




pymssql官方文档  api  http://www.pymssql.org/en/stable/ref/_mssql.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值