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

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;
    }
}

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: SQLiteDatabase类是在Android系统中用于操作SQLite数据库的类。以下是这些方法的作用和基本格式: 1. insert(String table, String nullColumnHack, ContentValues values)方法:这个方法用于向给定的表中插入一条新记录。table参数是表名,nullColumnHack参数是插入空值的列名,values参数是包含列名和值的ContentValues对象。这个方法返回新记录的行号,如果插入失败则返回-1。 2. update(String table, ContentValues values, String whereClause, String[] whereArgs)方法:这个方法用于更新表中的记录。table参数是表名,values参数是包含列名和新值的ContentValues对象,whereClause参数是用于指定更新记录的条件的SQL语句,whereArgs参数是与whereClause中的占位符对应的参数值。这个方法返回更新的记录数,如果更新失败则返回0。 3. delete(String table, String whereClause, String[] whereArgs)方法:这个方法用于删除表中的记录。table参数是表名,whereClause参数是用于指定删除记录的条件的SQL语句,whereArgs参数是与whereClause中的占位符对应的参数值。这个方法返回删除的记录数,如果删除失败则返回0。 4. execSQL(String sql)方法:这个方法用于执行给定的SQL语句。sql参数是SQL语句字符串。这个方法无返回值。 5. query(String table, String[] columns, String selection, String[] selectionArgs, String ### 回答2: insert(...... )方法:该方法用于向数据库表中插入数据。它的基本格式为:insert(String table, String nullColumnHack, ContentValues values)。其中,table参数表示要插入的表名,nullColumnHack表示插入记录中的一个可以为空的列,values参数表示要插入的数据内容。 update(...... )方法:该方法用于更新数据库表中的数据。它的基本格式为:update(String table, ContentValues values, String whereClause, String[] whereArgs)。其中,table参数表示要更新的表名,values参数表示要更新的数据内容,whereClause参数表示指定更新的条件,whereArgs参数表示whereClause中占位符的值。 delete(...... )方法:该方法用于删除数据库表中的数据。它的基本格式为:delete(String table, String whereClause, String[] whereArgs)。其中,table参数表示要删除的表名,whereClause参数表示指定删除的条件,whereArgs参数表示whereClause中占位符的值。 execSQL(...... )方法:该方法用于执行SQL语句。它的基本格式为:execSQL(String sql, Object[] bindArgs)。其中,sql参数表示要执行的SQL语句,bindArgs参数表示SQL语句中的参数值。 query(...... )方法:该方法用于查询数据库表中的数据。它的基本格式为:query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)。其中,table参数表示要查询的表名,columns参数表示要查询的列名,selection参数表示查询的条件,selectionArgs参数表示selection中占位符的值,groupBy参数表示分组的列,having参数表示筛选分组的条件,orderBy参数表示排序的列。 以上是SQLiteDatabase类中常用的几个方法及其基本格式的介绍,通过这些方法可以方便地对数据库进行数据的插入、更新删除、执行SQL语句和查询操作。 ### 回答3: insert(......)方法:该方法用于向数据库中插入一条新的数据记录。基本格式为:insert(String table, String nullColumnHack, ContentValues values),其中table为表名,nullColumnHack为可选的列名,values为要插入的数据。 update(......)方法:该方法用于更新数据库中已有的数据记录。基本格式为:update(String table, ContentValues values, String whereClause, String[] whereArgs),其中table为表名,values为要更新的数据,whereClause为更新条件,whereArgs为更新条件参数。 delete(......)方法:该方法用于删除数据库中的数据记录。基本格式为:delete(String table, String whereClause, String[] whereArgs),其中table为表名,whereClause为删除条件,whereArgs为删除条件参数。 execSQL(......)方法:该方法用于执行带参数的SQL语句。基本格式为:execSQL(String sql, Object[] bindArgs),其中sql为要执行的SQL语句,bindArgs为SQL语句中的参数。 query(......)方法:该方法用于查询数据库中的数据记录。基本格式为:query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit),其中table为表名,columns为要返回的列名,selection为查询条件,selectionArgs为查询条件参数,groupBy为分组条件,having为分组条件参数,orderBy为排序条件,limit为限制返回结果数量。 这些方法都是SQLiteDatabase类中的常用方法,通过调用这些方法可以方便地进行数据库操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TL。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值