Use SQLiteDatabase : SQLiteDatabase « Database « Android






Use SQLiteDatabase

    
package app.Test;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.TextView;

public class appTest extends Activity {
  private static String[] FROM = { "ID", "TIME", "TITLE", };
  private static String ORDER_BY = "TIME DESC";

  private EventsData events;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    events = new EventsData(this);
    try {
      addEvent("Hello, Android!");
      Cursor cursor = getEvents();
      showEvents(cursor);
    } finally {
      events.close();
    }
  }

  private void addEvent(String string) {
    SQLiteDatabase db = events.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("titme", System.currentTimeMillis());
    values.put("title", string);
    db.insertOrThrow("event", null, values);
  }

  private Cursor getEvents() {
    SQLiteDatabase db = events.getReadableDatabase();
    Cursor cursor = db.query("event", FROM, null, null, null, null,ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
  }

  private void showEvents(Cursor cursor) {
    StringBuilder builder = new StringBuilder("Saved events:\n");
    while (cursor.moveToNext()) {
      long id = cursor.getLong(0);
      long time = cursor.getLong(1);
      String title = cursor.getString(2);
      builder.append(id).append(": ");
      builder.append(time).append(": ");
      builder.append(title).append("\n");
    }
    // Display on the screen
    TextView text = (TextView) findViewById(R.id.empty);
    text.setText(builder);
  }

}

class EventsData extends SQLiteOpenHelper {
  private static final String DATABASE_NAME = "events.db";
  private static final int DATABASE_VERSION = 1;

  /** Create a helper object for the Events database */
  public EventsData(Context ctx) {
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE event (id INTEGER PRIMARY KEY AUTOINCREMENT, time INTEGER,title TEXT NOT NULL);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS event");
    onCreate(db);
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <!-- Note built-in ids for 'list' and 'empty' -->
   <ListView
      android:id="@+id/list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
   <TextView
      android:id="@+id/empty"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/empty" />
</LinearLayout>
//strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
   <string name="app_name">Events</string>
   <string name="empty">No events!</string>
</resources>

   
    
    
    
  








Related examples in the same category

1.SQLiteDatabase and ReentrantLock
2.SQLiteDatabase wrapper
3.SQLiteDatabase Helper class
4.Using Database
5.SQLite based diary app
6.Create table, insert record, delete records, query table, remove table
7.Insert Data into database
8.Searchable Dictionary
9.Sqlite Annotations Helper
10.Convert From Java Week To Sqlite Week