简体   繁体   English

Android ORMLite DbHelper onCreate()甚至在卸载后都没有调用

[英]Android ORMLite DbHelper onCreate() not called even after uninstall

I'm getting this error when I run my app for the first time after re-install: android.database.sqlite.SQLiteException: no such table 我在重新安装后第一次运行我的应用程序时收到此错误:android.database.sqlite.SQLiteException:没有这样的表

(This error happens when my app tries to read from the database) (当我的应用尝试从数据库中读取时会发生此错误)

For some reason the onCreate() method in DBHelper is not getting called and therefore the tables are not getting created. 由于某种原因,DBHelper中的onCreate()方法没有被调用,因此没有创建表。 I followed the advice from other question and tried calling getWritableDatabase(), also tried a create() call to insert data in some table, but still no luck: onCreate is never called. 我按照其他问题的建议尝试调用getWritableDatabase(),也试过一个create()调用在一些表中插入数据,但仍然没有运气:onCreate永远不会被调用。

I got it to work however by changing the DATABASE_VERSION value to 2. But that doesn't make sense since this is a brand new installation after uninstall. 我通过将DATABASE_VERSION值更改为2来实现它。但这没有意义,因为这是卸载后的全新安装。 Also I found that before the SQL read error the database got created but it has only 1 table "android_metadata" (not created by me). 另外我发现在SQL读取错误之前数据库已经创建但是它只有一个表“android_metadata”(不是由我创建的)。 I'm posting some code here for reference 我在这里发布一些代码供参考

 public class DatabaseHelper extends OrmLiteSqliteOpenHelper{ private static final String DATABASE_NAME = "RoutePlanner.db"; private static final int DATABASE_VERSION = 1; private Dao<Trip, Integer> tripDAO = null; private RuntimeExceptionDao<Trip, Integer> tripRunTimeDAO = null; ... } @Override public SQLiteDatabase getWritableDatabase() { return super.getWritableDatabase(); } public DatabaseHelper(Context context){ super(context, DATABASE_NAME,null, DATABASE_VERSION, R.raw.ormlite_config); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource source) { try { Log.i(DatabaseHelper.class.getSimpleName(), "onCreate"); TableUtils.createTable(source, Trip.class); ... } catch (SQLException ex) { Log.e(DatabaseHelper.class.getSimpleName(), "Error creating db", ex); throw new RuntimeException(ex); } } 

OK, I found the problem, hope this explanations helps others on what NOT to do. 好的,我发现了问题,希望这些解释可以帮助别人做什么不做。 The issue was that I had a separate calendar module which I wanted to access my Database. 问题是我有一个单独的日历模块,我想访问我的数据库。 To make things 'simpler' I created a separate DatabaseHelper on that module to access the same SQLite databse as my main module. 为了使事情“更简单”,我在该模块上创建了一个单独的DatabaseHelper,以访问与我的主模块相同的SQLite数据库。 The existance of the 2nd DatabaseHelper was causing all my issues. 第二个DatabaseHelper的存在引起了我的所有问题。 Solutions are either join the 2 modules into one, or use a Database Service Provider 解决方案要么将2个模块合并为一个,要么使用数据库服务提供商

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM