0

I want to get some data from my database I have created using my application. Here is my code

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.select_to_delete);

database = new DatabaseHandler(this);

bundle = getIntent().getExtras();
date = bundle.getString("DATE");

SQLiteDatabase db=database.getWritableDatabase();

Cursor cur=db.rawQuery("SELECT " +TITLE+  " FROM " +TABLE_NAME + " WHERE " + CDATE + "=" + date,null);


int i = cur.getCount();


if(cur !=null){
    if (cur.moveToFirst()){
           do{
              tdate = cur.getString(cur.getColumnIndex("DATE"));
              titlearray.add(tdate);
           }while(cur.moveToNext());
        }

}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titlearray);

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}

My table has 5 columns (id, title, date, time, descrption). and have 4 raws. The table is created in another class.

When I run this application it doesn't get the data from the sql table and show it in listview. When I debug it, it shows i=1 and the it returns false to if (cur.moveToFirst()). So it doesn't go inside that if part.

What could my mistake be?

A--C
  • 36,351
  • 10
  • 106
  • 92
Ravindu
  • 2,408
  • 8
  • 30
  • 46
  • If you want existing database, follow this: http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application/9109728#9109728 – Yaqub Ahmad Apr 16 '13 at 04:18

3 Answers3

1

place apostrophe (') around your date

Cursor cur=db.rawQuery("SELECT " +TITLE+  " FROM " +TABLE_NAME + " WHERE " + CDATE + "='" + date+"'",null);

N.B. I am assuming your table creation and other field names are accurate. Also recheck those

stinepike
  • 54,068
  • 14
  • 92
  • 112
1

Use selection args and you will be avoiding tons of problems:

Cursor cur=db.rawQuery("SELECT " +TITLE+  " FROM " +TABLE_NAME + " WHERE " + CDATE + " = ?", new String[] { date });
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0
SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, new String[] { TITLE}, CDATE + "=?",
                new String[] { String.valueOf(date) }, null, null, null, null);
       if (cursor.moveToFirst()) {
        do {
           //add to the array
        } while (cursor.moveToNext());
    }
Srikanth Roopa
  • 1,782
  • 2
  • 13
  • 19