【问题标题】:Android: SQLite database not in internal memoryAndroid:SQLite 数据库不在内存中
【发布时间】:2014-08-08 23:17:13
【问题描述】:

我正在编写一个在运行时创建和填充 SQLite 数据库的 Android 应用程序。在类似的应用程序中,数据库会自动创建在“/data/data//databases/”目录中。然而,这一次并没有发生。我已经通过 adb shell 进入“/data/data/”,但是没有 /databases/ 目录,更不用说那里的数据库了。但是,我知道我的数据库正在创建和填充,因为我打印出前十个条目进行检查。它还能存放在哪里?我有 WRITE_EXTERNAL_STORAGE 权限,我的代码没有错误。

如果有帮助,这是我的数据库助手类(相关部分):

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;

    private static int colNum;
    private static String dbName, tableName, filePath;
    private static String[] cols;
    private final Context myContext;

    public DatabaseHelper(Context context) {
        super(context, dbName, null, VERSION);
        this.myContext = context;
    }


    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

        FileReader fr = null;
        try {
            File dbFile = new File(filePath);
            fr = new FileReader(dbFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader buffer;
        buffer = new BufferedReader(fr);
        String line = "";
        String firstLine;

        try {
            firstLine = buffer.readLine();
            cols = firstLine.split(",");    // get name of columns from header (first row of .csv)
            colNum = cols.length;           // number of columns in each row of the file
        } catch (IOException e) {
            e.printStackTrace();
        }

        String table_col = getTableCols(colNum, cols);

        db.execSQL("DROP TABLE IF EXISTS " + tableName + ";");
        String create = "CREATE TABLE " + tableName + table_col;
        db.execSQL(create);

        populateDB(db, buffer, line);
    }

    public void populateDB(SQLiteDatabase db, BufferedReader buffer, String line) {

        db.beginTransaction();
        try {
            while ((line = buffer.readLine()) != null) {
                String[] row = line.split(",");
                ContentValues cv = new ContentValues();
                for (int i = 0; i < colNum; i++) {
                    cv.put(cols[i], row[i].trim());
                }
                db.insert(tableName, null, cv);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    public String getTableCols(Integer num, String[] cols) {
        String table_col = "(";
        for (int i = 0; i < (num - 1); i++) {
            table_col += (cols[i] + " text, ");
        }
        table_col += cols[num - 1] + " text)";
        return table_col;
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + tableName + ";");
        //Create tables again
        onCreate(db);
    }


}

【问题讨论】:

    标签: android database sqlite memory


    【解决方案1】:

    数据库在您的应用程序特定数据目录下的databases 中创建。在许多但不是所有固件上,数据目录为/data/data/your.package.name,因此数据库位置为/data/data/your.package.name/databases/

    应用程序数据目录受文件系统权限保护。除非您有 root 权限或者您正在使用包的 uid 运行,否则您无法访问它。

    【讨论】:

    • 但这很奇怪 - 目录 /data/data/my.package.name/databases/ 根本不存在。我一直通过使用我的包 uid 运行来访问它,但那里什么都没有。
    • 你是如何调用数据库助手的?您需要get...Database() 方法之一来实际创建数据库。此外,人口代码中的事务可能不是一个好主意。 sqlite helper 生命周期回调已经包含在事务中,sqlite 本身不支持嵌套事务,Android 解决这些限制的尝试充其量是不稳定的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多