Devesh Agrawal
Devesh Agrawal

Reputation: 9212

1064 (42000): You have an error in your SQL syntax

I am expecting True/False from this function, which checks whether a mobile number is present in db or not?

def find(self, mobile):
    query = "select id from table_customer " \
            "WHERE mobile = %s"
    args = (mobile)
    resp = False

    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        cursor = conn.cursor(buffered=True)
        cursor.execute(query, args)

        if cursor.rowcount == 1:
            resp = True

    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()

    return resp

while I execute It gave me this error:

1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

with hardcoded mobile number there is no issues.

Why it is throwing syntax error in above code?

Upvotes: 0

Views: 321

Answers (1)

xio
xio

Reputation: 640

Try this method

The following code converts args to a tuple type.

args = (mobile,)

Upvotes: 1

Related Questions