zoukankan      html  css  js  c++  java
  • Android SQLite 添加、更新和删除行

    SQLiteDatabase类暴露了特定的方法,如insert、delete和update等方法,这些方法包装了执行这些动作所需的SQL语句。尽管如此,execSQL方法允许你在数据库表上执行任何有效的SQL语句,而这些动作就是你想要手动执行的。
     
    在任何时候,如果你修改了底层数据库的值,你都应该调用任一在当前表上浏览的Cursor的refreshQuery方法。
     
    插入新行
     
    为了创建一个新行,构造一个ContentValues对象,并使用它的put方法来为每一列提供值。通过在目标数据库对象上调用insert方法,并将ContentValues对象传入方法中来插入一个新行——需要有表的名称——如下面的片段所示:
     
    // Create a new row of values to insert.
    ContentValues newValues = new ContentValues();
     
    // Assign values for each row.
    newValues.put(COLUMN_NAME, newValue);
    [ ... Repeat for each column ... ]
     
    // Insert the row into your table
    myDatabase.insert(DATABASE_TABLE, null, newValues);
     
    更新行
     
    更新行也需要通过ContentValues来实现。
     
    创建一个新的ContentValues对象,使用put方法来为每一个你想更新的列指定新的值。调用数据库对象的update方法,传入表名和更新的ContentValues对象,一个where语言来指明哪些行需要更新。
     
    更新的处理在下面的片段中有演示:
     
    // Define the updated row content.
    ContentValues updatedValues = new ContentValues();
     
    // Assign values for each row.
    updatedValues.put(COLUMN_NAME, newValue);
    [ ... Repeat for each column ... ]
    String where = KEY_ID + “=” + rowId;
     
    // Update the row with the specified index with the new values.
    myDatabase.update(DATABASE_TABLE, updatedValues, where, null);
     
    删除行
     
    为了删除一行,你可以在数据库对象上简单调用delete方法,指定表名和一个where语句来指明那些行你想要删除,如下面的代码所示:
     
    myDatabase.delete(DATABASE_TABLE, KEY_ID + “=” + rowId, null);
  • 相关阅读:
    两个路由器配置静态路由只能单边 ping 通
    CVE202125646:Apache Druid远程命令执行漏洞复现
    批量修改图片的格式
    十大远程控制软件排名
    Splashtop 免费60天 大赠送
    单例设计模式
    蓄水池抽样算法/水塘采样算法
    kafka安装(单机版)
    LeetCode382链表随机节点
    LeetCode398随机数索引
  • 原文地址:https://www.cnblogs.com/top5/p/2439592.html
Copyright © 2011-2022 走看看