Error Description:
Today, when using Python to connect to MySQL database for data addition, an error “pymysql. Err. Operationalerror: (1054,” unknown column ‘AA’ in ‘field list’) “appears. After query, it is because when pymysql inserts data, if it is of type vchar or string, it needs single quotation marks to insert successfully.
Solution:
Modify the SQL statement using the repr() function in Python:
Original SQL statement:
sql = "INSERT INTO student(name,sex,age,grade) VALUES ("+name+","+sex+","+age+","+grade+")"
Modified SQL statement:
sql_0="INSERT INTO student(name,sex,age,grade) VALUES (%s,%s,%s,%s)"
sql=sql_0 % (repr(name), repr(sex),repr(age),repr(grade))
Similar Posts:
- [Solved] json.decoder.JSONDecodeError: Expecting ‘,‘ delimiter: line xx column xx (char xxx)
- Treatment of MySQL database keyword as column name by mybatisplus — sqlsyntax errorexception: you have an error in your SQL syntax;
- With check option in SQL
- [Err] 1064 – 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
- Error reporting of Python STR type dictionary conversion
- BindingException: Mapper method ‘xxx.dao.StudentDao.insertStudent’ attempted to return null from a method with a primitive return type (int).
- Solve Python MySQL insert int data error: typeerror% d format: a number is required, not str
- [Solved] Python error: attributeerror: type object ‘STR’ has no attribute ‘_name_’ (machine learning practice treeplotter code)
- About BeanShell error invoking BSH method
- [Solved] Python pymysql.err.OperationalError: (2013, ‘Lost connection to MySQL server during query…