简体   繁体   English

如何使用 python 将变量插入 mysql 数据库? “您的 SQL 语法有错误”

[英]How to insert variable to mysql database with python? “You have an error in your SQL syntax”

I am trying to insert one variable to mysql database with python.我正在尝试使用 python 将一个变量插入 mysql 数据库。 But I have syntax error.. Can You help me please?但我有语法错误..你能帮帮我吗? :) :)

import mysql.connector
    from mysql.connector import Error
    from mysql.connector import errorcode

    try:
        connection = mysql.connector.connect(host='localhost',
                                         database='Xiaomi_Temp',
                                         user='user',
                                         password='pass')
        cursor = connection.cursor()
        mySql_insert_query = """ INSERT INTO Temp_test (batt) VALUES (%d) """
        recordTuple = (batt)
        print(batt)
        cursor.execute(mySql_insert_query, recordTuple)


        connection.commit()
        print("Record inserted successfully into table")
        cursor.close()

    except mysql.connector.Error as error:
        print("Failed to insert record into table {}".format(error))

    finally:
        if (connection.is_connected()):
            connection.close()
            print("MySQL connection is closed")

I have variable batt and it has integer value.我有可变的 batt,它有 integer 值。 My output is:我的 output 是:

Failed to insert record into table 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 '%d)' at line 1
MySQL connection is closed

As the whole query needs to be in a string format while execution of query so %s should be used instead of %d which can be done as follows,由于在执行查询时整个查询需要采用字符串格式,因此应使用 %s 而不是 %d ,可以按如下方式完成,

         mySql_insert_query = """ INSERT INTO Temp_test (batt) VALUES (%s) """
         recordTuple = (batt)
         print(batt)
         cursor.execute(mySql_insert_query, recordTuple)

暂无
暂无

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

相关问题 1064,“你的SQL语法有错误; ......”Python MySQL - 1064, “You have an error in your SQL syntax;…” Python MySQL Python MySQL, 1064 (42000), “您的 SQL 语法有错误;...” - Python MySQL, 1064 (42000), "You have an error in your SQL syntax;..." 您的 SQL 语法有错误; 带 python - You have an error in your SQL syntax; With python 您的 SQL 语法有错误; 检查与您的 MySQL 服务器 PYTHON 相对应的手册 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server PYTHON Python,MySQL _mysql_exceptions.ProgrammingError:(1064,'您的SQL语法有错误 - Python, MySQL _mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax 将CSV数据插入MySQL数据库-“您的SQL语法有误;在第1行的''附近 - Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1 1064,“你的SQL语法有错误”插入MySql - 1064, “You have an error in your SQL syntax” inserting in MySql MYSQL 数据库错误:“pymysql.err.ProgrammingError: (1064, ”您的 SQL 语法有错误;请查看与您对应的手册-" - MYSQL Database error: “pymysql.err.ProgrammingError: (1064, ”You have an error in your SQL syntax; check the manual that corresponds to your-" Python mysql 参数化 date_add 查询失败(“您的 SQL 语法有错误”) - Python mysql paramaterised date_add query fails ('You have an error in your SQL syntax') python tkinter 你的 SQL 语法有错误 - python tkinter You have an error in your SQL syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM