public Database(@NonNull Context _context, @NonNull String _tableName){
super(_context, DB_NAME, null, DB_VERSION);
this.mContext = _context;
this.tableName = _tableName;
this.db_create = "CREATE TABLE " + tableName + "(" + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " +
COLUMN_DATA + " BLOB )";
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
Log.d("tableCreate", "onCreate: created");
db.execSQL(db_create);
}catch(SQLiteException ex){
ex.printStackTrace();
Log.d("TableCreation", "onCreate: cannot create");
}
}
public long insert(Model model){
SQLiteDatabase db = getWritableDatabase();
long id = -1;
try{
if(maxData > 0 || maxData <= counter){
ContentValues value = new ContentValues();
value.put(COLUMN_ID, model.getCode());
value.put(COLUMN_DATA, model.toJson().getBytes());
counter += 1;
id = db.insert(tableName, null, value); //exception is thrown here.
}
}catch(SQLiteException ex){
ex.printStackTrace();
}finally{
if(db != null)
db.close();
}
return id;
}
}
I have a Database class which extends SQLiteHelper. Here an exception is always thrown whenever i attempt to insert an item in the table. Error message
no such table: university_basic
08-28 23:26:23.036 30754-30786/com.ujjwal.univhub E/SQLiteDatabase: Error inserting ...
android.database.sqlite.SQLiteException: no such table: university_basic (code 1): , while compiling: INSERT INTO university_basic(id,model) VALUES (?,?)
As from the documentation and other online resources i found that onCreate()
method is called when getWriteableDatabase()
or getReadableDatabase()
is invoked and the table doesn't exist. I had a call to getWriteableDatabase()
inside insert()
method which presumably should create table if not exists or open database. But error msg is thrown everytime. Instance of Database is created in one of the Activity of my application.
Connectivity.OfflineDataLoader offlineLoader = connectivity.new OfflineDataLoader(this,
"university_basic", 30);
connectivity.enableOfflineData(offlineLoader);
One of the constructor of OfflineLoader is
public OfflineDataLoader(@NonNull Context _context, String tableName,int maxData){
this.context = _context;
this.tableName = (tableName != null)? tableName : "university_basic";
this.database = new Database(_context, this.tableName);
this.database.setMaxData(maxData);
}
Why table is not created here??