• python 连接MS sql server2005数据库


    在google上有个开源的项目叫pymssql

    项目地址:https://code.google.com/p/pymssql/

    看来段代码

    import pymssql
    conn = pymssql.connect(host='192.168.1.1',port='1433', user='user', password='password', database='mydatabase', as_dict=True)
    cur = conn.cursor()
    
    cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
    for row in cur:
        print "ID=%d, Name=%s" % (row['id'], row['name'])
    
    conn.close()


    第一行导入

    第二个连接ip为192.168.1.1端口为1433的mydatabase数据库

    第四行就是查询表

    下面就是遍历表里面的数据很简单吧。

    调用存储过程

    import pymssql
    conn = pymssql.connect(host='192.168.1.1','1433', user='user', password='password', database='mydatabase', as_dict=True)
    cur = conn.cursor()
    
    cur.callproc('findPerson', ('John Doe',))
    for row in cur:
        print "ID=%d, Name=%s" % (row['id'], row['name'])
    
    conn.close()

    创建表

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





  • 相关阅读:
    cogs luogu 1901. [国家集训队2011]数颜色 待修改莫队
    luogu cogs 421. HH的项链
    luogu P2709 小B的询问
    排序
    算法基本概念
    金融的简单介绍
    Admin组件-----Django自带
    day02-菜单处理
    day01
    selenium常用方法
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3278143.html
Copyright © 2020-2023  润新知