william_
william_

Reputation: 1133

no such column error on querying sqlite database

I am currently creating a simple To do list app. the user will enter the name, location and status of the task and it will be saved in the SQLite database and I will list the info in two categories (completed and not completed) and finally displayed in ListView.

The problem I am having is with my sql query to select either all the items that are completed or all the items that are not completed.

This is the error I am getting for not completed

android.database.sqlite.SQLiteException: no such column: completed (code 1): , while compiling: SELECT code, taskName, taskLocation, taskStatus FROM TASK WHERE taskStatus=not completed

and here is my code

VIEWTASK.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/choice"/>
    <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <com.example.assignment1.CustomListView
        android:id="@+id/listViewTask"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

this is the relevant database code:

public class databaseManagerTaskToDoList {
    String complete = "completed";
    String notComplete = "not completed";
    public static final String DB_NAME = "TODOLIST";
    public static final String DB_TABLE = "TASK";
    public static final int DB_VERSION = 1;
    private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE +
            " (code INTEGER PRIMARY KEY AUTOINCREMENT , taskName TEXT NOT NULL, " +
            "taskLocation TEXT, taskStatus TEXT NOT NULL, deleted INTEGER NOT NULL);";
    private SQLHelper helper;
    private SQLiteDatabase db;
    private Context context;
    public databaseManagerTaskToDoList(Context c) {
        this.context = c;
        helper = new SQLHelper(c);
        this.db = helper.getWritableDatabase();
    }

public boolean addRow( String name,String location, String status) {
        ContentValues newTask = new ContentValues();
        newTask.put("taskName", name);
        newTask.put("taskLocation", location);
        newTask.put("taskStatus", status);
        newTask.put("deleted",0);
        db.insert(DB_TABLE,null,newTask);
        return true;
    }
public ArrayList<String>retrieveRowsNC() {
        ArrayList<String> result = new ArrayList<String>();
        String[] columns = new String[] {"code", "taskName", "taskLocation","taskStatus"};
        Cursor cursor = db.query(DB_TABLE, columns, "taskStatus" + "=" + notComplete, null, null, null, null);
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            result.add(cursor.getInt(0) + ", " + cursor.getString(1) + ", " +
                    cursor.getString(2) + ", "+ cursor.getString(3)+ "\n");
            cursor.moveToNext();
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return result;
    }

}

and this is how I am using it

public class viewTasks extends AppCompatActivity {
    View view;
    private CustomListView  listView,listView1;
    Spinner spinner;
     private   int   poss;
    private databaseManagerTaskToDoList mydb;


    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewtasks);
        listView = (CustomListView) findViewById(R.id.listViewTask);
        spinner = (Spinner)findViewById(R.id.spinner);

        mydb = new databaseManagerTaskToDoList(viewTasks.this);
                mydb.openReadable();
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                long selection = spinner.getSelectedItemId();
                if (selection == 0){
                    displayCompletedTask();
                }
                else if (selection == 1){
                    displayNotCompletedTask();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
    public void displayCompletedTask() {

        ArrayList<String> result = mydb.retrieveRowsC();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, result);
        listView.setAdapter(adapter);
    }
    public void displayNotCompletedTask() {

        ArrayList<String> result = mydb.retrieveRowsNC();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, result);
        listView.setAdapter(adapter);
    }


}

this is what i am trying to achieve: when the user clicks complete or not complete from the spinner I would like to display there request in the list view.

EDIT added more code from comment

 public ArrayList<String>retrieveRowsC() {
        ArrayList<String> result = new ArrayList<String>();
        String[] columns = new String[] {"code", "taskName", "taskLocation","taskStatus"};
        Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            result.add(cursor.getInt(0) + ", " + cursor.getString(1) + ", " +
                    cursor.getString(2) + ", "+ cursor.getString(3)+ "\n");
            cursor.moveToNext();
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return result;
    }

the above code works correctly and displays all rows in the database: i get the same error when adding the where clause to only search for completed tasks.

Upvotes: 0

Views: 574

Answers (1)

John Joe
John Joe

Reputation: 12803

query

public Cursor query (boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)

From your previous answer, "taskStatus" + "=" + notComplete belong to selection parameter, which is wrong.

The correct way should be

Cursor cursor = db.query(DB_TABLE, columns,
      "taskStatus = ?", new String[] {notComplete}, null, null, null);

Upvotes: 1

Related Questions