简体   繁体   English

从Python在MySql中的插入记录中获取错误

[英]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; 1064(42000):您的SQL语法错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s)' at line 1 检查与您的MariaDB服务器版本相对应的手册,以找到在第1行附近使用'%s)'的正确语法

Please Guide I am new Python 请指导我是新Python

Your args is not a tuple, tuple with one element requires comma. 您的args不是元组,只有一个元素的元组需要逗号。

args = (tit,)

It may be the solution 这可能是解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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