我将数据从json解析为相应的类数据,他们想将它保存在ORMLite中...这里我的类
public class Categories {
ArrayList<Category> categories;
public Categories() {
categories = new ArrayList<Category>();
}
public ArrayList<Category> getCategories() {
return this.categories;
}
}
和级别:
@DatabaseTable(tableName = "levels")
public class Level {
@SerializedName("id")
@DatabaseField(id = true)
int id;
@SerializedName("title")
@DatabaseField(dataType = DataType.STRING)
String title;
public Level() { }
public Level(int id, String title) {
this.id = id;
this.title = title;
}
}
DatabaseHandler类中的OnCreate方法:
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Category.class);
TableUtils.createTable(connectionSource, Level.class);
} catch (SQLException e){
Log.e(TAG, "error creating DB " + DATABASE_NAME);
throw new RuntimeException(e);
}
}
DatabaseHandler中的DAO方法:
public Dao<Category, Integer> getCategoryDao() throws SQLException {
if (simpleCategoryDao == null) {
simpleCategoryDao = getDao(Category.class);
}
return simpleCategoryDao;
}
public Dao<Level, Integer> getLevelDao() throws SQLException {
if (simpleLevelDao == null) {
simpleLevelDao = getDao(Level.class);
}
return simpleLevelDao;
}
public RuntimeExceptionDao<Category, Integer> getSimpleCategoryDao() {
if (categoryRuntimeDao == null) {
categoryRuntimeDao = getRuntimeExceptionDao(Category.class);
}
return categoryRuntimeDao;
}
public RuntimeExceptionDao<Level, Integer> getSimpleLevelDao() {
if (levelRuntimeDao == null) {
levelRuntimeDao = getRuntimeExceptionDao(Level.class);
}
return levelRuntimeDao;
}
我在我的Activity中以这样的方式解析JSON中的数据:
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject data = json.getJSONObject(KEY_DATA);
// getting categories
JSONArray categories = new JSONArray();
categories = data.getJSONArray(KEY_CATEGORIES);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(categories.toString()).getAsJsonArray();
Type listType = new TypeToken<List<Category>>() {}.getType();
List<Category> tasks = new ArrayList<Category>();
tasks = gson.fromJson(array.toString(), listType);
RuntimeExceptionDao<Category, Integer> simpleDao = getHelper1().getSimpleCategoryDao();
Dao<Category, Integer> categoryDao = databaseHandler.getCategoryDao();
Log.i("categoryDAO",categoryDao.queryForAll().toString());
// getting levels
JSONArray jsonlevels =new JSONArray();
jsonlevels = data.getJSONArray(KEY_LEVELS);
JsonArray levelsarray = parser.parse(jsonlevels.toString()).getAsJsonArray();
Type listlevels = new TypeToken<List<Level>>() {}.getType();
List<Level> levels = new ArrayList<Level>();
levels = gson.fromJson(levelsarray.toString(), listlevels);
Log.i("levelsgson",levels.toString());
RuntimeExceptionDao<Level, Integer> levelDao = getHelper1().getSimpleLevelDao();
Log.i("levelDAO",levelDao.toString());
Dao<Level, Integer> levelsDao = databaseHandler.getLevelDao();
Log.i("levelsDAO",levelsDao.queryForId(3).toString());
我成功获得了类别数据,但是当我想为Level DAO实例调用queryForId()时我有异常。 有这样的例外:
java.sql.SQLException: queryForOne from database failed: SELECT * FROM `levels` WHERE `id` = ?
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.android.AndroidDatabaseConnection.queryForOne(AndroidDatabaseConnection.java:169)
at com.j256.ormlite.stmt.mapped.MappedQueryForId.execute(MappedQueryForId.java:38)
at com.j256.ormlite.stmt.StatementExecutor.queryForId(StatementExecutor.java:84)
at com.j256.ormlite.dao.BaseDaoImpl.queryForId(BaseDaoImpl.java:219)
at com.assignmentexpert.LoginActivity$1.onClick(LoginActivity.java:119)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: levels: , while compiling: SELECT * FROM `levels` WHERE `id` = ?
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
at com.j256.ormlite.android.AndroidDatabaseConnection.queryForOne(AndroidDatabaseConnection.java:155)
... 15 more
所以问题是“等级”表无法创建。不明白为什么我使用与Category表相同的机制..
经过一些分析,我明白问题在于这个方法
public void saveContacts(List<Category> contacts) throws SQLException
{
OrmLiteSqliteOpenHelper dbHelper= DatabaseHandler.getInstance(_context);
Dao<Category, Integer> daoContact=dbHelper.getDao(Category.class);
QueryBuilder<Category, Integer> queryBuilder = daoContact.queryBuilder();
Log.i("dao",queryBuilder.selectColumns("title").prepare().toString());
for (Category contact : contacts) {
Log.i("dao",contact.toString());
HelperFactory.GetHelper().getCategoryDao().create(contact);
}
}
在create()行上。它抛出了一组例外:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.library.DataParsing.saveContacts(DataParsing.java:61)
at com.library.DataParsing.fillCategories(DataParsing.java:45)
at com.library.DatabaseHandler.onCreate(DatabaseHandler.java:85)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:169)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:63)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:51)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:202)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:155)
at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:113)
at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:237)
at com.assignmentexpert.LoginActivity$1.onClick(LoginActivity.java:97)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
请帮助...
答案 0 :(得分:2)
因此,当应用程序第一次运行时,会调用onCreate()
方法。如果您以后添加levels
表(或修改其架构),则需要增加数据库版本以便可以调用onUpgrade()
。
您可以在Android examples主页的ORMLite中看到典型的帮助模式。这是source code for the helper。您的助手需要以下内容。
// any time you make changes to your database objects, you may have to increase
// the database version
private static final int DATABASE_VERSION = 2;
...
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, SimpleData.class, true);
// after we drop the old databases, we create the new ones
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
答案 1 :(得分:0)
问题解决了将上下文传递给从OrmLiteSqliteOpenHelper扩展的DatabaseHandler。