gpt4 book ai didi

android - ORMLite 无法创建第二个表

转载 作者:行者123 更新时间:2023-11-29 22:01:48 25 4
gpt4 key购买 nike

我将数据从 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

所以问题是无法创建“级别”表。不明白为什么如果我对类别表使用相同的机制..


经过一些分析,我明白问题出在这个方法中

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)

请帮忙...

最佳答案

所以 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);
}
}

关于android - ORMLite 无法创建第二个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11737214/

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