繁体   English   中英

在 mysql 数据库上使用 python 插入表时 Sql 语法错误

[英]Sql syntax error when insert into table with python on mysql database

我正在尝试将此视频中的代码实现到我的 mysql 数据库中。 当我手动将值输入到 python 代码时,它可以工作。 但是,当我使用 python 字典从 gui 中获取条目时,就像视频中所说: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':b_name, :b_category, :b_status, :b_author)' at line 1 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':b_name, :b_category, :b_status, :b_author)' at line 1

在视频中,他使用 SQLite 这可能是问题,但我不确定。

我的代码在这里

# Insert Into Table
c.execute("INSERT INTO `books` (`bookName`, `bookCategory`, `BookCurrentStatus`, `bookAuthor`) VALUES (:b_name, :b_category, :b_status, :b_author)",
        {
            'b_name': b_name.get(),
            'b_category': b_category.get(),
            'b_status': b_status.get(),
            'b_author': b_author.get()
        })

我相信您实际上并没有用当前设置进行字符串替换,试试这个:

# Insert Into Table
c.execute("INSERT INTO `books` (`bookName`, `bookCategory`, `BookCurrentStatus`, `bookAuthor`) VALUES ({b_name}, {b_category}, {b_status}, {b_author})",
        {
            'b_name': b_name.get(),
            'b_category': b_category.get(),
            'b_status': b_status.get(),
            'b_author': b_author.get()
        })

MySQLdb 仅支持formatpyformat参数 styles 不支持视频中使用的named参数样式。

暂无
暂无

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

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