• Android_SQLite简单的增删改查


    SQLite数据库,和其他的SQL数据库不同, 我们并不需要在手机上另外安装一个数据库软件,Android系统已经集成了这个数据库,我们无需像 使用其他数据库软件(Oracle,MSSQL,MySql等)又要安装,然后完成相关配置,又要改端口之类的是一个轻量级的关系型数据库,运算速度快,占用资源少,很适合在移动设备上使用, 不仅支持标准SQL语法,还遵循ACID(数据库事务)原则,无需账号,使用方便!

     

    SQLiteOpenHelper 抽象类,在这里完成数据库的命名创建和更新

    SQLiteDatabase   通过该类进行增删改查等操作

    Cursor            resultset结果集

     

    首先要具体化openhelper那个抽象类

    增删改查数据库语句:

    增:insert into 表名 values (?,?,?)

    删:delete from 表名 where 条件

    改:update 表名 set 列名 = ?where 条件

    查:select * from 表名  

    语法与其他数据库语句基本相同,不多写了。

    示例代码:

    插入数据:

     SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

        db.execSQL("INSERT INTO person(name,phone) values(?,?)",

                    new String[]{p.getName(),p.getPhone()});

     

    删除数据:

    public void delete(Integer id){

        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

        db.execSQL("DELETE FROM person WHERE personid = ?",

                    new String[]{id});}

    修改数据:

    public void update(Person p){

        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

        db.execSQL("UPDATE person SET name = ?,phone = ? WHERE personid = ?",

            new String[]{p.getName(),p.getPhone(),p.getId()});}

    查询数据:

    public Person find(Integer id){

        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();

        Cursor cursor =  db.rawQuery("SELECT * FROM person WHERE personid = ?",

                new String[]{id.toString()});

        //存在数据才返回true

        if(cursor.moveToFirst())

        {

            int personid = cursor.getInt(cursor.getColumnIndex("personid"));

            String name = cursor.getString(cursor.getColumnIndex("name"));

            String phone = cursor.getString(cursor.getColumnIndex("phone"));

            return new Person(personid,name,phone);

        }

        cursor.close();

        return null;}

  • 相关阅读:
    jedis异常:Could not get a resource from the pool
    redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resourc
    【redis】常见JedisConnectionException异常分析
    JAVA,字符串首字母转大写(高效率)
    Type handler was null on parameter mapping for property 'products'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified
    java读取blob,clob转换为字符串
    mybatis对blob属性数据的处理
    mybatis读取oracle中blob
    jpa2.x的getOne()/findOne()/findById()的区别及使用
    IDEA如何导入导出出settings配置文件
  • 原文地址:https://www.cnblogs.com/XiaoGao128/p/12269401.html
Copyright © 2020-2023  润新知