gpt4 book ai didi

android - 我应该总是在 android sqlite 中使用事务吗?

转载 作者:IT王子 更新时间:2023-10-29 06:28:40 25 4
gpt4 key购买 nike

这是一个简单的例子:

public boolean containsId(Long userid) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + getTableName() + " where id = " + userid, null);
boolean rows = cursor.getCount() > 0;
db.close();
return rows;
}

我以为 sqlite 会自动启动一个事务。我的同事说我必须始终开始交易。

那么什么是正确的模式呢?如果我从数据库中读取数据,是否应该启动一个事务?我确信此时没有其他线程会在这个表中写入。

最佳答案

No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last query finishes.

Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command. But a transaction will also ROLLBACK if the database is closed or if an error occurs and the ROLLBACK conflict resolution algorithm is specified.

SQLite Query Language .

是的,交易会自动开始。但是,如果您在不手动启动事务的情况下执行多个查询,则会启动和完成多个事务,这会对性能产生负面影响。
此外,自动回滚机制仅在失败时回滚单个查询。

关于android - 我应该总是在 android sqlite 中使用事务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29722155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com