title: Querying and working with Cursors in SQLite on Android
body: <p>Not sure if I'm the only one who feels this... </p>
  
  <p>I find working with the sqlite api in android a complete pain in the butt and pretty soul destroying. Has anyone got any tips/helpers to make my life easier?</p>
  
  <p>Here's an example of what I'm talking about.</p>
  
  <pre><code>//create code
  
  db.execSQL("CREATE TABLE " + CUSTOMER_TABLE_NAME + " ("
                          + GENERIC_ID_KEY+ " INTEGER PRIMARY KEY NOT NULL, " 
                          + PHONE_KEY + " INTEGER NOT NULL, "
                          + CUSTOMER_NAME_KEY+ " TEXT NOT NULL, "
                          + EMAIL_KEY + " TEXT NOT NULL, "
                          + ADDRESS_KEY +" TEXT);");
  
  
  //get code
      Cursor mCursor = mDb.query(true, CUSTOMER_TABLE_NAME, new String[] {GENERIC_ID_KEY,
                          ADDRESS_KEY, PHONE_KEY, EMAIL_KEY,CUSTOMER_NAME_KEY}, GENERIC_ID_KEY + "=" + customerDbId, null,
                                  null, null, null, null);
  
          Customer customer = new Customer (customerDbId, (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(CUSTOMER_NAME_KEY)),
                              (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(PHONE_KEY)),
                              (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(EMAIL_KEY)),
                              (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(ADDRESS_KEY)));
  </code></pre>
  
  <p>This a simple exmple of creating a simple customer object from a db query; some of my code is far nastier than this. Hand crafting queries in this way leads to all sort of errors I don't find until runtime.</p>
  
  <p>Any tips greatly appreiciated!</p>
  
  <p>Ok after the tips below I now have this:</p>
  
  <pre><code>  db.execSQL("CREATE TABLE customer (_id INTEGER PRIMARY KEY NOT NULL, " 
                          + "phone_number INTEGER NOT NULL, "
                          + "name TEXT NOT NULL, "
                          + "email TEXT NOT NULL, "
                          + "address TEXT);");
  
  
      //get code
  String q = "SELECT * FROM customer WHERE _id = " + customerDbId +";"
          Cursor mCursor = mDb.rawQuery(q, null);
  
          Customer customer = new Customer (mCursor);
  </code></pre>
  
  <p>in the Customer, I access the fields like this</p>
  
  <pre><code>mName = cursor.getString(2)
  </code></pre>
  
  <p>Ahh, I feel much better :)</p>
  
  <p>Cheers
  Si</p>