Using java Syntax Highlighting
- package com.android.dbdroid;
- import android.os.Bundle;
- import android.app.ListActivity;
- import android.database.sqlite.*;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.util.Log;
- import android.database.Cursor;
- import java.util.ArrayList;
- import android.widget.SimpleCursorAdapter;
- public class DBDroid extends ListActivity {
- private final String USERS_TABLE = "users";
- private final String DATABASE_NAME = "DbDroid";
- private static final String TAG="DBDroidActivity";
- //private static final int DATABASE_VERSION = 1;
- private CursorFactory cf;
- private SQLiteDatabase mydb;
- ArrayList<String> results = new ArrayList<String>();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.v(TAG,"Creating database");
- mydb.openOrCreateDatabase(DATABASE_NAME, cf);
- mydb.execSQL("CREATE TABLE IF NOT EXISTS "
- + USERS_TABLE
- + " (LastName VARCHAR, FirstName VARCHAR,"
- + " Country VARCHAR, Age INT(3));");
- /* Add two DataSets to the Table. */
- mydb.execSQL("INSERT INTO "
- + USERS_TABLE
- + " (LastName, FirstName, Country, Age)"
- + " VALUES ('Gramlich', 'Nicolas', 'Germany', 20);");
- mydb.execSQL("INSERT INTO "
- + USERS_TABLE
- + " (LastName, FirstName, Country, Age)"
- + " VALUES ('Doe', 'John', 'US', 34);");
- Cursor c = mydb.query(USERS_TABLE, new String[] { "feed_id", "title",
- "url" }, null, null, null, null, null);
- /* Get the indices of the Columns we will need */
- int firstNameColumn = c.getColumnIndex("FirstName");
- int ageColumn = c.getColumnIndex("Age");
- if(c!=null){
- if(c.moveToFirst()){
- int i=0;
- do{
- i++;
- String firstName=c.getString(firstNameColumn);
- int age=c.getInt(ageColumn);
- String ageColumName = c.getColumnName(ageColumn);
- results.add("" + i + ": " + firstName
- + " (" + ageColumName + ": " + age + ")");
- }while(c.moveToNext());
- }
- }
- SimpleCursorAdapter users = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, c, null, null);
- setListAdapter(users);
- //setContentView(R.layout.main);
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
When I run the code in eclipse I get the following error within the console:
- Code: Select all
[2009-02-05 13:36:14 - DBDroid] emulator-5554 disconnected! Cancelling 'com.android.dbdroid.DBDroid' launch!
I think there's something missing in my code, not sure, I want to keep this code as simple as possible and the notepad example is a bit to complex for me to grasp the basics of database connection and data retrieval.



