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

发布于 2025-01-14 04:09:57 字数 396 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2025-01-21 04:09:57

执行该命令时,sub_table_name1 变量的值是多少?如果它是空的,那么我认为会出现错误,因为 SQL 语句将是:

create table (srno int, ...

MySQL 会将其视为丢失的名称。它通知您错误的方式是告诉您查询中出现混淆的部分:

在 '(srno int, ... 附近使用正确的语法

这意味着当它看到 (srno int, ... ) 时,它正在期待其他东西 - 我猜它正在期待一个标识符表的名称,但到达了左括号字符,

因为您说您是Python新手(也许是编程新手),所以这里有一个一般提示:质疑您的假设。您假设创建表。语句中有一个表名。你怎么知道?你可以在使用变量格式化 SQL 语句字符串之后添加代码来打印它吗?当然,你可以在字符串中使用它之前添加代码来打印它吗? ,您应该在完成调试后删除此类代码。

“最有效的调试工具还是缜密的思考,再加上
并明智地放置打印语句。”

— Brian Kernighan,《Unix 初学者》(1979)

这是一本老书,但这个想法仍然是正确的!

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:

create table (srno int, ...

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:

for the right syntax to use 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.

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.

"The most effective debugging tool is still careful thought, coupled
with judiciously placed print statements."

— Brian Kernighan, "Unix for Beginners" (1979)

That's an old book, but the idea is still true!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文