在 mysql.connector python 中创建表时出错 -- 语法错误代码 1064(4200)
c1.execute(f"create table {sub_table_name1}(srno int, data_name varchar(255), data varchar(255))")
错误是
mysql.connector.errors.ProgrammingError: 1064 (42000): 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '(srno int, data_name varchar(255), data varchar(255))' 附近使用的正确语法
我向上帝发誓我不知道出了什么问题。 ..我仍然是 python 和 mysql 连接的初学者,欢迎任何帮助
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; 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行该命令时,
sub_table_name1
变量的值是多少?如果它是空的,那么我认为会出现错误,因为 SQL 语句将是:MySQL 会将其视为丢失的名称。它通知您错误的方式是告诉您查询中出现混淆的部分:
这意味着当它看到
(srno int, ...
) 时,它正在期待其他东西 - 我猜它正在期待一个标识符表的名称,但到达了左括号字符,因为您说您是Python新手(也许是编程新手),所以这里有一个一般提示:质疑您的假设。您假设创建表。语句中有一个表名。你怎么知道?你可以在使用变量格式化 SQL 语句字符串之后添加代码来打印它吗?当然,你可以在字符串中使用它之前添加代码来打印它吗? ,您应该在完成调试后删除此类代码。
这是一本老书,但这个想法仍然是正确的!
What is the value of your
sub_table_name1
variable at the time you execute that? If it's empty, then I think that error would be expected, because the SQL statement would be:MySQL will see that as a missing name. The way it informs you of the error is to tell you the part of the query at the point it got confused:
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.Since you said you are new to python (and perhaps to programming), here's a general tip: question your assumptions. You assumed the create table statement had a table name in it. Does it? How would you know? Can you add code to print the SQL statement string after you format it with the variables? Can you add code to print the
sub_table_name1
variable before using it in the string? Of course, you should remove such code after you are done debugging.That's an old book, but the idea is still true!