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 无法从数据库检索列的数据_Android_Sqlite - Fatal编程技术网

Android 无法从数据库检索列的数据

Android 无法从数据库检索列的数据,android,sqlite,Android,Sqlite,我在数据库中有一个名为keywords的表。我想从此表中检索报警列和位置列的数据,但除了联系人号码外,无法检索它们。目前,我在Toast中显示它们的值,但每次我运行任何查询以在Toast中显示我的报警或位置时,它都是空的。但我的联系人号码总是显示出来。不要这样做了解这个问题的原因。我也检查了我的表视图,它显示了报警值和位置 Create Table keywords( contact_number text primary key , alarm text , location text )

我在数据库中有一个名为keywords的表。我想从此表中检索报警列和位置列的数据,但除了联系人号码外,无法检索它们。目前,我在Toast中显示它们的值,但每次我运行任何查询以在Toast中显示我的报警或位置时,它都是空的。但我的联系人号码总是显示出来。不要这样做了解这个问题的原因。我也检查了我的表视图,它显示了报警值和位置

Create Table keywords( contact_number text primary key  , alarm text  , location text )
我的插入函数是

  public boolean insertkeys (String alarm ,String location ,String contact){

        SQLiteDatabase db = this.getWritableDatabase();
        //ContentValues is a name value pair, used to insert or update values into database tables.
        // ContentValues object will be passed to SQLiteDataBase objects insert() and update() functions.
        //  ContentValues contentValues = new ContentValues();
        ContentValues contentValues = new ContentValues();


        contentValues.put("alarm",alarm);
        contentValues.put("location",location);
        contentValues.put("contact_number",contact);
        long ins = db.insert("keywords",null,contentValues);
        long upd = db.update("keywords",contentValues,"contact_number = ?",new String[]{contact});
        //  db.close();
        if(ins == -1 && upd == -1)
            return  false;
        else
            return true;
    }

每次单击“保存”按钮时,我都会插入并更新数据。这里有人能告诉我如何编写查询来检索这些字段的数据,并将其设置为Toast或Edit text吗。我是数据库新手,在这里呆了大约一个星期。提前感谢您的帮助:)

您可以通过使用Android SDK时作为光标返回的查询来提取数据

光标类似于表,因为它有许多行,每个行有一组列,这些列由您选择的内容决定

要获取所有行,SELECT查询将沿着以下行进行:-

`SELECT * FROM keywords`
要使用Android SDK实现这一点,您可以使用SQLiteDatabase便利方法,例如,对于上述方法,您可以使用:-

Cursor cursor = db.query("keywords",null,null,null,null,null,null);
  • 检查上面的链接,了解可以传递的值/参数以及它们与SELECT语句的关联方式
然后遍历返回的光标以提取数据,通常使用的方法是。注意,如果无法移动,most将返回false,同时注意光标中的原始位置在第一行之前

因此,您可以有一个方法返回游标,如下所示:-

public Cursor getAllKeys(){

    SQLiteDatabase db = this.getWritableDatabase();
    return db.query("keywords",null,null,null,null,null,null);
}
然后,您可以使用以下方法处理所有行:-

Cursor csr = yourDBHelper.getAllKeys();
while (csr.moveToNext()) {
    String current_contact_number = csr.getString(csr.getColumnIndex("contact_number");
    String current_alarm = csr.getString(csr.getColumnIndex("alarm");
    String current_location = csr.getString(csr.getColumnIndex("location"));
    ...... your code to Toast or use the retrieved values       
}
csr.close(); //<<<<<<<<<< you should always close a Cursor when finished with it.
Cursor csr=yourDBHelper.getAllKeys();
while(csr.moveToNext()){
字符串current_contact_number=csr.getString(csr.getColumnIndex(“contact_number”);
字符串当前_报警=csr.getString(csr.getColumnIndex(“报警”);
字符串当前位置=csr.getString(csr.getColumnIndex(“位置”);
……您的代码用于Toast或使用检索到的值
}

csr.close();//非常感谢……查询在获取所有数据方面绝对有效。但如果我需要获取特定列,那么在while中应该使用什么来代替moveToNext@AzmatInayatmoveToNext仍然可以工作,就像只有一行一样,然后在该行之后,光标将移动到最后一行之后,将返回false并退出while循环。如果您知道,只有一行可以使用moveToFirst,但始终要检查返回值。有关其他游标方法(例如moveToPosition、moveToPrevious、getInt()、getLong()等……),请参阅答案中的链接。我正在尝试分别获取报警、位置和联系人号码(比如从关键字中选择报警,其中联系人号码=?”)比如为每个关键字单独查询。您建议的游标查询我尝试在其中进行更改,比如放置列和where子句,但之后执行时它不会返回任何内容。您能不能也告诉我该查询。再次感谢您的帮助:)@AzmatInayat我编辑了答案,并给出了一个基于联系人号码获取警报的示例方法。
public String getAlarmByContactNumber(String contactNumber){

    String rv = "";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor csr = db.query(
        "keywords", //<<<<<<<<<<<< the FROM clause (less the FROM keyword) typically the name of the table from which the data is to be extracted (but can include JOINS for example, for more complex queries)
        new String[]{"alarm"}, //<<<<<<<<<< A String[] of the column names to be extracted (null equates to * which means all columns) (note can be more complex and include functions/sub queries)
        "contact_number=?", //<<<<<<<<<< WHERE clause (less the WHERE keyword) note **?** are place holders for parameters passed as 4th argument
        new String[]{contactNumber},
        null, //<<<<<<<<<< GROUP BY clause (less the GROUP BY keywords)
        null, //<<<<<<<<<< HAVING clause (less the HAVING keyword)
        null  //<<<<<<<<<< ORDER BY clause (less the ORDER BY keywords)
    );
    if (csr.moveToFirst()) {
        rv = csr.getString(csr.getColumnIndex("alarm"));
    }
    csr.close();
    return rv;
}