使用python遍历SQL Server数据库的程序

以前调试刚接手的程序,该程序使用SQL Server ,表很多,上百。常常需要根据某一个特定的值,定位该值在数据库中的什么表的什么位置。对于此需要,我写了下面的代码,用Python连接到SQL Server ,找出所有表,对于表中每个字段值查找,找出目标值。

 

#write by mrjwt

#need pip install pyodbc

 

import pyodbc

 

def listTables():

    pass

    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourserverpathname;DATABASE=yourdbname;UID=sa;PWD=yourpassword')

    cursor = cnxn.cursor()

    cursor.execute("select name,type,type_desc, create_date,modify_date from sys.tables")

 

    rows = cursor.fetchall()

    for row in rows:

        #print(row)

        tname=row[0]

        print(tname)

        print(str(tname))

        s =  str(tname) 

        sqlstr = "select COLUMN_NAME,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS t where t.TABLE_NAME = '" +s+ "';"

        cursor2 = cnxn.cursor()

        cursor2.execute(sqlstr)

        rows2 = cursor2.fetchall()

        for row2 in rows2:

            print(row2)

        cursor2.close()


 

    cursor.close()

    cnxn.close()

 

def foundInTables( c,  v):

    pass

    colName=str(c)

    value=str(v)

    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourserverpathname;DATABASE=yourdbname;UID=sa;PWD=yourpassword')

    cursor = cnxn.cursor()

    sqls = "select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS t where t.COLUMN_NAME = '" + colName + "';"

    cursor.execute(sqls)

    rows = cursor.fetchall()

    for row in rows:

        tname=str(row[0])

        print("TableName:"+tname)

        sqlstr =  "select * from "+ tname + " t where t."+ colName + " = '" + value + "';"

        cursor2 = cnxn.cursor()

        cursor2.execute(sqlstr)

        rows2 = cursor2.fetchall()

        for row2 in rows2:

            print(row2)

        cursor2.close()

 

    cursor.close()

    cnxn.close()

 

def foundValueInTables(v):

    pass

    print("#### \nTo find in tables : " + v+'\n####\n')

    value=unicode(v)

    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourserverpathname;DATABASE=yourdbname;UID=sa;PWD=yourpassword')

    cursor = cnxn.cursor()

    sqls = "select DISTINCT TABLE_NAME from INFORMATION_SCHEMA.COLUMNS ;"

    #sqls = "select name  from sys.tables"

    cursor.execute(sqls)

    rows = cursor.fetchall()

    for row in rows:

        tname=str(row[0])

        foundValueInTable(value,tname,cnxn)

    cursor.close()

    cnxn.close()


 

def foundValueInTable(v,tname,con):

    pass

    sqlstr =  "select * from "+ tname + ";"

    cursor2 = con.cursor()

    cursor2.execute(sqlstr)

    rows2 = cursor2.fetchall()

    bfound=False

    for row2 in rows2:

        for colval in row2:

            pass

            if(unicode(colval)==v):

                bfound=True

                print(row2)

                break

    if(bfound==True):

        print("\nFound in TableName: "+tname+"\n\n")

    cursor2.close()


 

if __name__ == '__main__':

    pass

    #listTables()

    foundValueInTables("CPText_039")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值