简体   繁体   English

在 mysql.connector python 中创建表时出错 -- 语法错误代码 1064(4200)

[英]Error while creating tables in mysql.connector python -- syntax error code 1064(4200)

c1.execute(f"create table {sub_table_name1}(srno int, data_name varchar(255), data varchar(255))")

the error is错误是

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '(srno int, data_name varchar(255), data varchar(255))' at line 1检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“(srno int, data_name varchar(255), data varchar(255))”附近使用的正确语法

i swear to god i have no idea what is wrong...i'm still a beginner to python and mysql connection and any help is welcome我向上帝发誓我不知道出了什么问题......我仍然是 python 和 mysql 连接的初学者,欢迎任何帮助

What is the value of your sub_table_name1 variable at the time you execute that?执行时sub_table_name1变量的值是多少? If it's empty, then I think that error would be expected, because the SQL statement would be:如果它是空的,那么我认为该错误是预料之中的,因为 SQL 语句将是:

create table (srno int, ...

MySQL will see that as a missing name. MySQL 会将其视为缺失名称。 The way it informs you of the error is to tell you the part of the query at the point it got confused:它通知您错误的方式是在它混淆时告诉您查询的一部分:

for the right syntax to use near '(srno int, ...为 near '(srno int, ...

This means when it saw (srno int, ... it was expecting something else — I'm guessing it was expecting an identifier for the name of the table, but reached the open parenthesis character instead.这意味着当它看到(srno int, ...它期待着别的东西——我猜它期待着表名的标识符,但却到达了左括号字符。

Since you said you are new to python (and perhaps to programming), here's a general tip: question your assumptions.既然你说你是 python 的新手(也许是编程的新手),这里有一个一般提示:质疑你的假设。 You assumed the create table statement had a table name in it.您假设 create table 语句中有一个表名。 Does it?可以? How would you know?你怎么知道的? Can you add code to print the SQL statement string after you format it with the variables?您能否添加代码以在使用变量格式化后打印 SQL 语句字符串? Can you add code to print the sub_table_name1 variable before using it in the string?在字符串中使用它之前,您可以添加代码来打印sub_table_name1变量吗? Of course, you should remove such code after you are done debugging.当然,你应该在调试完成后删除这些代码。

"The most effective debugging tool is still careful thought, coupled with judiciously placed print statements." “最有效的调试工具仍然是仔细思考,加上明智地放置打印语句。”

— Brian Kernighan, "Unix for Beginners" (1979) — Brian Kernighan,“初学者的 Unix”(1979 年)

That's an old book, but the idea is still true!那是一本旧书,但这个想法仍然是正确的!

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

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