O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Tutorial database SQLite di Eclipse

895 visualizações

Publicada em

Membuat database menggunakan SQLite di Eclipse

Publicada em: Celular
  • Seja o primeiro a comentar

Tutorial database SQLite di Eclipse

  1. 1. DATABASE SQLITE 1. BUAT PROJECT BARU 2. TAMBAHKAN 1 LISTVIEW DI .XML 3. BUAT CLASS BARU DIPROJECT KAMU KLIK KANAN DI COM.EXAMPLE……, KEMUDIAN NEW, CLASS, ISI NAMANYA “DATABASE”
  2. 2. ISIKAN NAMANYA trus pilih finish, Pastikan memiliki bentuk seperti berikut:
  3. 3. 4. Buka kelas yang baru saja dibuka, tuliskan coding berikut : 1. package com.example.databasesqlite; 2. import java.util.ArrayList; 3. import android.content.ContentValues; 4. import android.content.Context; 5. import android.database.Cursor; 6. import android.database.sqlite.SQLiteDatabase; 7. import android.database.sqlite.SQLiteOpenHelper; 8. 9. public class Database extends SQLiteOpenHelper 10. { 11. private final static int DATABASE_VERSION = 1; 12. private final static String DATABASE_NAME = "TEMAN"; 13. private SQLiteDatabase DB; 14. 15. public Database (Context context) 16. { 17. super (context, DATABASE_NAME, null, DATABASE_VERSION); 18. } 19. 20. @Override 21. public void onCreate (SQLiteDatabase db) 22. { 23. createTable (db); 24. } 25. 26. public void openWritable() 27. { 28. this.DB = getReadableDatabase(); otomatis dari mentod otomatis juga, dari mentod extend sqlite open helper buat mentod dibawah nanti
  4. 4. 29. } 30. 31. public void close () 32. { 33. this.DB.close(); 34. } 35. 36. private void createTable(SQLiteDatabase db2) 37. { 38. // TODO Auto-generated method stub 39. String sql = "CREATE TABLE IF NOT EXISTS person " 40. + "(id INTEGER PRIMARY KEY, name TEXT)"; 41. db2.execSQL(sql); 42. } 43. 44. public long insertData (int id, String name) 45. { 46. ContentValues values = new ContentValues(); 47. values.put("name", name); 48. return DB.insert("person", null, values); 49. } 50. 51. public int updateData(int id, String name) 52. { 53. ContentValues values = new ContentValues(); 54. values.put("name", name); 55. return DB.update("person", values, "id = "+id, null); 56. } 57. public int deleteData (int id) 58. { 59. return DB.delete("person", "id = " +id, null); 60. } 61. 62. public ArrayList<String> getPersonList() 63. { 64. Cursor cursor = DB.rawQuery("SELECT * from person", null); 65. ArrayList<String> list = new ArrayList<String>(); 66. cursor.moveToFirst(); 67. for (int i = 0; i<cursor.getCount();i++ ) 68. { 69. list.add(cursor.getString(cursor.getColumnIndex("name"))); 70. cursor.moveToNext(); 71. } 72. return list; 73. } 74. 75. @Override 76. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  5. 5. 77. // TODO Auto-generated method stub 78. } 79. } 5. Buka main activity.java, tuiskan coding berikut 1. package com.example.databasesqlite; 2. import java.util.ArrayList; 3. import android.app.Activity; 4. import android.os.Bundle; 5. import android.view.Menu; 6. import android.view.MenuItem; 7. import android.widget.ArrayAdapter; 8. import android.widget.ListView; 9. 10. public class MainActivity extends Activity { 11. 12. @Override 13. protected void onCreate(Bundle savedInstanceState) { 14. super.onCreate(savedInstanceState); 15. setContentView(R.layout.activity_main); 16. 17. Database data = new Database(this); 18. data.openWritable(); 19. 20. //mengambil data dari database 21. ArrayList<String> listdata = data.getPersonList(); 22. 23. //periksa apakah ada data, jika tidak ada maka dilakukan inisialisasi data 24. if (listdata.size()<1) 25. { 26. //inisialisasi data dalam database 27. data.insertData(1, "Bayu"); 28. data.insertData(2, "Candra"); 29. data.insertData(3, "Dika"); 30. data.insertData(4, "Erna"); 31. data.insertData(5, "Farah"); 32. 33. listdata = data.getPersonList(); 34. } 35. 36. //perubahan data 37. data.updateData(1, "Bayu Wono"); 38. listdata = data.getPersonList(); 39. 40. //penghapusan data 41. data.deleteData(1); 42. listdata = data.getPersonList();
  6. 6. 43. 44. data.close(); 45. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 46. android.R.layout.simple_list_item_1,listdata); 47. 48. ListView listView = (ListView) findViewById(R.id.listViewTampilData); 49. listView.setAdapter(adapter); 50. } 51. 52. @Override 53. public boolean onCreateOptionsMenu(Menu menu) { 54. // Inflate the menu; this adds items to the action bar if it is present. 55. getMenuInflater().inflate(R.menu.main, menu); 56. return true; 57. } 58. 59. @Override 60. public boolean onOptionsItemSelected(MenuItem item) { 61. // Handle action bar item clicks here. The action bar will 62. // automatically handle clicks on the Home/Up button, so long 63. // as you specify a parent activity in AndroidManifest.xml. 64. int id = item.getItemId(); 65. if (id == R.id.action_settings) { 66. return true; 67. } 68. return super.onOptionsItemSelected(item); 69. } 70. }

×