Search code examples
pythonpypyodbc

Pypyodbc TabError: inconsistent use of tabs and spaces in indentation


This is a common problem it seems on here but in my case I cant find an answer. Why is it saying inconsistent use of tabs and indentation here

def exectute_SQL():    #This function executes SQL to pull counts from a table where it wasnt possible to get an excel 
    con = pypyodbc.connect(conn_str)
    cur = con.cursor()
    sql = "SELECT * FROM Elig_Own.DST_Report_Validation_Test" #WHERE ysn_active = '1'"
    cur.execute(sql)

    rows = cur.fetchall()

    for row in rows:
        strFnd = 0
        strReportName = row[1]
        strSrcName = row[2]
        strDestName = row[3]
        strFileName = row[4]
        try:
            for report in strReportName:
                if report == 'STR_DB Load to SQL':
                    cur.execute("$result = SELECT TOP 1 COUNT(*) FROM Elig_Own.STR_DB GROUP BY LAST_UPDATED ORDER BY LAST_UPDATED DESC;")
                    cur.execute("INSERT INTO Elig_Own.DST_Report_Status_Test(TDate, Report, Records, Status) VALUES(CAST(GetDate() AS Date), 'STR_DB Load to SQL', ?, 'Passed')",(result))
                    con.commit()
        except:
            print("Couldnt execute script")

And This is the error message

C:\Users\cn192406\Documents\Programs>python File_Check_Dart_Functions.py
File "File_Check_Dart_Functions.py", line 73
cur.execute("$result = SELECT TOP 1 COUNT(*) FROM Elig_Own.STR_DB GROUP BY LAST_UPDATED ORDER BY LAST_UPDATED DESC;")

TabError: inconsistent use of tabs and spaces in indentation


Solution

  • Try this:

    def exectute_SQL():  # This function executes SQL to pull counts from a table where it wasnt possible to get an excel
        con = pypyodbc.connect(conn_str)
        cur = con.cursor()
        sql = "SELECT * FROM Elig_Own.DST_Report_Validation_Test"  # WHERE ysn_active = '1'"
        cur.execute(sql)
    
        rows = cur.fetchall()
    
        for row in rows:
            strFnd = 0
            strReportName = row[1]
            strSrcName = row[2]
            strDestName = row[3]
            strFileName = row[4]
            try:
                for report in strReportName:
                    if report == "STR_DB Load to SQL":
                        cur.execute(
                            "$result = SELECT TOP 1 COUNT(*) FROM Elig_Own.STR_DB GROUP BY LAST_UPDATED ORDER BY LAST_UPDATED DESC;"
                        )
                        cur.execute(
                            "INSERT INTO Elig_Own.DST_Report_Status_Test(TDate, Report, Records, Status) VALUES(CAST(GetDate() AS Date), 'STR_DB Load to SQL', ?, 'Passed')",
                            (result),
                        )
                        con.commit()
            except Exception as e:
                pass