I am new in android development so the code is not completed yet....
When I created my DB and insert some items inside with using contentValues in the onCreate() method but after the app is running, I tried to read my DB but it was empty.
That is the first time to launch my app so it will use the onCreate() method but unfortunately not inserting.
That is my code here...
package com.example.axel.spotopia;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.security.PublicKey;
public class SpotopiaDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "spotopia";
private static final int DB_VESION = 1;
public SpotopiaDBHelper(Context context) {
super(context, DB_NAME, null, DB_VESION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
updateMyDB(db, 0, DB_VESION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
updateMyDB(db, oldVersion, newVersion);
}
private void insertSpot(SQLiteDatabase db, String name, String address, String note, String rank) {
ContentValues content = new ContentValues();
content.put("NAME", name);
content.put("ADDRESS", address);
content.put("NOTE", note);
content.put("RANK", rank);
db.insert(DB_NAME, null, content);
}
private void updateMyDB(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (oldVersion < 1) {
db.execSQL("CREATE TABLE SPOT ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "NAME TEXT, "
+ "ADDRESS TEXT, "
+ "NOTE TEXT, "
+ "RANK REAL);");
insertSpot(db, "jaz", null, null, "8");
insertSpot(db, "jaz1", null, null, "9");
insertSpot(db, "jaz2", null, null, "10");
}
}
}