python操作SqlServer,, python操作
python操作SqlServer,, python操作
python操作sqlserver需要使用pymssql模块
import pymssqlclass Mysql(): def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def connectDB(self): if not self.db: raise(NameError,"没有设置数据库信息") self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8") cur = self.conn.cursor() if not cur: raise(NameError,"连接数据库失败") else: return cur def ExecQuery(self,sql): ‘‘‘ 查询数据库 :param sql: :return: ‘‘‘ cur = self.connectDB() cur.execute(sql) res = cur.fetchall() self.conn.close() return res def ExecNonQuery(self,sql): ‘‘‘ 非查询操作 :param sql: :return: ‘‘‘ cur = self.connectDB() cur.execute(sql) self.conn.commit() self.conn.close()ms = Mysql(host="127.0.0.1",user="lary",pwd="lary123",db="testdb")res = ms.ExecQuery("select * from users")for i in res: print(i)sql="update users set name=‘%s‘ where id=1"%u‘测试‘print(sql)ms.ExecNonQuery(sql.encode(‘utf-8‘))
python操作SqlServer
相关内容
- python控制台输出带颜色的文字方法,, 在python开
- python数字类型之math库使用,,首先我们应当了解什么
- [Python]Pip换源以及设置代理,,Install py
- 一道简单的CTFpython沙箱逃逸题目,,看了几天的ssti注
- python访问数据库,,1. python
- Python ——tempfile,,主要有以下几个函数:
- linux python 修改环境变量 添加自定义模块路径,,举一个
- python——面向对象相关,,其他相关一、isin
- [Python]在python中调用shell脚本,并传入参数-02python操作s
- Python3中continue的用法,,continue :
评论关闭