I
I
Ivan Bogomolov2014-08-16 11:23:55
Java
Ivan Bogomolov, 2014-08-16 11:23:55

How to fix android no such table error?

I rummaged through stackoverflow and everywhere everything is simple for everyone. We change DB_VERSION, check it in the onUpgrade method, delete the table and create it again (or just add new fields).
I tried deleting the app, clearing the data (clear data in apps manager), deleting the database from /data/data/myapp/databases/ , trying renaming the database (test,test.db,new.db,new, etc.) .
The database is created, everything is ok, all tables are created (updated), but when I insert data db.insert(TABLE_NAME,null,contentvalues) , I get an error no such table table which should not be in the database. (In the old scheme there was a users table)
I have a feeling that there is some kind of cache that stores the values ​​​​of variables and it strictly preserved the name of the table. Below is a code example.
Base manager code:

class Database extends SQLiteOpenHelper {

  private static final String TAG = "Database";

  private static final int DB_VERSION = 1;
  	private static final String DB_NAME = "notedb";

  	public static final String TABLE_NAME = "notes";
  	public static final String NOTE_FIELD = "note";
  	
  public Database(Context context) 
  {
    super(context, DB_NAME, null,DB_VERSION);
  }

  /** it's hooks invoke when getWritableDatabase called **/
  @Override
  public void onCreate(SQLiteDatabase db) 
  {
    
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + 
          " ( _id integer primary key autoincrement, "
      			  + NOTE_FIELD + " TEXT)");
    Log.i(TAG,"Database create " + DB_NAME);
  }

  @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       if(oldVersion < DB_VERSION)
       {
    // Logs that the database is being upgraded
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
               + newVersion + ", which will destroy all old data");

        // Kills the table and existing data
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME );
        db.execSQL("DROP TABLE IF EXISTS users");
        // Recreates the database with a new version
        onCreate(db);       	
       }
       
   }
}

Handler code:
/* Java - событие на onClick */
Database databaseInstance = new Database(MainActivity.this);
SQLiteDatabase db = databaseInstance.getWritableDatabase();
ContentValues valuesStorage = new ContentValues();
/* prepare */
valuesStorage.put(databaseInstance.NOTE_FIELD,resultsTmp );
db.insert(databaseInstance.TABLE_NAME,null,valuesStorage);
db.close();

How to get rid of this error?
ENV: I check on the emulator through android avd , I work with the emulator through the adb shell. There is a device nexus 7 2013 and a Sony phone, they have the same thing. The project build goes through ant debug, adb install -r bin/mysppname-debug.apk. OS Ubuntu 12.04

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Bogomolov, 2014-08-24
@kraso4niy

Solution: ant clear in the terminal
As a result, the build commands look like this:
ant clear
ant debug
adb install -r bin/yourapp-debug.apk

I
Ivan Balaksha, 2014-08-17
@tagantroy

I skimmed through the code, everything seems to be correct. Are you testing on an emulator? Create a completely new image (if using genymotion) and check there.
PS Working with the database in the onClick method is such a practice, make a DAO, in which you put all the functionality of working with the database. I recently wrote a note-taking application, and in parallel got the first experience with SQLite. take a look at github, maybe it will be useful https://github.com/tagantroy/Notes/tree/master/app...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question