python3 操作sqlSever
Posted shaomine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 操作sqlSever相关的知识,希望对你有一定的参考价值。
相关代码如下:
#coding =utf-8 import os import pyodbc import time class SqlDb: def __init__(self, server=‘DESKTOP-94P70L6‘, database=‘ott‘, userid=‘sa‘, password=‘password‘): ‘‘‘‘‘ initialization ‘‘‘ self.server = server self.database = database self.userid = userid self.password = password def __GetConnect(self): ‘‘‘‘‘ Connect to the DB ‘‘‘ if not self.database: raise (NameError, "no setting db info") self.conn = pyodbc.connect(DRIVER=‘{SQL Server Native Client 10.0}‘, SERVER=self.server, DATABASE=self.database, UID=self.userid, PWD=self.password, charset="UTF-8") # self.conn = "DRIVER={SQL Server Native Client 10.0};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s" % (self.server,self.database,self.userid,self.password) cur = self.conn.cursor() if not cur: raise (NameError, "connected failed!") else: return cur def ExecQuery(self, sql): ‘‘‘‘‘ Perform one Sql statement ‘‘‘ cur = self.__GetConnect() cur.execute(sql) ret = cur.fetchall() cur.close() self.conn.close() return ret def ExecNoQuery(self, sql): ‘‘‘‘‘ Person one Sql statement like write data, or create table, database and so on‘‘‘ cur = self.__GetConnect() cur.execute(sql) self.conn.commit() cur.close() self.conn.close()
以上是关于python3 操作sqlSever的主要内容,如果未能解决你的问题,请参考以下文章