python sqlserver 完整的例子

import pymssql

SQLSERVER = '10.19.1.50'
USER = 'sa'
PASSWORD = '123'
DATABASE = 'GuBei_Data'
g_Conn = None

def openDB():
    global g_Conn
    g_Conn = pymssql.connect(host=SQLSERVER,user=USER,password=PASSWORD,database=DATABASE)

def closeDB():
    global g_Conn
    g_Conn.close()

def query(sql):
    try:
        openDB()
        global g_Conn
        cursor = g_Conn.cursor()
        cursor.execute(sql)
        rows = cursor.fetchall()
        return rows
    except Exception,e:
        print e
    finally:
        closeDB()

def insert(sql):
    try:
        openDB()
        global g_Conn
        cursor = g_Conn.cursor()
        cursor.execute(sql)
        g_Conn.commit()
    except Exception,e:
        g_Conn.rollback()
        print e
    finally:
        closeDB()

def insert_many(sql, params):
    try:
        openDB()
        global g_Conn
        cursor = g_Conn.cursor()
        for param in params:
            sql_temp = sql % param
            cursor.execute(sql_temp)
        g_Conn.commit()
        return True
    except Exception,e:
        g_Conn.rollback()
        print e
    finally:
        closeDB()


import bag.sqlserver as sql
reload(bag.sqlserver)

sql.insert("INSERT INTO tblComment([ID],[Number],[visitor],[comments]) VALUES(5,'view3','youke4','very done')")

sqlstr="INSERT INTO tblComment([ID],[Number],[visitor],[comments]) VALUES(%d,'%s','%s','%s')"
data=[(6,'view3','youke4','very done'),(7,'view3','youke4','very done')]
sql.insert_many(sqlstr,data)

sql.insert("delete from tblComment where id=5 or id=6 or id=7")

print sql.query("select * from tblComment")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值