简体   繁体   English

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

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

I'm trying to implement the code on this video to my mysql database.我正在尝试将此视频中的代码实现到我的 mysql 数据库中。 When I enter the values by hand to python code it works.当我手动将值输入到 python 代码时,它可以工作。 However when i use python dictionaries for getting entries from gui like the video it's says: 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但是,当我使用 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

On video he uses SQLite this can be the problem but i'm not sure.在视频中,他使用 SQLite 这可能是问题,但我不确定。

my code is here我的代码在这里

# 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()
        })

I believe that you are not actually doing string replacement with your current setup, try this:我相信您实际上并没有用当前设置进行字符串替换,试试这个:

# 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 only supports the format and pyformat parameter styles . MySQLdb 仅支持formatpyformat参数 styles The named parameter style used in the video isn't supported.不支持视频中使用的named参数样式。

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

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