Android下SQLite数据库学习笔记3——使用系统API实现数据库的增删改查

来源:互联网 发布:淘宝图片上传大小 编辑:程序博客网 时间:2024/04/29 05:08
  • 我们可以在代码中自己添加查询语句,但是不可避免的会发生错误,比如标点符号的问题,空格的问题,当自己的SQL语句写得多了,调试起来未免太过麻烦。所以,尽可能使用系统提供的API来操作数据库,同时也方便对结果进行观察。

添加数据

  • db.insert(table, nullColumnHack, values);
    参数:

    table:the table to insert the row into(要插入记录的表名)

    nullColumnHack:optional; may be null. SQL doesn’t allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can’t be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.(当你插入一条完全为空的数据时,这个参数就会指定你所设置的那个字段的值为NULL,一般忽略此项,为NULL)

    values:this map contains the initial column values for the row. The keys should be the column names and the values the column values(所要插入数据的MAP集合,key就是字段名,value就是字段的值。value的数据类型为ConttentValues,内部实现了map)

    Returns:
    the row ID of the newly inserted row, or -1 if an error occurred(此方法的返回值为long类型的row ID,如果返回值为-1,则表示,数据没有添加成功。利用这个特性,可以方便的知道我们的数据有没有插入成功)

查询数据

  • db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
  • ~~~这个方法参数太多了;参数介绍
    Parameters:

    table:The table name to compile the query against.(表名)

    columns:A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn’t going to be used.(你所要查询的字段名,如果写null就代表查询所有*)

    selection:A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.(查询的where条件语句,但不包含where)

    selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.(前方用到的占位符对应的字符型数组)

    groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.(GroupBy可设为null)

    having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.(可设为null)

    orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.(可设为null)
    Returns:A Cursor object(依然为一个Cursor对象)

修改数据

  • db.update(table, values, whereClause, whereArgs);
  • 参数介绍
    Parameters:

    table:the table to update in(表名)

    values:a map from column names to new column values. null is a valid value that will be translated to NULL.(需要修改的值,map对象)

    whereClause:the optional WHERE clause to apply when updating. Passing null will update all rows.(z指定的where条件语句,还是不包括where,如果设为null,就修改所有的数据对应的字段)

    whereArgs:对应的String类型的数组

    Returns:
    the number of rows affected(返回受影响的行数)

删除数据

  • db.delete(table, whereClause, whereArgs);
  • 参数介绍

    Parameters:

    table:the table to delete from

    whereClause:the optional WHERE clause to apply when deleting. Passing null will delete all rows.

    whereArgs

    Returns:
    the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass “1” as the whereClause.


  • 这些方法中的参数,在系统执行的时候,会组装成对应的SQL语句对数据库进行操作。
0 0