我第一次使用android中的SQLite开发。 Currenly,我有现有的sqlite数据库,我想在资产文件夹中复制粘贴,然后阅读它。 但我面临以下问题:
我的位置:
09-21 13:08:23.725: I/Database(11660): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-21 13:08:23.784: E/Database(11660): sqlite3_open_v2("/data/data/YOUR_PACKAGE/databases/myDBName", &handle, 1, NULL) failed
09-21 13:08:23.866: I/In createDataBase();IOException(11660): myDBName
我在google上搜索了它,得到了解决方案并根据它更新了我的代码,但仍面临问题。我无法解决问题所在。
我的代码如下:
public class DataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.example.androidtestapplication/databases/";
private static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}
else{
this.getReadableDatabase();
try {
copyDataBase();
}
catch (IOException e) {
//throw new Error("Error copying database");
Log.i("In createDataBase();IOException",e.getMessage());
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//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;
//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;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@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) {
}
}
MainActivity.java文件是:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
}
catch (IOException ioe) {
//throw new Error("Unable to create database");
Log.i("In MainActivity:IOException",ioe.getMessage());
}
try {
myDbHelper.openDataBase();
}
catch(SQLException sqle){
//throw sqle;
Log.i("In MainActivity:SQLException",sqle.getMessage());
}
}
}
我还在清单文件中添加了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我已按照本教程操作: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
请告诉我这里缺少的东西。
请帮助我......提前致谢... :)