如何将所有实例保存在peewee(python orm库)中的单个事务中?例如:
from peewee import SqliteDatabase, Model, TextField
DB_NAME = 'users.db'
db = SqliteDatabase(os.path.join(os.path.dirname(__file__), DB_NAME))
class Users(Model):
user_id = IntegerField(index = True)
name = TextField(null = True, index = True)
password = TextField(null = True)
description = TextField(null = True)
class Meta:
database = db
if not Drugs.table_exists():
Drugs.create_table()
data_to_save = [('user1', 'pass1'), ('user2', 'pass2'), ('user3', 'pass3')]
for user_name, user_password in data_to_save:
user_data = Users.get_or_create(name=user_name, password=user_password)
user_data.save()
如何在单个事务中重写?