0
import mysql.connector
from mysql.connector import Error

def query_with_fetchone(tit):    

    try:
        conn = mysql.connector.connect(host='localhost',
                                   database='python_mysql',
                                   user='root',
                                   password='')     

        cursor = conn.cursor()
        query = "INSERT INTO sample(title) VALUES(%s)"
        args = (tit)
        cursor.execute(query,args)

        if cursor.lastrowid:
            print('last insert id', cursor.lastrowid)
        else:
            print('last insert id not found')

        conn.commit()
    except Error as e:
        print(e)

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

if __name__ == '__main__':
    query_with_fetchone('Vrajesh')

I am getting following error:

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

Please Guide I am new Python

2
  • 2
    I have two guesses that may be helpful: (1) you should put an ending semicolon at the end of the query, and (2) try to change args into a list, not a tuple. Commented Dec 2, 2017 at 10:44
  • Your Second Guess Worked Commented Dec 2, 2017 at 10:52

1 Answer 1

1

Your args is not a tuple, tuple with one element requires comma.

args = (tit,)

It may be the solution

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You. It was the expected solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.