Android学习——查询数据

查询数据

SQLiteDatabase 中还提供了一个 query()方法用于对数据进行查询。 这个方法的参数非常复杂,最短的一个方法重载也需要传入七个参数。那我们就先来看一下 这七个参数各自的含义吧。

第一个参数是表名,表示我们希望从哪张表中查 询数据。

第二个参数用于指定去查询哪几列,如果不指定则默认查询所有列。

第三、第四个 参数用于去约束查询某一行或某几行的数据,不指定则默认是查询所有行的数据。

第五个参 数用于指定需要去 group by 的列,不指定则表示不对查询结果进行 group by 操作。

第六个参 数用于对 group by 之后的数据进行进一步的过滤,不指定则表示不进行过滤。

第七个参数用 于指定查询结果的排序方式,不指定则表示使用默认的排序方式。

实例

修改 activity_main.xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
 2 
 3 android:orientation="vertical" >
 4 
 5  
 6 
 7 ……
 8 
 9  
10 
11  
12 
13 <Button android:id="@+id/query_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Query data"/>
14 
15 </LinearLayout>

修改 MainActivity 中的代码:

 1 public class MainActivity extends Activity {
 2 
 3  
 4 
 5 private MyDatabaseHelper dbHelper;
 6 
 7  
 8 
 9 @Override
10 
11  
12 
13 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
14 
15 dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
16 
17 ……
18 
19 Button queryButton = (Button) findViewById(R.id.query_data);
20 
21 queryButton.setOnClickListener(new OnClickListener() {
22 
23 @Override
24 
25 public void onClick(View v) {
26 
27 SQLiteDatabase db = dbHelper.getWritableDatabase();
28 
29 // 查询Book表中所有的数据
30 
31 Cursor cursor = db.query("Book", null, null, null, null, null, null);
32 
33 if (cursor.moveToFirst()) {
34 
35 do {
36 
37 // 遍历Cursor对象,取出数据并打印
38 
39 String name = cursor.getString(cursor. getColumnIndex("name"));
40 
41 String author = cursor.getString(cursor.
42 
43 getColumnIndex("author"));
44 
45 int pages = cursor.getInt(cursor.getColumnIndex("pages"));
46 
47  
48 
49 double price = cursor.getDouble(cursor.
50 
51  
52 
53 getColumnIndex("price"));
54 
55 Log.d("MainActivity", "book name is " + name); Log.d("MainActivity", "book author is " + author); Log.d("MainActivity", "book pages is " + pages); Log.d("MainActivity", "book price is " + price);
56 
57 }
58 
59 while (cursor.moveToNext());
60 
61  
62 
63  }});}
64 
65 
66 }
67 
68 cursor.close();
69 
70  
71 
72  
73 
74 }

我们首先在查询按钮的点击事件里面调用了 SQLiteDatabase 的 query()方法 去查询数据。这里的 query()方法非常简单,只是使用了第一个参数指明去查询 Book 表,后 面的参数全部为 null。这就表示希望查询这张表中的所有数据,虽然这张表中目前只剩下一 条数据了。查询完之后就得到了一个 Cursor 对象,接着我们调用它的 moveToFirst()方法将数据的指针移动到第一行的位置,然后进入了一个循环当中,去遍历查询到的每一行数据。在这个循环中可以通过 Cursor 的 getColumnIndex()方法获取到某一列在表中对应的位置索引, 然后将这个索引传入到相应的取值方法中,就可以得到从数据库中读取到的数据了。接着我 们使用 Log 的方式将取出的数据打印出来,借此来检查一下读取工作有没有成功完成。最后 别忘了调用 close()方法来关闭 Cursor。

public class MainActivity extends Activity {

 

private MyDatabaseHelper dbHelper;

 

@Override

 

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);

……

Button queryButton = (Button) findViewById(R.id.query_data);

queryButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

SQLiteDatabase db = dbHelper.getWritableDatabase();

// 查询Book表中所有的数据

Cursor cursor = db.query("Book", null, null, null, null, null, null);

if (cursor.moveToFirst()) {

do {

// 遍历Cursor对象,取出数据并打印

String name = cursor.getString(cursor. getColumnIndex("name"));

String author = cursor.getString(cursor.

getColumnIndex("author"));

int pages = cursor.getInt(cursor.getColumnIndex("pages"));

 

double price = cursor.getDouble(cursor.

 

getColumnIndex("price"));

Log.d("MainActivity", "book name is " + name); Log.d("MainActivity", "book author is " + author); Log.d("MainActivity", "book pages is " + pages); Log.d("MainActivity", "book price is " + price);

}

while (cursor.moveToNext());

 

 }});}


}

cursor.close();

 

 

}

原文地址:https://www.cnblogs.com/znjy/p/14908237.html