Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 查询sqlite数据库时没有此类列错误_Android_Sqlite - Fatal编程技术网

Android 查询sqlite数据库时没有此类列错误

Android 查询sqlite数据库时没有此类列错误,android,sqlite,Android,Sqlite,我目前正在创建一个简单的待办事项列表应用程序。用户将输入任务的名称、位置和状态,并将其保存在SQLite数据库中,我将按两个类别(已完成和未完成)列出信息,最后显示在ListView中 我遇到的问题是sql查询选择所有已完成的项目或所有未完成的项目 这是我因未完成而收到的错误 android.database.sqlite.SQLiteException:无此类列:已完成 (代码1):,编译时:选择代码、任务名称、任务位置, 任务中的taskStatus,其中taskStatus=未完成 这是我

我目前正在创建一个简单的待办事项列表应用程序。用户将输入任务的名称、位置和状态,并将其保存在
SQLite
数据库中,我将按两个类别(已完成和未完成)列出信息,最后显示在
ListView

我遇到的问题是sql查询选择所有已完成的项目或所有未完成的项目

这是我因未完成而收到的错误

android.database.sqlite.SQLiteException:无此类列:已完成 (代码1):,编译时:选择代码、任务名称、任务位置, 任务中的taskStatus,其中taskStatus=未完成

这是我的密码

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>

这是相关的数据库代码:

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;
    }

}
公共类数据库管理器任务列表{
String complete=“completed”;
String notComplete=“未完成”;
公共静态最终字符串DB_NAME=“TODOLIST”;
公共静态最终字符串DB_TABLE=“TASK”;
公共静态最终int DB_版本=1;
私有静态最终字符串CREATE_TABLE=“CREATE TABLE”+DB_TABLE+
代码整数主键自动递增,任务名文本不为空+
“任务位置文本、任务状态文本不为NULL、已删除整数不为NULL);”;
私人助理;
专用数据库数据库;
私人语境;
公共数据库管理器任务列表(上下文c){
this.context=c;
helper=newsqlhelper(c);
this.db=helper.getWritableDatabase();
}
公共布尔addRow(字符串名称、字符串位置、字符串状态){
ContentValues newTask=新ContentValues();
newTask.put(“任务名称”,名称);
newTask.put(“任务位置”,位置);
newTask.put(“任务状态”,状态);
newTask.put(“已删除”,0);
插入(db_表,null,newTask);
返回true;
}
公共ArrayListreeVerowsnc(){
ArrayList结果=新建ArrayList();
字符串[]列=新字符串[]{“代码”、“任务名称”、“任务位置”、“任务状态”};
Cursor Cursor=db.query(db_表,列,“taskStatus”+“=”+notComplete,null,null,null);
cursor.moveToFirst();
while(cursor.isAfterLast()==false){
结果.add(cursor.getInt(0)+”,“+cursor.getString(1)+”,”+
cursor.getString(2)+“,”+cursor.getString(3)+“\n”);
cursor.moveToNext();
}
if(cursor!=null&!cursor.isClosed()){
cursor.close();
}
返回结果;
}
}
这就是我使用它的方式

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);
    }


}
公共类viewTasks扩展了AppCompative活动{
视图;
私有CustomListView listView,listView1;
纺纱机;
私人int poss;
私有数据库管理器任务数据库mydb;
@凌驾
创建时受保护的void(@Nullable final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.viewtasks);
listView=(CustomListView)findViewById(R.id.listViewTask);
微调器=(微调器)findViewById(R.id.spinner);
mydb=新数据库管理器任务列表(viewTasks.this);
mydb.openReadable();
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
long selection=spinner.getSelectedItemId();
如果(选择==0){
displayCompletedTask();
}
else if(选择==1){
displayNotCompletedTask();
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}
public void displayCompletedTask(){
ArrayList result=mydb.retrieveRowsC();
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,结果);
setAdapter(适配器);
}
public void displayNotCompletedTask(){
ArrayList result=mydb.retrieveRowsNC();
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,结果);
setAdapter(适配器);
}
}
这就是我努力实现的目标: 当用户从微调器中单击“完成”或“不完成”时,我希望在列表视图中显示该请求

编辑从注释中添加了更多代码

 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;
    }
public arraylistrieverowsc(){
ArrayList结果=新建ArrayList();
字符串[]列=新字符串[]{“代码”、“任务名称”、“任务位置”、“任务状态”};
Cursor Cursor=db.query(db_表,列,null,null,null,null);
cursor.moveToFirst();
while(cursor.isAfterLast()==false){
结果.add(cursor.getInt(0)+”,“+cursor.getString(1)+”,”+
cursor.getString(2)+“,”+cursor.getString(3)+“\n”);
cursor.moveToNext();
}
if(cursor!=null&!cursor.isClosed()){
cursor.close();
}
返回结果;
}
上面的代码工作正常并显示数据库中的所有行:当添加where子句以仅搜索已完成的任务时,我遇到相同的错误。

query

public Cursor query (boolean distinct, 
                String table, 
                String[] columns, 
                String selection, 
                String[] selectionArgs, 
                String groupBy, 
                String having, 
                String orderBy, 
                String limit)
从上一个答案来看,
“taskStatus”+“=”+notComplete
属于
选择
参数,这是错误的

正确的方法应该是

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

使用Room保存您的数据试试这个
String[]selectionArgs=newstring[]{notComplete};光标c=db.rawQuery(“从“+db_TABLE+”中选择*,其中“+taskStatus+”=?”,selectionArgs)@JohnJoe这对我来说很有用,很高兴它能成为我能接受的答案!你也知道为什么我原来在哪里吗