【发布时间】: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