设为首页 加入收藏

TOP

Python连接SQL Server数据获取2
2017-12-23 06:07:10 】 浏览:190
Tags:Python 连接 SQL Server 数据 获取
# -*- coding:utf-8 -*-

import pymssql
import json

class MSSQLTABEL:
    def __init__(self,host,user,pwd,db):
        self.host = host
        self.user = user
        self.pwd = pwd
        self.db = db

    def __GetConnect(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")
        cursor = self.conn.cursor()
        if not cursor:
            raise(NameError,"连接数据库失败")
        else:
            return cursor

    def ExecQuery(self,sql):
        cursor = self.__GetConnect()
        cursor.execute(sql)
        resList = cursor.fetchall()

        #查询完毕后必须关闭连接
        self.conn.close()
        return resList

    def ExecNonQuery(self,sql):
        cursor = self.__GetConnect()
        cursor.execute(sql)
        self.conn.commit()
        self.conn.close()

msList = MSSQLTABEL(host="1.1.1.1",user="1",pwd="123",db="test_1")
reslist = msList.ExecQuery("select * from A2017")
for i in reslist:
    print json.dumps(i,ensure_ascii=False)

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python自动化--语言基础8--接口请.. 下一篇python 面向对象十二 元类

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目