【发布时间】:2019-03-06 14:28:50
【问题描述】:
我最近在几年前编写的应用程序上迁移到 Studio 3.3,但我用于从 Assets 文件夹复制填充的 SQLite db 的代码似乎不再有效。下面是一个简单的测试应用。
数据库名称是 SteelSectionProperties,它有 1 个用户表,SectionProps 和 android_metadata 表
活动调用了一个典型的助手,但没有找到 SectionProps 表。
我是 Android 的初学者。我错过了一些简单/明显的东西吗?
调试输出是; E/helper checkdb 路径:/data/data/com.silverfernsolutions.steelsections/databases/SteelSectionProperties
E/helper checkdb DB=: SQLiteDatabase: /data/data/com.silverfernsolutions.steelsections/databases/SteelSectionProperties
E/createDB.: dbExists=true
E/helper openDB 路径:/data/data/com.silverfernsolutions.steelsections/databases/SteelSectionProperties
E/helper openDataBase:在 SQLiteDatabase.openDatabase 之后
I/System.out: TABLE - android_metadata
I/System.out:COLUMN - 语言环境
E/SQLiteLog: (1) 没有这样的表:SectionProps
public class CopyDbActivity extends AppCompatActivity {
Cursor c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy_db);
DataBaseHelperReign myDbHelper = new DataBaseHelperReign(this);
myDbHelper = new DataBaseHelperReign(this);
try {
myDbHelper.createDataBase();
}catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
//This does not find the table SectionProps???
//Only the android_metadata table
myDbHelper.getDatabaseStructure();
//This causes a crash
myDbHelper.getTablecontents("SectionProps");
}
助手是
public class DataBaseHelperReign extends SQLiteOpenHelper {
//The Android's default system path of your application database.
//private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
//private static String DB_NAME = "myDBName";
private static String DB_PATH = "/data/data/com.silverfernsolutions.steelsections/databases/";
private static String DB_NAME = "SteelSectionProperties";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
*
* @param context
*/
public DataBaseHelperReign(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
//do nothing - database already exist
Log.e("createDB.", " dbExists=true");
} else {
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
Log.e("copying db helper ", "db copied");
} catch (IOException e) {
Log.e("copying db helper ", "Error copying DB");
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
*
* @return true if it exists, false if it doesn't
*/
/*Larrybud says:
June 17, 2011 at 2:20 am
Nice code, but a better way to get the full path of the file would be to do:
File fdb=getDatabasePath(DATABASE_NAME);
return fdb.getAbsolutePath();
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
Log.e("helper checkdb path", myPath);
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.e("helper checkdb DB= ", checkDB.toString());
} catch (SQLiteException e) {
Log.e("helper checkdb", "Error " + e.toString());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
*/
private void copyDataBase() throws IOException {
Log.e("helper copyDB ", " opening input stream");
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
Log.e("helper outfileName", outFileName);
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
Log.e("helper openDB path ", myPath);
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.e("helper openDataBase ", "after SQLiteDatabase.openDatabase");
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getTablecontents(String table){
String q = "SELECT * FROM " + table ;
Cursor mCursor = myDataBase.rawQuery(q, null);
return mCursor;
}
public ArrayList<String[]> getDatabaseStructure() {
Cursor c = myDataBase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
ArrayList<String[]> result = new ArrayList<String[]>();
int i = 0;
result.add(c.getColumnNames());
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String[] temp = new String[c.getColumnCount()];
for (i = 0; i < temp.length; i++) {
temp[i] = c.getString(i);
System.out.println("TABLE - " + temp[i]);
Cursor c1 = myDataBase.rawQuery(
"SELECT * FROM " + temp[i], null);
c1.moveToFirst();
String[] COLUMNS = c1.getColumnNames();
for (int j = 0; j < COLUMNS.length; j++) {
c1.move(j);
System.out.println(" COLUMN - " + COLUMNS[j]);
}
}
result.add(temp);
}
return result;
}
}
【问题讨论】: