Android Open Source - Smartlab Database Adapter






From Project

Back to project page Smartlab.

License

The source code is released under:

Apache License

If you think the Android project Smartlab listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.listviewexample.data;
//from  www  . ja v a2 s. c om
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAdapter {

  DatabaseOpenHelper helper;

  public DatabaseAdapter(Context context) {
    helper = new DatabaseOpenHelper(context, "books_db.db", null, 1);
  }

  public ArrayList<Book> getAllBooks() {
    ArrayList<Book> result = null;
    SQLiteDatabase db = null;
    Cursor c = null;

    try {

      db = helper.getReadableDatabase();
      c = db.query("BOOKS", new String[] { "*" }, null, null, null, null,
          null);

      if (c.moveToFirst()) {
        result = new ArrayList<Book>();
        do {
          Long id = c.getLong(c.getColumnIndex("ID"));
          String name = c.getString(c.getColumnIndex("NAME"));
          String author = c.getString(c.getColumnIndex("AUTHOR"));
          result.add(new Book(id, name, author));
        } while (c.moveToNext());
      }

    } catch (Exception e) {
      
    } finally {
      if (db.isOpen()) {
        db.close();
      }
      if (c != null) {
        c.close();
      }
    }

    return result;
  }

  class DatabaseOpenHelper extends SQLiteOpenHelper {

    public DatabaseOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
      super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table BOOKS (ID integer primary key, NAME text, AUTHOR text)");

      ContentValues cv = new ContentValues();
      for (int i = 0; i < 20; i++) {
        cv.put("NAME", "NAME " + i);
        cv.put("AUTHOR", "AUTHOR " + i);
        db.insert("BOOKS", null, cv);
        cv.clear();
      }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

  }

}




Java Source Code List

com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.SecondActivity.java
com.example.analyticstest.MainActivity.java
com.example.androidgraphic.MainActivity.java
com.example.animationexample.MainActivity.java
com.example.animationexample.SecondActivity.java
com.example.compassview.CompassView.java
com.example.compassview.MainActivity.java
com.example.databasetest.BookListActivity.java
com.example.databasetest.MainActivity.java
com.example.databasetest.data.Book.java
com.example.databasetest.data.DatabaseAdapter.java
com.example.fragmentexample.BookDetailsFragment.java
com.example.fragmentexample.BookListFragment.java
com.example.fragmentexample.MainActivity.java
com.example.fragmentexample.data.BookListAdapter.java
com.example.fragmentexample.data.Book.java
com.example.fragmentexample.data.DatabaseAdapter.java
com.example.listviewexample.MainActivity.java
com.example.listviewexample.data.BookListAdapter.java
com.example.listviewexample.data.Book.java
com.example.listviewexample.data.DatabaseAdapter.java
com.example.persiandatepicker.MainActivity.java
com.example.persiandatepicker.PersianDatePicker.java
com.example.preferencesexample.MainActivity.java
com.example.sqlitetestapp.MainActivity.java
com.example.sqlitetestapp.PersonListActivity.java
com.example.sqlitetestapp.data.DatabaseAdapter.java
com.example.sqlitetestapp.data.PersonListAdapter.java
com.example.sqlitetestapp.data.model.Person.java
com.example.viewpagerexample.MainActivity.java
ir.smartlab.android.broadcast.MainActivity.java
ir.smartlab.android.broadcast.WifiStateChangeReceiver.java
ir.smartlab.android.downloadasynctask.MainActivity.java
ir.smartlab.android.helloworld.MainActivity.java
ir.smartlab.audiorecord.MainActivity.java
ir.smartlab.audiotest.MainActivity.java
ir.smartlab.camera.MainActivity.java
ir.smartlab.captureimage.MainActivity.java
ir.smartlab.customaudiorecorder.MainActivity.java
ir.smartlab.java.ch04.firstcup.FisrtCup.java
ir.smartlab.java.ch05.circlesample.Circle.java
ir.smartlab.java.ch05.circlesample.Main.java
ir.smartlab.java.ch06.simplecalculator.SimpleCalculator.java
ir.smartlab.java.ch06.variablescope.VariableScopeTest.java
ir.smartlab.java.persiancalendar.Main.java
ir.smartlab.musicplayer.MainActivity.java
ir.smartlab.musicplayerservice.MainActivity.java
ir.smartlab.musicplayerservice.PlayerService.java
ir.smartlab.personlist.MainActivity.java
ir.smartlab.personlist.PersonListActivity.java
ir.smartlab.personlist.data.DatabaseAdapter.java
ir.smartlab.personlist.data.PersonListAdapter.java
ir.smartlab.personlist.data.Person.java