android-sqlite(浅解析)

来源:互联网 发布:淘宝股份有日本人多少 编辑:程序博客网 时间:2024/06/10 05:48
*个人目前从事的是我喜欢的工作,唯一的不顺心的是自己的提升很有限,提升的很艰难。不过,我对自己的成长很有信心,终有一天会从上层到底层彻底的打通的,大到框架,小到底层源码。*

android的数据存储方式有以下5种。

1 使用SharedPreferences存储数据2 文件存储数据      3 SQLite数据库存储数据4 使用ContentProvider存储数据5 网络存储数据

其中SharedPreference和文件、数据库存储算比较常见的存储方式。

今天,我们看的是sqlite,android设备内置数据库。

android.database.sqlite

这里写图片描述

Some device manufacturers include different versions of SQLite on their devices. There are two ways to programmatically determine the version number.
(大意:不同设备生产商内置的数据库版本不同,使用两种方法可以查看数据库版本)

If available, use the sqlite3 tool, for example: adb -e shell sqlite3 –version.
(主要的意思推荐我们使用sqlite3工具查看版本)

创建和查询内存数据库示例代码:

  String query = "select sqlite_version() AS sqlite_version";    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);    Cursor cursor = db.rawQuery(query, null);    String sqliteVersion = "";    if (cursor.moveToNext()) {        sqliteVersion = cursor.getString(0);    }
SQLiteDatabasepublic final class SQLiteDatabase extends SQLiteClosable java.lang.Object   ↳    android.database.sqlite.SQLiteClosable   ↳    android.database.sqlite.SQLiteDatabaseDatabase names must be unique within an application, not across all applications.(大意:数据库名称必须在应用程序中是唯一的,而不是在所有应用程序。)

create(创建)

SQLiteDatabase create (SQLiteDatabase.CursorFactory factory)

delete(删除)

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

deleteDatabase(删除数据库)

boolean deleteDatabase (File file)

Parameters
file File: The database file path.
Returns
boolean True if the database was successfully deleted.

execSQL

void execSQL (String sql)

getPath

String getPath ()
Gets the path to the database file.
(大意:获取数据库文件路径。)
Returns
String The path to the database file.

getVersion

int getVersion ()
Gets the database version.

Returns
int the database version

insert(插入)

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

query(查询)

Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)

update(更新)

int update (String table,
ContentValues values,
String whereClause,
String[] whereArgs)

好了,下期:

SQLiteOpenHelper

这里写图片描述

0 0
原创粉丝点击