Android SqLite学习笔记——查数据

       使用Android SQLite原生数据库,SQLiteDatabase包含有四个参数不同的query函数,分别包含有7,8,9,10个参数。这么多的参数很难记,在这里以7个参数的query函数为例,介绍一下各个参数的意义,并且举一个自己敲的例子。

Cursor query (
              String table,  
              String[] columns,  
              String selection,  
              String[] selectionArgs, 
              String groupBy,  
              String having, 
              String orderBy )

1. table,String: The table name to compile the query against. (String类型:表示要操作的表名)
3. columns,String[]: A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn’t going to be used.(要返回的列的名字的数组。如果需要根据关键字去查找整条信息,则设置为null,返回所有列) 
4. selection,String: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.(一个决定返回哪一行的过滤器,相当于SQL语句中的 WHERE 关键字。传递null则会返回给定表的所有行。在代码中会有?,例如:" id=?") 
5. selectionArgs,String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.(用于替换上一个参数中的 ? ,顺序对应selection中?的顺序。格式限制为String格式。) 
6. groupBy,String: A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.(用于设定返回行的分组方式,相当于SQL语句中的GROUP BY 关键字。在本例中设置为null,表示返回的行不会被分组。) 
7. having,String: A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.(决定哪一行被放到Cursor中的过滤器。如果使用了行分组,相当于SQL语句中的HAVING关键字。传递null会导致所有的行都包含在内,前提是groupBy属性也设置为null。) 
8. orderBy,String: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.(行的排列方式,相当于SQL语句中的“ORDER BY”关键字,传递null表示使用默认的排序方式,可能是无序排列。) 

例子:

       实现功能:查看SQLite中的数据,并且将数据显示在listview中。

    public void queryData() {

        String mInput = mEt_query.getText().toString().trim();

        Cursor mCursor = mDbReader.query("表名", null, "行名=?", new String[]{mInput}, null, null, null);

        if (mCursor.getCount() > 0) {
            mStringList.clear();        //清空List
            while (mCursor.moveToNext()) {     //游标总是在查询到的上一行
                Map<String, String> mMap = new HashMap<>();
                String form1 = mCursor.getString(mCursor.getColumnIndex("显示数据名称"));
                String form2 = mCursor.getString(mCursor.getColumnIndex("显示数据名称"));
                String form3 = mCursor.getString(mCursor.getColumnIndex("显示数据名称"));

                Log.v("info", "form1是 " + form1 + "  form2是 " + form2+ "  form3是 " + form3);

                mMap.put("tv1", form1);
                mMap.put("tv2", form2);
                mMap.put("tv3", form3);

                mStringList.add(mMap);
            }
            mCursor.close();
        }

在布局文件中添加三个textview控件,用以显示三个查找的数据(本例中每条的数据有三条信息)。以下是添加点击按钮事件的代码:

switch(v.getId()) {
            case R.id.btn_insert:
                insertData();
                mEt_information.setText("");
                mEt_species.setText("");
                mEt_id.setText("");
                break;
            //点击查询按钮
            case R.id.btn_query:
                queryData();
                mEt_query.setText("");      //清空输入框
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("查询结果")
                        .setAdapter(new SimpleAdapter(MainActivity.this, mStringList, R.layout.dialog_tv_layout, new String[]{"tv1", "tv2", "tv3"}, new int[]{R.id.tv1_dialog, R.id.tv2_dialog, R.id.tv3_dialog}), null)
                        .setPositiveButton("确定", null)
                        .show();
                break;
}

主要的还是query()方法的参数理解问题,查询的列和行要分辨好。

展开阅读全文

没有更多推荐了,返回首页