package com.drp.dbexample;
//import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
public class mainDBExample extends ListActivity {
private final String MY_DATABASE_NAME = "db_DrP";
private final String MY_DATABASE_TABLE = "Users";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
EditText et = new EditText(this);
et.setSelection(et.getText().length());
/* Will hold the 'Output' we want to display at the end. */
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase myDB = null;
try {
/* Create the Database (no Errors if it already exists) */
/* Open the DB and remember it */
myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (LastName VARCHAR, FirstName VARCHAR,"
+ " Country VARCHAR, Age INT(3));");
/* Add two DataSets to the Table. */
myDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Gramlich', 'Nicolas', 'Germany', 20);");
myDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Doe', 'John', 'US', 34);");
/* Query for some results with Selection and Projection. */
Cursor c = myDB.rawQuery("SELECT FirstName,Age" +
" FROM " + MY_DATABASE_TABLE
+ " WHERE Age > 10 LIMIT 7;",
null);
/* Get the indices of the Columns we will need */
int firstNameColumn = c.getColumnIndex("FirstName");
int ageColumn = c.getColumnIndex("Age");
/* Check if our result was valid. */
if (c != null) {
/* Check if at least one Result was returned. */
if (c.isFirst()) {
int i = 0;
/* Loop through all Results */
do {
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String firstName = c.getString(firstNameColumn);
int age = c.getInt(ageColumn);
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to show we can <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" /> */
String ageColumName = c.getColumnName(ageColumn);
/* Add current Entry to results. */
results.add("" + i + ": " + firstName
+ " (" + ageColumName + ": " + age + ")");
} while (c.moveToNext());
}
}
} finally {
if (myDB != null)
myDB.close();
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
}