如何用peewee访问远程MySQL数据库?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (550)

我想使用peewee连接到腾讯云的MySQL数据库上,。我刚开始使用数据库,所以我可能在问一些愚蠢的问题,但这正是我正在尝试的:

import peewee as pw

myDB = pw.MySQLDatabase(host="mydb.crhauek3cxfw.us-west-2.rds.amazonaws.com",port=3306,user="user",passwd="password",db="mydb")


class MySQLModel(Model):
    """A base model that will use our MySQL database"""
    class Meta:
        database = myDB

class User(MySQLModel):
    username = CharField()

myDB.connect()

它挂在第二条线上,说__init__() takes at least 2 arguments (1 given)

非常感谢

提问于
用户回答回答于

我把它变成这样,它起了作用:

import peewee as pw

myDB = pw.MySQLDatabase("mydb", host="mydb.crhauek3cxfw.us-west-2.rds.amazonaws.com", port=3306, user="user", passwd="password")

class MySQLModel(pw.Model):
    """A base model that will use our MySQL database"""
    class Meta:
        database = myDB

class User(MySQLModel):
    username = pw.CharField()
    # etc, etc


# when you're ready to start querying, remember to connect
myDB.connect()

谢谢各位

扫码关注云+社区

领取腾讯云代金券