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