如何指定 table 用于使用 peewee 插入数据?
How to specify table for inserting data using peewee?
我使用以下代码在数据库中插入数据:
db = dbConnection("localhost", "root", "")
class Products(peewee.Model):
article = peewee.TextField()
name = peewee.TextField()
price = peewee.TextField()
price_distributor = peewee.TextField()
price_discount = peewee.TextField()
class Meta:
database = db
Products.create_table()
book = Products(article="1", name="2", price="3", price_distributor="4", price_discount="5")
book.save()
以及连接函数:
def dbConnection(host, user, password):
return peewee.MySQLDatabase("test", host=host, port=3306, user=user, passwd=password)
如何设置插入数据的table名称?
如果您查看 peewee model options
上的文档
您可以在 class Meta
中指定一个名为 db_table
的选项,它是 table 存储数据的名称。
我使用以下代码在数据库中插入数据:
db = dbConnection("localhost", "root", "")
class Products(peewee.Model):
article = peewee.TextField()
name = peewee.TextField()
price = peewee.TextField()
price_distributor = peewee.TextField()
price_discount = peewee.TextField()
class Meta:
database = db
Products.create_table()
book = Products(article="1", name="2", price="3", price_distributor="4", price_discount="5")
book.save()
以及连接函数:
def dbConnection(host, user, password):
return peewee.MySQLDatabase("test", host=host, port=3306, user=user, passwd=password)
如何设置插入数据的table名称?
如果您查看 peewee model options
上的文档您可以在 class Meta
中指定一个名为 db_table
的选项,它是 table 存储数据的名称。