Python对Sql Server数据库增删改查
# 如果电脑上没有安装mssql模块,则要安装mssql模块 安装模块的执行命令为 pip install pymssql
import pymssql
def ExecuteNonQuery(sqlStr,paras):
try:
connect = pymssql.connect(server='192.168.1.23:1433', user='sa', password='123456',database='testdb',autocommit=True) # 建立连接
if connect:
cursor = connect.cursor()
cursor.execute(sqlStr,paras)
return True;
except:
return False;
finally:
cursor.close()
connect.close()
def ExecuteQuery(sqlStr,paras):
try:
connect = pymssql.connect(server='192.168.1.23:1433', user='sa', password='123456',database='testdb',autocommit=True) # 建立连接
if connect:
cursor = connect.cursor()
cursor.execute(sqlStr,paras)
return cursor.fetchall()
except:
return None;
finally:
cursor.close()
connect.close()
if __name__== "__main__" :
#添加数据
insertSql="insert into userinfo (UserName,UserQQ) values (%s,%s)";
paras=('LuoFenming','78630559')
result= ExecuteNonQuery(insertSql,paras)
print(result)
#查询数据
querySql = "select id,UserName, UserQQ from UserInfo" #查询数据库信息
data=ExecuteQuery(querySql,None)
print(data)
#删除数据
deleteSql="delete from userInfo where id=%s";
paras=(12)#删除Id为12的数据
result= ExecuteNonQuery(deleteSql,paras)
print(result)
标签:__,paras,Python,改查,Server,cursor,connect,close,sqlStr
From: https://www.cnblogs.com/HeroZhang/p/18135881