0

I want to two tables in android but I see an error like does not found second table . How can I fixed? is problem about database version or onCreate method I guess?

public class dbHelper extends SQLiteOpenHelper{
  private static final String DATABASE_NAME   = "DBO_Finish";
  private static final String TABLE_USERS = "users";
  private static final String TABLE_OLCUM = "olcum";
  private static final String TABLE_BILGI="asd";

  public dbHelper(Context context,Activity act) {
        super(context, DATABASE_NAME, null, 1);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
      String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS + 
            "(id INTEGER PRIMARY KEY,user_name TEXT,user_password TEXT,olcum_id INTEGER" + ")";
      db.execSQL(sql);

      String sql2 = "CREATE TABLE IF NOT EXISTS " + TABLE_BILGI + 
            "(id INTEGER PRIMARY KEY,shortName TEXT,longName TEXT" + ")";
      db.execSQL(sql2);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BILGI);

        onCreate(db);
  }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
user3423728
  • 51
  • 1
  • 1
  • 6
  • Uninstall your app. See http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run for explanation. – laalto May 22 '14 at 11:13

2 Answers2

0

Im using method below to create my table. if u want to modify table structure like adding field , first change ur class then , increase number of dbVersion in DatabaseHelper by one. and new table will be created.

each table with specified class like this :

TblTest.java class for Tbltest :

public class TblTest {

    public static final String TABLE = "Test";
    public static final String COLUMN_ID = "id";


    public int id;

    public static final String CREATE_TABLE = "create table "
              + TABLE + "("
              + COLUMN_ID + " integer primary key"
              + ");";
    }

and "DatabaseHelper.java" class for database helper (creating table):

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DBNAME = "mytestdb";
    public SQLiteDatabase db; 
    private final static int dbVersion = 1;

    public DatabaseHelper() {
        super(AppContext.getAppContext(), DBNAME, null, dbVersion);
        open();
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
          try {
              db.execSQL(TblTest.CREATE_TABLE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          Log.w(DatabaseHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
          db.execSQL("DROP TABLE IF EXISTS " + TblTest.TABLE);
          onCreate(db);
      }

      public void open() throws SQLException {

         db = getWritableDatabase();

      }

      public void close() {
          db.close();
      }


}
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
0

The 3rd argument of your constructor is a number indicating in which version of the DB are you. Basically every time you call your constructor with that number, the Android OS checks if there is already a DB with that version, if that's the case nothing more happens, but if the DB doesn't exists or the version registered to it is lower, then your onUpgrade method will be called.

So if you want to add new tables, simply change that number to '2' in this case, and increment it every time you want to create new tables.

Hope that helps, greetings!

Aballano
  • 1,037
  • 12
  • 19