批量插入 Sugar ORM 不起作用
Bulk insert with Sugar ORM not working
我的 Android 应用程序中有一个数据库,安装应用程序时必须包含一些预定义的数据。为此,我正在尝试 "Bulk Insert" 使用 Sugar ORM。 SugarORM 文档中的示例代码似乎在语法和实现方面都有一些错误,或者不完整!有谁知道插入批量数据的实用方法吗?
文档代码:
List<Book> books = new ArrayList<>();
books.add(new Book("isbn123", "Title here", "2nd edition"))
books.add(new Book("isbn456", "Title here 2", "3nd edition"))
books.add(new Book("isbn789", "Title here 3", "4nd edition"))
SugarRecord.saveInTx(books);
SugarRecord 中似乎没有 saveInTx class!
我的代码:
import com.orm.SugarRecord;
import com.orm.dsl.Table;
import com.orm.dsl.Unique;
import java.util.ArrayList;
import java.util.List;
@Table()
public class Foods extends SugarRecord{
@Unique
private String foodName;
private String calorie;
public Foods(String foodName, String calorie) {
this.foodName = foodName;
this.calorie = calorie;
}
public void putData(){
List<Foods> foods = new ArrayList<>();
foods.add(new Foods("Lamb Chops", "12"));
foods.add(new Foods("Onion", "32"));
foods.add(new Foods("Apple", "43"));
SugarRecord.saveInTx(foods);
}
}
你不能这样说 似乎 SugarRecord class! 中没有 saveInTx,SugarRecord
中有一个方法 class即
@SuppressWarnings("deprecation")
public static <T> void saveInTx(Collection<T> objects) {
SQLiteDatabase sqLiteDatabase = getSugarContext().getSugarDb().getDB();
try {
sqLiteDatabase.beginTransaction();
sqLiteDatabase.setLockingEnabled(false);
for (T object: objects) {
save(object);
}
sqLiteDatabase.setTransactionSuccessful();
} catch (Exception e) {
Log.i("Sugar", "Error in saving in transaction " + e.getMessage());
} finally {
sqLiteDatabase.endTransaction();
sqLiteDatabase.setLockingEnabled(true);
}
}
我已经试过了,效果很好。只需 在保存记录时使用 "Sugar" 关键字 过滤您的 Logcat。你会发现类似 I/Sugar: Foods saved : 1
您还可以查看在 table 中添加的计数:
long foodCount= SugarRecord.count(Foods.class);
Log.i("Foods", "Foods " + foodCount);
因此,如果您没有通过 SugarRecord.listAll(oods.class)
、
获取数据
So the only problem is you just miss an empty constructor at the model class
尝试添加空构造函数并重试。也删除你以前的数据。
希望对您有所帮助!!!
我的 Android 应用程序中有一个数据库,安装应用程序时必须包含一些预定义的数据。为此,我正在尝试 "Bulk Insert" 使用 Sugar ORM。 SugarORM 文档中的示例代码似乎在语法和实现方面都有一些错误,或者不完整!有谁知道插入批量数据的实用方法吗?
文档代码:
List<Book> books = new ArrayList<>();
books.add(new Book("isbn123", "Title here", "2nd edition"))
books.add(new Book("isbn456", "Title here 2", "3nd edition"))
books.add(new Book("isbn789", "Title here 3", "4nd edition"))
SugarRecord.saveInTx(books);
SugarRecord 中似乎没有 saveInTx class!
我的代码:
import com.orm.SugarRecord;
import com.orm.dsl.Table;
import com.orm.dsl.Unique;
import java.util.ArrayList;
import java.util.List;
@Table()
public class Foods extends SugarRecord{
@Unique
private String foodName;
private String calorie;
public Foods(String foodName, String calorie) {
this.foodName = foodName;
this.calorie = calorie;
}
public void putData(){
List<Foods> foods = new ArrayList<>();
foods.add(new Foods("Lamb Chops", "12"));
foods.add(new Foods("Onion", "32"));
foods.add(new Foods("Apple", "43"));
SugarRecord.saveInTx(foods);
}
}
你不能这样说 似乎 SugarRecord class! 中没有 saveInTx,SugarRecord
中有一个方法 class即
@SuppressWarnings("deprecation")
public static <T> void saveInTx(Collection<T> objects) {
SQLiteDatabase sqLiteDatabase = getSugarContext().getSugarDb().getDB();
try {
sqLiteDatabase.beginTransaction();
sqLiteDatabase.setLockingEnabled(false);
for (T object: objects) {
save(object);
}
sqLiteDatabase.setTransactionSuccessful();
} catch (Exception e) {
Log.i("Sugar", "Error in saving in transaction " + e.getMessage());
} finally {
sqLiteDatabase.endTransaction();
sqLiteDatabase.setLockingEnabled(true);
}
}
我已经试过了,效果很好。只需 在保存记录时使用 "Sugar" 关键字 过滤您的 Logcat。你会发现类似 I/Sugar: Foods saved : 1
您还可以查看在 table 中添加的计数:
long foodCount= SugarRecord.count(Foods.class);
Log.i("Foods", "Foods " + foodCount);
因此,如果您没有通过 SugarRecord.listAll(oods.class)
、
So the only problem is you just miss an empty constructor at the model class
尝试添加空构造函数并重试。也删除你以前的数据。
希望对您有所帮助!!!