设为首页 加入收藏

TOP

安卓开发笔记(十三):SQLite数据库储存(下)数据的增添,更改,删除,查询(四)
2019-09-01 23:12:59 】 浏览:80
Tags:安卓 开发 笔记 十三 SQLite 数据库 储存 数据 增添 更改 删除 查询
); ContentValues values
=new ContentValues(); values.put("name","我是傻逼\n"); db.update("Book",values,"name=?",new String[]{"the fuck code"});//如果名字等于这个就可以进行更新了 } }); Button deleteData= (Button)findViewById(R.id.deletedata); deleteData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDatabase db=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name","我是傻逼\n"); db.delete("Book","name=?",new String[]{"the fuck code"});//如果名字等于这个就可以直接删除了 } }); }}

4.查询数据

在咱们的安卓开发当中的SQLiteDatabase类当中还提供了一个query()方法对于数据进行查询,这个方法非常复杂,最短的一个方法重载也需要传入7个参数。

主界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/creat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Create database"/>
<Button
    android:id="@+id/add"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add data"/>
<Button
    android:id="@+id/updata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Updata data"/>
<Button
    android:id="@+id/deletedata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Delete data"/>

</LinearLayout>

主活动的查询代码如下:

 private void queryStudents() {

        // 相当于 select * from students 语句
        Cursor cursor = mSQLiteDatabase.query(SQLiteDbHelper.TABLE_STUDENT, null,
                "cls_id > ? and id >= 1", new String[]{"3"},
                null, null, null, null);

        // 不断移动光标获取值
        while (cursor.moveToNext()) {
            // 直接通过索引获取字段值
            int stuId = cursor.getInt(0);

            // 先获取 name 的索引值,然后再通过索引获取字段值
            String stuName = cursor.getString(cursor.getColumnIndex("name"));
            Log.e("", "id: " + stuId + " name: " + stuName);
        }
        // 关闭光标
        cursor.close();
    }

最后我们利用adb工具就可以查看到我们是否成功进行数据库操作啦!!

 

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分享 Xamarin.android 关于使用SQ.. 下一篇Android自动解析html带图片,实现..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目