SQLiteDatabase的于添加、删除、更新、查询的操作方法:insert()、delete()、update()、和query()

这篇博客详细介绍了如何使用SQLite数据库在Android中进行数据操作,包括使用ContentValues进行数据插入,通过delete()方法删除数据,update()方法更新数据,以及运用query()方法进行数据查询和分页查询。同时,还展示了如何获取数据库记录总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQLiteDatabase还专门提供了对应于添加、删除、更新、查询的操作方法:insert()、delete()、update()、和query()。

insert()方法用于添加数据,各个字段的数据使用ContentValues进行存放。
ContentValues类似于map,相对于map,它提供了存取数据对应的put(String key,xxx Value)和getAsXxx(String key)方法,key为字段名称,value为字段值,Xxx指的是各种常用的数据类型,如:String、integet等。

insert添加


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.domain.Person;

import java.util.ArrayList;
import java.util.List;

public class OtherpersonService {
    private Context context;
    private DBOpenHelper dbOpenHelper;
    public OtherpersonService(Context context,DBOpenHelper dbOpenHelper) {
        this.context=context;
        this.dbOpenHelper=dbOpenHelper;
    }

    //添加
    public void save(Person person){
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        //name一定要和表的名称一致
        values.put("name",person.getName());
        values.put("phone",person.getPhone());
        //第一个参数是表的名称
        //第二个参数为空值字段,就是如果第三个参数为空(null)的时候就会用到第二个参数的值。用第二个参数代替第三个参数组拼成SQL语句
        //比如:insert into person(name) values(null)   这里的person字段使用了第二个参数的name
        //第三个参数不为空就不会用到第二个参数
        db.insert("person","name",values);//values值为null就使用
    }

delete删除

//删除
    public void delete(Integer id){
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
        //1表名、2字段名、3占位符的数据
        db.delete("person","personid=?",new String[]{id.toString()});
    }

update更新
ContentValues类似于map,相对于map,它提供了存取数据对应的put(String key,xxx Value)和getAsXxx(String key)方法,key为字段名称,value为字段值,Xxx指的是各种常用的数据类型,如:String、integet等。

//更新
    public void update(Person person){
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name",person.getName());
        values.put("phone",person.getPhone());
        //1表名、2需要更新值、3以什么条件字段更新、4条件字段的数据值(占位符的值)
        db.update("person",values,"personid=?",new String[]{person.getId().toString()});
    }

query查询

//查询
    public Person find(Integer id){
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
        //1 表名、   2 需要查询的字段列表,用字符串数组形式传入,null为所有的字段、   3 以什么条件字段查询、   4 条件字段的数据值(占位符的值)、
        // 5 groupBy相当于select语句的groupby后面的部分、   6 having相当于select语句的having后面的部分、  7 order是我们想要的排序方式。
        Cursor cursor=db.query("person",null,"personid=?",new String[]{id.toString()},null,null,null);
		//moveToFirst移动一下,判断是否可以移动
        if(cursor.moveToFirst()){
            Integer personid=cursor.getInt(0);
            String name=cursor.getString(1);
            String phone=cursor.getString(2);
            return new Person(personid,name,phone);
        }
        return null;
    }

query分页查询

//分页查询
    public List<Person> getScrolld(int offset, int maxResult){
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
        List<Person> personList=new ArrayList<>();
        // Cursor cursor=db.rawQuery("select * from person order by personid asc limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)});
        //"personid asc",offset+","+maxResult这段相当于这里personid asc limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)}
        Cursor cursor=db.query("person",null,null,null,null,null,"personid asc",offset+","+maxResult);
        while (cursor.moveToNext()){
            int personid=cursor.getInt(0);
            String name=cursor.getString(1);
            String phone=cursor.getString(2);
            personList.add(new Person(personid,name,phone));
        }
        db.close();
        return personList;
    }

count总数

    //总数
    public int getCount(){
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
        //select count(*) from person",null
        //new一个字符串
        Cursor cursor = db.query("person", new String[]{"count(*)"}, null, null, null, null,null);
        cursor.moveToFirst();
        int result=cursor.getInt(0);
        db.close();
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TL。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值