私は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;
}
アクティビティで 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
問題は、「レベル」テーブルを作成できないことです。カテゴリテーブルと同じメカニズムを使用する理由がわかりません..
いくつかの分析の後、私は問題がこの方法にあることを理解しています
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)
それを手伝ってください...