使用sqlite数据库按列获取特定数据

时间:2017-05-30 07:36:55

标签: java android database sqlite

我无法对我遇到的问题给出好的标题,但基本上我分别有两列NameMaxNum字符串和int! 我需要int方法来生成一个查询,该查询检索我在参数中提供的特定MaxNum的{​​{1}}!

..如果我有这个数据

ID | 姓名 |的 MaxNum来

0 |迈克| 50

1 |约翰| 40

2 |杰斯| 30

..当我将Name作为参数时,它将返回30!

到目前为止我的方法..但我不能使用它,因为它不会返回Jess值!

int

5 个答案:

答案 0 :(得分:4)

你必须使用结果 Cursor

String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
Cursor c = db.rawQuery(query, null);
c.moveToFirst();   
return c.getInt(c.getColumnIndex("MaxNum"));

<强>更新

由于您遇到问题,我建议您使用以下查询:

String query = Select MaxNum from azkar WHERE Name ='Jess';

在使用Cursor c行的代码后,这将有效。

答案 1 :(得分:3)

使用rawQuery从SQLite获取数据。并使用getInt函数从游标获取整数值。

使用以下内容更改maxFromName功能。

public int maxFromName(String name){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "= '" + name +"'" ;
        Cursor c = db.rawQuery(query, null);
        if(c.getCount()!=0){
            c.moveToFirst();
            return c.getInt(c.getColumnIndex("MaxNum"))
          }
    return 0;
}

答案 2 :(得分:2)

在API中,它在execSQL()的说明中说明了以下内容:

  

执行一个非SELECT或任何其他SQL的SQL语句   返回数据的语句。

使用返回Cursor类型的查询方法来检索结果

答案 3 :(得分:2)

从数据库获取单个值的最简单方法是使用stringForQuery()longForQuery()辅助函数:

There are total 19 lines
There are totalx 19 lines
The text in Relationship Type node line 19 is ---Dropdowns
The text in Relationship Type node line 19 is ---Accordions
The text in Relationship Type node line 19 is ---Convert Weights
The text in Relationship Type node line 19 is ---Animated Buttons
The text in Relationship Type node line 19 is ---Side Navigation
The text in Relationship Type node line 19 is ---Top Navigation
The text in Relationship Type node line 19 is ---JS Animations
The text in Relationship Type node line 19 is ---Modal Boxes
The text in Relationship Type node line 19 is ---Progress Bars
The text in Relationship Type node line 19 is ---Parallax
The text in Relationship Type node line 19 is ---Login Form
The text in Relationship Type node line 19 is ---HTML Includes
The text in Relationship Type node line 19 is ---Google Maps
The text in Relationship Type node line 19 is ---Loaders
The text in Relationship Type node line 19 is ---Tooltips
The text in Relationship Type node line 19 is ---Slideshow
The text in Relationship Type node line 19 is ---Filter List
The text in Relationship Type node line 19 is ---Sort List
[31mF[0m

Failures:
1) My Test Test starts
  Message:
[31m    Expected false to be truthy.

(为了防止字符串格式化问题和SQL注入攻击,请始终使用parameters而不是将字符串值直接插入查询中。)

答案 4 :(得分:1)

您可以使用rawQuery

public ArrayList<String> maxFromName(String name){

ArrayList<String> maxNums = new ArrayList<>();

SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " 
+COLUMN_NAME+ "=" + name ;

Cursor c = db.rawQuery(query, null);
c.moveToFirst();  
do { // if that kind of rows are more then one, this metod get all of them
maxNums.add(c.getInt(c.getColumnIndex("MaxNum")));
}
return maxNums;
}