SQLite初识(八)数据库--查询数据

前端之家收集整理的这篇文章主要介绍了SQLite初识(八)数据库--查询数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

写一个按钮执行查询数据的操作

private void queryData() {
        String querysql="select * from Student where _id=?";
        String []args=new String[]{"1"};
        // 返回值是一个Cursor对象
        Cursor cursor= msqliteDatabase.rawQuery(querysql,args);
        while (cursor.moveToNext()){
            //真正的获取数据
            int id =cursor.getInt(0);
            String name=cursor.getString(1);
            Log.i("sqlITE","id"+id+"name"+name);
        }
    }

我们指定查询id=1;


如果要查询所有呢
我们修改下我们的查询条件
/**
*查询数据
*/
private void queryData() {
String querysql=”select * from Student”;
// String []args=new String[]{“1”};
// 返回值是一个Cursor对象
Cursor cursor= msqliteDatabase.rawQuery(querysql,null);
while (cursor.moveToNext()){
//真正的获取数据
int id =cursor.getInt(0);
String name=cursor.getString(1);
Log.i(“sqlITE”,”id”+id+”name”+name);
}
}


这就查询了表中所有的数据


直接知道列名称,也可以全部打印

int column_id=cursor.getColumnIndex("_id");
            int id=cursor.getInt(column_id);
            int column_name=cursor.getColumnIndex("name");
            String name=cursor.getString(column_name);
            Log.i("sqlITE","id"+id+"name"+name);

刚才我们用的是sql语法查询的,同样android语法也是可以查询

/** * 用 android提供的方法查询 */
    private void queryData2() {
       Cursor cursor= msqliteDatabase.query("Student",null,null);
        while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            Log.i("sqlITE","id"+id+"name"+name);
        }
        cursor.close();
    }

如果用倒序排列直接修改查询语句

Cursor cursor= msqliteDatabase.query("Student","_id desc");

总结一下


贴上全部代码

public class MainActivity extends AppCompatActivity {
    Button mButton,btn_updata,btn_delete,btn_query;
    sqliteDatabase msqliteDatabase;

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

        //第四个参数,版本号
        MyOpenHelper myOpenHelper=new MyOpenHelper(getApplicationContext(),"mydb",1);
        //两种获取数据库方法
        msqliteDatabase= myOpenHelper.getReadableDatabase();
        // myOpenHelper.getWritableDatabase();
        initView();
        initClick();
    }


    private void initView() {
        mButton=(Button)findViewById(R.id.btn_insert);
        btn_updata=(Button)findViewById(R.id.btn_updata);
        btn_delete=(Button)findViewById(R.id.btn_delete);
        btn_query=(Button)findViewById(R.id.btn_query);
    }

    private void initClick() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertData();
            }
        });
        btn_updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateData();

            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteData();
            }


        });
        //查询
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// 用数据库语言查询
// queryData();
// 用Android提供的方法查询
                   queryData2();

            }


        });
    }

    /** * 用 android提供的方法查询 */
    private void queryData2() {
       Cursor cursor= msqliteDatabase.query("Student","_id desc");
        while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            Log.i("sqlITE","id"+id+"name"+name);
        }
        cursor.close();
    }

    /** *用数据库语言查询数据 */
    private void queryData() {
        String querysql="select * from Student";
// String []args=new String[]{"1"};
        // 返回值是一个Cursor对象
        Cursor cursor= msqliteDatabase.rawQuery(querysql,null);
        while (cursor.moveToNext()){
            //真正的获取数据
// int id =cursor.getInt(0);
// String name=cursor.getString(1);
// Log.i("sqlITE","id"+id+"name"+name);
            int column_id=cursor.getColumnIndex("_id");
            int id=cursor.getInt(column_id);
            int column_name=cursor.getColumnIndex("name");
            String name=cursor.getString(column_name);
            Log.i("sqlITE","id"+id+"name"+name);
        }
        cursor.close();
    }
    /** * 删除数据 */
    private void deleteData() {
// String deletesql="delete from Student Where _id = ?";
// Object[] objs=new Object[]{"3"};
// msqliteDatabase.execsql(deletesql,objs);

        msqliteDatabase.delete("Student","_id=?",new String[]{"7"});
    }

    /** * 修改数据 */
    private void updateData() {
// String updatesql="update Student set name=?,age=? where _id=?";
// Object[] objs=new Object[]{"赵四",30,"1"};
// msqliteDatabase.execsql(updatesql,objs);
        ContentValues contentValues=new ContentValues();
        contentValues.put("age",20);
        msqliteDatabase.update("Student",contentValues,new String[]{"7"});
    }

    /** * 插入数据 */
    private void insertData() {
// String name="李四";
// int age =21;
// String address="北京";
// String insertsql="insert into Student(name,age,address)values('feng',20,'xian')";
// String insertsql="insert into Student(name,address)values('"+name+"',"+age+",'"+address+"')";
// msqliteDatabase.execsql(insertsql);
// String insertsql="insert into Student(name,address)values(?,?,?)";
// Object[]objs=new Object[]{"刘",18,"a"};
// msqliteDatabase.execsql(insertsql,objs);
        ContentValues values=new ContentValues();
        values.put("name","小明");
        values.put("age",18);
        values.put("address","长沙");
        //第二个参数,如果values为空,要传入一个列名,我们给类名中传入一个null
        //如果values不为空的时候,第二个参数没有意义,我们传入一个null就好
        msqliteDatabase.insert("Student",values);
    }

}
@H_321_404@ 原文链接:https://www.f2er.com/sqlite/198285.html

猜你在找的Sqlite相关文章