android之SQLite的增删查改(个人觉得挺不错)

这篇博客详细介绍了SQLite数据库在Android中的使用,包括delete、insert、replace、update和query等基本操作。通过示例代码展示了如何进行数据的增删查改,以及创建和管理数据库的方法。
摘要由CSDN通过智能技术生成

先介绍一下几个方法:

 

1:删除

public int delete (String table,String whereClause, String[] whereArgs)

Convenience method for deleting rows in the database.

Parameters
table the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
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.

2, 插入

public long insert (String table,String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database.

Parameters
table the table to insert the row into
nullColumnHack SQL doesn't allow inserting a completely empty row, so if initialValues is empty this column will explicitly be assigned a NULL value
values this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns
  • the row ID of the newly inserted row, or -1 if an error occurred
3 修改
public long replace (String table,String nullColumnHack, ContentValues initialValues)

Convenience method for replacing a row in the database.

Parameters
table the table in which to replace the row
nullColumnHack SQL doesn't allow inserting a completely empty row, so if initialValues is empty this row will explicitly be assigned a NULL value
initialValues this map contains the initial column values for the row. The key
Returns
  • the row ID of the newly inserted row, or -1 if an error occurred

4 更新
public int update (String table,ContentValues values, String whereClause, String[] whereArgs)

Convenience method for updating rows in the database.

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.
whereClause the optional WHERE clause to apply when updating. Passing null will update all rows.
Returns
  • the number of rows affected

5 查询
public Cursor query (String table,String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Query the given table, returning a Cursor over the result set.

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.
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.
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.
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.
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.
limit Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
  • A Cursor object, which is positioned before the first entry
See Also



privatestaticfinal StringDATABASE_CREATE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,SQLite是一个常用的本地数据库,用于存储应用的数据。使用SQLite进行增删查改(CRUD)操作,可以通过Android的Java或Kotlin API来实现。以下是一些基本步骤: 1. **创建数据库**: - 首先,在`res`目录下创建`database`文件夹,然后在其中创建一个`.db`文件(例如`app.db`),这是SQLite文件的实际位置。 - 在AndroidManifest.xml中添加权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 2. **连接数据库**: - 使用`SQLiteOpenHelper`类来管理数据库版本和事务。创建一个继承自它的子类,并重写`onCreate()`和`onUpgrade()`方法。 ```java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "app.db"; private static final int DATABASE_VERSION = 1; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // ... } ``` 3. **CRUD操作**: - **插入数据** (Insert): - 创建SQL INSERT语句并调用`insert()`方法。 ```java ContentValues contentValues = new ContentValues(); contentValues.put("column_name", "value"); dbHelper.getWritableDatabase().insert("table_name", null, contentValues); ``` - **查询数据** (Read): - 使用`query()`方法执行SQL SELECT查询。 ```java Cursor cursor = dbHelper.rawQuery("SELECT * FROM table_name WHERE column_name = ?", new String[]{"value"}); while (cursor.moveToNext()) { // 获取结果 } cursor.close(); ``` - **更新数据** (Update): - 构造SQL UPDATE语句,并使用`update()`方法。 ```java int rowsAffected = dbHelper.getWritableDatabase().update("table_name", contentValues, "column_name = ?", new String[]{"value"}); ``` - **删除数据** (Delete): - 构造SQL DELETE语句,然后调用`delete()`方法。 ```java int deletedRows = dbHelper.getWritableDatabase().delete("table_name", "column_name = ?", new String[]{"value"}); ``` 4. **事务处理**: - 如果需要确保一组操作要么全部成功要么全部失败,可以使用`beginTransaction()`、`endTransaction()`以及`setTransactionSuccessful()`或`rollback()`。 5. **关闭连接**: - 在完成所有操作后,记得关闭`Cursor`和数据库连接。 ```java cursor.close(); dbHelper.close(); ``` **相关问题--:** 1. Android Studio中的SQLiteOpenHelper类有何作用? 2. 如何在Android Studio中执行带有参数的SQL查询? 3. 在Android中,如何正确处理数据库操作的异常?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值