khirulnizam

MyDB.java

Dec 16th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //package com.khirulnizam.placeofinterest;
  2.  
  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6.  
  7. public class MyDB extends SQLiteOpenHelper {
  8. final protected static String DATABASE_NAME="POI";//define DB name
  9. public MyDB(Context context) {
  10. super(context, DATABASE_NAME, null,1);
  11. }
  12. @Override
  13. public void onCreate(SQLiteDatabase db) {//1st time install auto create DB
  14.  
  15. //here is the database definition
  16. //or some data to insert
  17.  
  18. //CREATE TABLE pbentry ((id, entri, makna, contoh, englishproverb, englishmeaning, sumber, kategori);
  19. db.execSQL("CREATE TABLE lokasi (" +
  20. "namalokasi TEXT," +
  21. "latitude REAL," +
  22. "longitude REAL);");
  23.  
  24. //data entry
  25. //Eg: db.execSQL("INSERT INTO orang (nama, jantina, negeri) VALUES(‘Khirulnizam', ‘lelaki‘, ‘Negeri Sembilan’);");
  26. db.execSQL("INSERT INTO lokasi (namalokasi, latitude, longitude) " +
  27. "values ('KUIS', '2.890484', '101.788470')");
  28. db.execSQL("INSERT INTO lokasi (namalokasi, latitude, longitude) " +
  29. "values ('Masjid Shah Alam', '3.078622', '101.520718')");
  30. db.execSQL("INSERT INTO lokasi (namalokasi, latitude, longitude) " +
  31. "values ('Zoo Negara', '3.209096', '101.758233')");
  32. }
  33. @Override
  34. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  35. if (newVersion > oldVersion){//renew DB if DB upgraded to new version
  36. //do something if database is updated
  37. db.execSQL("DROP TABLE IF EXISTS pbentry");
  38. onCreate(db);
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment