I’ve gone through many examples here, but all seem complicated and most of them are for large data. I’m new to both Android and also SQL. What I want to do is just pre-populate my SQL database with some data.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, type TEXT, notes TEXT, feed TEXT, lat REAL, lon REAL);");
ContentValues cv=new ContentValues();
cv.put("name", "Hi");
cv.put("address", "There");
cv.put("type", "delivery");
cv.put("notes", "");
cv.put("feed", "");
cv.put("lat", "");
cv.put("lon", "");
getWritableDatabase().insert("restaurants", "name", cv);
}
There is no error but unfortunately no data is inserted as well. This should be very simple, please help me?
Your approach is fine, inside
SQLiteOpenHelper#onCreate()is the right place to put initial data since that is where you create the database in the state you like it to be.But inside
onCreateyou usedb.insert("restaurants", "name", cv)directly. You are not finished to create the writable database you try to get viagetWritableDatabase()at that point. Not sure if that is the problem but it could be.