0
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??

ujjwal mainali
  • 379
  • 1
  • 5
  • 17
  • Well, the error clearly states that the table that you are using wasn't created, so please show the code that you used to make that "university_basic" table. – OneCricketeer Aug 28 '16 at 18:07
  • I created the instance of Database in my MainActivity class.This should have created the table when the insert() method is invoked. – ujjwal mainali Aug 28 '16 at 18:10
  • Can you please [edit] your question to include the `new Database()` line of the Activity so we may try to reproduce the problem? Here on StackOverflow, we prefer your question is a [mcve]. Your question may be minimal, but it isn't complete / verifiable. – OneCricketeer Aug 28 '16 at 18:15
  • Also, if you aren't seeing the Log statement that your table is created, then the problem may be before the exception is thrown – OneCricketeer Aug 28 '16 at 18:16
  • "onCreate() method is called when getWriteableDatabase() or getReadableDatabase() is invoked and the table doesn't exist" - incorrect, called if the *database* did not exist. Also, `onCreate` should not swallow exceptions. – laalto Aug 28 '16 at 18:20
  • I can't see either of the log message. – ujjwal mainali Aug 28 '16 at 18:22
  • Yes because the database already exists but without the table. You can uninstall your app to make `onCreate()` execute again. See http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run for more. – laalto Aug 28 '16 at 18:46
  • laalto thanks, that was exactly the problem. – ujjwal mainali Aug 28 '16 at 18:57

2 Answers2

0

In your main activity, initiate your database helper class by calling the constructor.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       
    mHelper = new MyDbHelper(this); }

Now within your database manager class under onCreate you add in the code to create your database.

public class MyDbHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "people";
public static final String COL_NAME = "pName";
public static final String COL_DATE = "pDate";
private static final String STRING_CREATE = "CREATE TABLE "
        + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_NAME + " TEXT, " + COL_DATE + " DATE);";


public MyDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION);}

@Override
public void onCreate(SQLiteDatabase db) {
    //Create the database table
    db.execSQL(STRING_CREATE);
    //You may also load initial values into the database here
    ContentValues cv = new ContentValues(2);
    cv.put(COL_NAME, "John Doe");
    //Create a formatter for SQL date format
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    cv.put(COL_DATE, dateFormat.format(new Date())); //Insert 'now' as the date
    db.insert(TABLE_NAME, null, cv);
  }

}
Nate
  • 21
  • 6
  • Nate, first you have to read and then answer. Is my version of code is any way different than yours? – ujjwal mainali Aug 28 '16 at 18:29
  • Do you call you the database in you main activity to check if the database and table are created? That is the only difference that I can see is if the table isn't created as the app first starts the table won't exists as you try calling it. Make an initial setup call from `MainActivity` `onCreate`. – Nate Aug 29 '16 at 04:34
  • All i can say is read carefully i included that too. – ujjwal mainali Aug 29 '16 at 08:29
0

The very problem was that the database exits but without the table.So uninstalling and reinstalling the app solved the problem.

ujjwal mainali
  • 379
  • 1
  • 5
  • 17