views:

297

answers:

2

I successfully created the Database and inserted a row however I cannot Query it for some reason. My Droid crashes everytime.

          // Create a new row of values to insert.
          ContentValues newValues = new ContentValues();
          // Assign values for each row.
          newValues.put("value", "kunjan");
          // Insert the row into your table
          myDatabase.insert(DATABASE_TABLE, null, newValues);

          String[] result_columns = new String[] { "value" };

          // I GET AN EXCEPTION HERE
          Cursor allRows = myDatabase.query(true, DATABASE_TABLE, result_columns, null, null,
              null, null, null, null);

          if (allRows.moveToFirst()) {
            String value = allRows.getString(0);
            TextView foo = (TextView) findViewById(R.id.TextView01);
            foo.setText(value);
          }

          allRows.close();
          myDatabase.close();

I get this exception

no such column: value: , while compiling: SELECT DISTINCT value FROM mainTable
+2  A: 

I think you are creating table with only one column(value) and you are trying to read the column-1 instead of column-0 from the cursor. If that still not helps, please add try-catch block and capture the logs

String value = allRows.getString(0);
Vamsi
Changed it. Thanks. But I get an exception while querying.
kunjaan
could you please try query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)instead of the query() you used, i am not not much aware the of the field 'distinct' and how does it affect
Vamsi
A: 

Some of the problems with the original post

  • Creating a table that has already been created. I wrote "if not exists" in the "create table" instruction.
  • Not handling Exceptions properly. I got a lot of exceptions in the creation of the table, quering it etc. I sorrounded all these operations with try-catch
  • Using hard coded values for Column names - This made it harder to verify if I was querying the column that I had created.
kunjaan