简体   繁体   中英

Getting Error in insert record in MySql from Python

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

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

args = (tit,)

It may be the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM