Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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错误(返回带有对象的ArrayList)_Android_Sql_Sqlite - Fatal编程技术网

Android SQLIte错误(返回带有对象的ArrayList)

Android SQLIte错误(返回带有对象的ArrayList),android,sql,sqlite,Android,Sql,Sqlite,我需要帮助返回一个ArrayList对象,该对象包含学生的stduent_id,并仅选择状态为“已毕业”或“未激活”的学生 public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME= "TPRRHSCHOOL"; private static final String TABLE_STUDENT = "student"; private static final Stri

我需要帮助返回一个ArrayList对象,该对象包含学生的stduent_id,并仅选择状态为“已毕业”或“未激活”的学生

public class DBHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME= "TPRRHSCHOOL";
private static final String TABLE_STUDENT = "student";
private static final String COLUMN_ID = "student_id";
private static final String COLUMN_STATUS = " status";
private static final int DATABASE_VERSION = 1;



public ArrayList<String> getStudents(){
ArrayList<String> students = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null; 
if (cursor.getCount() > 0){
for (int i = 0; i < cursor.getCount(); i++){
Cursor rawQuery (String status, String[] 'Graudate', String[] "Inactive")
cursor.moveToNext();

db.close;
return getStudents;

根据给定的信息,我将假设您当前得到一个NullPointerException,并且不知道如何查询SQL数据库

我将从直接回答您的问题开始,并以一些资源结束,这些资源将帮助您了解正在发生的事情

NPE来自将游标初始化为null,并立即对其调用getCount。要正确初始化光标,需要创建一个查询。我想它看起来类似于:

Cursor cursor = getReadableDatabase().query(
        TABLE_STUDENT,
        new String[] { /* this is an array of column names that I want */ },
        COLUMN_STATUS + " in (?, ?)", // this is Prepared Statement syntax
        new String[] { 'Graudate', 'Inactive' },
        null,
        null,
        null  // Assuming you don't care about order
);
这样做的目的是创建一个类似于

SELECT * FROM student WHERE status IN ('Graudate', 'Inactive')
其中*将替换为所需的实际列名

现在可以使用光标检索数据:

if(cursor != null) { //makes sure that the query returned a cursor
    try {
        if(cursor.moveToFirst()) { //makes sure that the cursor contains data
            List<Student> students = new ArrayList<>(cursor.getCount());
            do {
                Student s = new Student();
                // do this for each variable in student
                s.setSomeVar(
                    cursor.getString(
                        cursor.getColumnIndexOrThrow("SOME_COLUMN_NAME")
                    )
                );
            } while(c.moveToNext()) // position the cursor at the next index
            return students;
        }
    } finally {
        cursor.close();
    }
}
有关创建和理解上述游标的详细信息,请参阅javadoc。您还应该熟悉SQL。有基础知识

与早期安卓系统中的大多数内容一样,SqlLite查询结构过于冗长,可能会让新手感到不快。我建议你在遇到任何你不知道的术语时用谷歌搜索

我建议:

SQL选择语句 SQL准备语句 Android Sqlite游标示例
您应该使用正在使用的编程语言添加标记。完成!非常感谢。很少有评论:1虽然我不认为这是一个问题,但是您在列_STATUS的定义中有一个额外的空白,您拥有STATUS;2您没有呈现错误信息,例如错误消息、结果或任何内容;3您在哪里提交查询并打开光标?根据@PetSerAl问题,您的程序语言是JAVA,而不是SQL或您添加的任何其他标记。您能缩进您的代码吗?非常感谢您的帮助,Mannwood先生!我一定会在我的sqlite langauge上振作起来!你的解释清楚多了!万分感谢!