0

I am trying to parse A JSON from the server and the parsed objects to the SQLite on android.

SQLHandler.java

public class SQLiteHandler extends SQLiteOpenHelper {

private static final String TAG = SQLiteHandler.class.getSimpleName();


public SQLiteHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



// Create Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ")";



    String CREATE_ZONE_TABLE = "CREATE TABLE " + TABLE_ZONES + "("
            + KEY_ZONE_ID + " INTEGER," + KEY_ZONE_NAME + " TEXT" + ")";


    db.execSQL(CREATE_ZONE_TABLE);
    db.execSQL(CREATE_LOGIN_TABLE);
}

It should automatically create the zones table but it does not. When running the app I have the error that zones table does not exist. I am calling the SQLHandler from my fragment activity from the onCreateView. Why it does not create the table? (I've taken the code from my another app where the table is created after I call the SQLhandler from the onCreate method)

SendDataFragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    db = new SQLiteHandler(getActivity().getApplicationContext());
    // db.deleteZones();
    ZonesArray = new ArrayList<>();
    new GetZones().execute();
    FrameLayout rootView = (FrameLayout) inflater.inflate(R.layout.fragment_send, container, false);
...
  • If you added the zone table later, you need to recreate/upgrade your database. http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Nov 15 '16 at 13:46

1 Answers1

0

A small advice to Uninstall the application and run it again.

Hitesh Danidhariya
  • 709
  • 1
  • 7
  • 14
  • thank you! The issue was that the onCreate method from SQLite is called only once. After I've reinstalled the app, the table was successful created. –  Nov 15 '16 at 14:10