Search code examples
python-3.xpyodbcsql-server-2017pypyodbc

Pypyodbc query returning incorrect data


Hi there so bit of an odd one, the sql query im running is SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE 'Joe.Pike%'

If i run this in the server i get my correct personID, however when i run it in my code i get a random personID that doesn't exist, now what is even weirder is that my second bit of code queries based on the personID and that returns a value

    UserName = row[1]+'.'+row[2]
    Password = 'XXXXXXXX'
    Email = UserName + '@XXXXXXX.org.uk'

    emaillike = [str(row[1]+'.'+row[2]).replace("'", '')+"%"]

    sql = ("""SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE {}""").format(str(emaillike).replace("[","").replace("]",""))
    PersonId = cursor.execute(sql).fetchone()

    PersonPin = \
        str(cursor.execute('SELECT [Pin] FROM tblPersonPins WHERE [PersonId] = ?'
                       , [PersonId[0]]).fetchone()).replace("'","").replace("(","").replace(")","").replace(",","")

    print(sql)
    print(emaillike)
    print(str(PersonId))
    print(PersonPin)

I have even tried to fetchall rows with the Email LIKE query but that still only returned one ID and that is the non existent 105524 value

The sql server is MSSQL server 2017 and im running Python 3.7.2 It also appears to be an issue with pyodbc as well as pypyodbc

My print statements return :

SELECT [idPerson] FROM tblPersons WHERE [Email] LIKE 'Joe.Pike%' ['Joe.Pike%'] (105524,) 7271

But the numbers should both be different and once again to confirm when I run the printed sql statement in sql server developer it returns the correct data.

thanks in Advance

P.S this is unfinished code so please ignore the terrible formatting (I also know i should not be using positional parameters for sql queries but I can't see any other way to achieve the end result)


Solution

  • OMG please shoot me!

    Incorrect database, Man I wish I could Delete questions