【问题标题】:SQL Syntax error on code that worked on php my admin在 php my admin 上工作的代码上的 SQL 语法错误
【发布时间】:2021-08-11 05:38:23
【问题描述】:

我在 phpmyadmin 中有以下代码:

CREATE TABLE IF NOT EXISTS Leaderboard2 (Player text, Score float)

当我在 python 中运行相同的代码时(我希望能够更改排行榜):

make_leaderboard = ("CREATE TABLE IF NOT EXISTS Leaderboard%s (Player text, Score float)")
cursor.execute(make_leaderboard, *Leaderboard_Number*)

它出现了这个错误:

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 '%s (Player text, Score float)' at line 1

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: python mysql phpmyadmin mysql-python mysql-connector


    【解决方案1】:

    因为您在表名上使用了%s,这将不起作用并在您的 sql 中创建错误 将变量添加为表名的最佳方法是使用 formatconcate 这样的字符串

    make_leaderboard = "CREATE TABLE IF NOT EXISTS Leaderboard{num} (Player text, Score float)"
    cursor.execute(make_leaderboard.format(num=Leaderboard_Number))
    

    python DB API 中的参数替换('%s') 仅用于值 - 不是表格或字段。

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多