Peewee 简明教程

Peewee - Insert a New Record

在 Peewee 中,有多个命令可以通过这些命令,可以在表中添加新的记录。我们已经使用了 Model 实例的 save() 方法。

In Peewee, there are more than one commands by which, it is possible to add a new record in the table. We have already used save() method of Model instance.

rec1=User(name="Rajesh", age=21)
rec1.save()

Peewee.Model 类还具有 a create() 方法,该方法创建一个新实例并在表中添加其数据。

The Peewee.Model class also has a create() method that creates a new instance and add its data in the table.

User.create(name="Kiran", age=19)

除此之外,Model 还具有 insert() 作为构造 SQL insert 查询对象的类方法。查询对象的 execute() 方法执行为基础表中的行添加操作。

In addition to this, Model also has insert() as class method that constructs SQL insert query object. The execute() method of Query object performs adding a row in underlying table.

q = User.insert(name='Lata', age=20)
q.execute()

查询对象相当于 INSERT 查询。q.sql() 返回查询字符串。

The query object is an equivalent INSERT query.q.sql() returns the query string.

print (q.sql())
('INSERT INTO "User" ("name", "age") VALUES (?, ?)', ['Lata', 20])

以下是演示使用上述插入记录方式的完整代码。

Here is the complete code that demonstrates the use of above ways of inserting record.

from peewee import *
db = SqliteDatabase('mydatabase.db')
class User (Model):
   name=TextField()
   age=IntegerField()
   class Meta:
      database=db
      db_table='User'

db.create_tables([User])
rec1=User(name="Rajesh", age=21)
rec1.save()
a=User(name="Amar", age=20)
a.save()
User.create(name="Kiran", age=19)
q = User.insert(name='Lata', age=20)
q.execute()
db.close()

我们可以在 SQLiteStudio GUI 中验证结果。

We can verify the result in SQLiteStudio GUI.

sqlitestuidio gui

Bulk Inserts

为了在表中一次使用多行,Peewee 提供两种方法: bulk_create 和 insert_many。

In order to use multiple rows at once in the table, Peewee provides two methods: bulk_create and insert_many.

insert_many()

insert_many() 方法生成等效 INSERT 查询,使用字典对象列表,每个列表都具有一个对象的一个字段值对。

The insert_many() method generates equivalent INSERT query, using list of dictionary objects, each having field value pairs of one object.

rows=[{"name":"Rajesh", "age":21}, {"name":"Amar", "age":20}]
q=User.insert_many(rows)
q.execute()

这里也是如此,q.sql() 返回 INSERT 查询字符串,如下获取 −

Here too, q.sql() returns the INSERT query string is obtained as below −

print (q.sql())
('INSERT INTO "User" ("name", "age") VALUES (?, ?), (?, ?)', ['Rajesh', 21, 'Amar', 20])

bulk_create()

此方法采用一个列表参数,其中包含映射到表的模型的一个或多个未保存实例。

This method takes a list argument that contains one or more unsaved instances of the model mapped to a table.

a=User(name="Kiran", age=19)
b=User(name='Lata', age=20)
User.bulk_create([a,b])

以下代码使用两种方法执行批量插入操作。

Following code uses both approaches to perform bulk insert operation.

from peewee import *
db = SqliteDatabase('mydatabase.db')
class User (Model):
   name=TextField()
   age=IntegerField()
   class Meta:
      database=db
      db_table='User'

db.create_tables([User])
rows=[{"name":"Rajesh", "age":21}, {"name":"Amar", "age":20}]
q=User.insert_many(rows)
q.execute()
a=User(name="Kiran", age=19)
b=User(name='Lata', age=20)
User.bulk_create([a,b])
db.close()