SQLite3 相关记录

1. 手机内安装 sqlite3 命令工具

  1. busybox 的下载,去应用商店安装
  2. 用命令 “uname -a” 查询当前手机 cpu 信息
shamu:/ $ uname -a
Linux localhost 3.10.40-gc7ebca13933 #1 SMP PREEMPT Wed Aug 16 15:25:44 UTC 2017 armv7l
  1. 在 https://github.com/tech128/sqlite3 网址下载 sqlite 文件,并拷贝到 /data/local/tmp
  2. 使用 MT 将 sqlite3 拷贝到 /system/xbin 路径内,权限设置

2. sqlite3 的使用

使用 sqlite3 查询数据库

# sqlite3 test.db
sqlite> .schema    // 查询表的属性值
CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE t_twobutton( _id INTEGER PRIMARY KEY AUTOINCREMENT, name);

sqlite> .tables    // 查询当前数据库的表
android_metadata  test

sqlite> .dump    // 导出数据库的所有信息
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;

过滤命令

sqlite> SELECT * FROM test;    //  过滤出 test 表的所有信息
1550042912147|test

sqlite> SELECT name FROM test;    // 过滤出 test 表的 name 属性
test

> SELECT count(*) FROM tets;    // 计算属性条目
1

3. android 数据库信息查询

直到 db 路径,直接查询

    public void queryPath() {
        try {
            String dataPath = mContext.getApplicationInfo().dataDir;
            String dbpasth = dataPath + "/databases/test.db";

            SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(dbpasth, null, SQLiteDatabase.OPEN_READONLY);
            if (!sqLiteDatabase.isOpen()) {
                return;
            }
            Cursor cursor =sqLiteDatabase.query("test", null, null, null, null, null, null);
            if (cursor.moveToFirst()){
                do {
                    int id=cursor.getInt(cursor.getColumnIndex("_id"));
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                }  while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

有趣的小问题,类似于下面的异常堆栈错误

06-10 23:07:01.831: E/Trace(4419): error opening trace file: No such file or directory (2)
06-10 23:07:03.611: E/SQLiteLog(4419): (14) cannot open file at line 30176 of [00bb9c9ce4]
06-10 23:07:03.621: E/SQLiteLog(4419): (14) os_unix.c:30176: (2) open(/data/data/com.example.quotes/databasesQuotesdb) - 
06-10 23:07:03.641: E/SQLiteDatabase(4419): Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'.
06-10 23:07:03.641: E/SQLiteDatabase(4419): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)

问题原因:
通过 MT 将 sdcard 数据库拷贝到 /data/data/ 路径下,然后 app 代码去操作数据库,这个时候权限是不对等的,app 代码没有权限去操作,所以出现这问题。

文件权限控制:

# chown u0_123 t.db    // 所有者
# chgrp u0_123 t.db    // 用户组

单这个是没用的,一样出现了问题

最后的解决是通过代码拷贝到沙盒路径

            File f = new File("/storage/emulated/0/t.db");

            String dataPath = mContext.getApplicationInfo().dataDir;
            String dbpasth = dataPath + "/123.db";

            // 文件拷贝
            if (f.exists()) {
                try {
                    FileOutputStream out = new FileOutputStream(dbpasth);
                    InputStream in = new FileInputStream(f);
                    byte[] buffer = new byte[1024];
                    int readBytes = 0;
                    while ((readBytes = in.read(buffer)) != -1)
                        out.write(buffer, 0, readBytes);
                    in.close();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
      SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(dbpasth, null, SQLiteDatabase.OPEN_READONLY);

你可能感兴趣的:(SQLite3 相关记录)