python 连接 sqlserver 数据库读取表数据

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:sqlserver.py
import pyodbc
 
# 数据库服务器信息
driver = 'SQL Server Native Client 10.0'  # 因版本不同而异
server = '194.00.000.00,00000'  
user = 'user'
password = 'password'
database = 'database'  #数据库名称
 
def select_log(server,user,password,database,sql,projectname):
    reList = []
    conn = pyodbc.connect(driver=driver, server=server, user=user, password=password, database=database)
    cur = conn.cursor()
    cur.execute(sql)       #执行SQL语句
    row = cur.fetchone()
    cur.execute(row[0])
    rows = cur.fetchall()  # list
    conn.close()

    for row in rows:
        if row.status == "未成功" :
             reList.append (projectname+':'+str(row))
    
    return reList

你可能感兴趣的:(python 连接 sqlserver 数据库读取表数据)