mysql python peewee您的SQL语法中存在错误;

djp7away  于 2023-02-18  发布在  Mysql
关注(0)|答案(1)|浏览(119)

我有一个保存电报机器人信息的模型

class Bots(BaseModel):
    bot_token = CharField(max_length=500, unique=True)
    username = CharField(max_length=32, null=True)
    admin_id = ForeignKeyField(Users, Users.user_id,    
                               on_delete='CASCADE')
 
    @classmethod
    def insert_bot(cls, token, username, admin_id):
        q = cls.insert(bot_token=token, username=username, 
                       admin_id=admin_id).on_conflict_ignore(ignore=True)
        q.execute()
        return q

如果我们打印q,我们会收到以下查询

INSERT IGNORE INTO `bots` (`bot_token`, `username`, `admin_id`) VALUES ('5500232067:AAHkXEzVHb-9Vvsw4bAKWfFZatQyQhVfIco','vdp_ka_1234_bot', 5366819345)

如果我们在mysqlshell中执行这个查询,它将被执行而不会出错,但是我在peewee中得到了错误
错误讯息

During handling of the above exception, another exception occurred:
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\python-projrct\upload_file\venv\Lib\site-packages\pymysql\connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "D:\projects\python-projrct\upload_file\venv\Lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\projects\python-projrct\upload_file\venv\Lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
peewee.ProgrammingError: (1064, "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 'IGNORE INTO `bots` (`bot_token`, `username`, `admin_id`) VALUES ('5500232067:AAH' at line 1")
nvbavucw

nvbavucw1#

似乎对我很有效:

db = MySQLDatabase('peewee_test')

class Reg(db.Model):
    key = CharField(unique=True)

db.create_tables([Reg])
Reg.create(key='k1')
Reg.insert(key='k1').on_conflict_ignore().execute()
print(len(Reg))

这将正确地打印“1”,没有错误。

相关问题