package com.example.expdb;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class RestauDBHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.expdb/databases/";
private static String DB_NAME = "restauDB";
private SQLiteDatabase restauBase;
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 RestauDBHelper(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
} 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.
restauBase = this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
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
*/
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;
}
/**
* 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 {
String text;
// InputStream is = this.getResources().openRawResource (R.raw.sql);
try {
InputStream myInput = myContext.getResources().openRawResource(
R.raw.restau_db);
// We guarantee that the available method returns the total
// size of the asset... of course, this does mean that a single
// asset can't be more than 2 gigs.
int size = myInput.available();
String outFileName = DB_PATH + DB_NAME;
//gettting failed to open file exception here , i can see DB file iscreated //through file explorer .Its present.
OutputStream myOutput = new FileOutputStream(outFileName);
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
restauBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (restauBase != null)
restauBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这是日志消息:
10-12 11:31:07.793: E/SQLiteLog(8679): (14) cannot open file at line 30174 of [00bb9c9ce4]
10-12 11:31:07.803: E/SQLiteLog(8679): (14) os_unix.c:30174: (2) open(/data/data/com.example.expdb/databases/restauDB) -
10-12 11:31:08.053: E/SQLiteDatabase(8679): Failed to open database '/data/data/com.example.expdb/databases/restauDB'.
10-12 11:31:08.053: E/SQLiteDatabase(8679): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at com.example.expdb.RestauDBHelper.checkDataBase(RestauDBHelper.java:71)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at com.example.expdb.RestauDBHelper.createDataBase(RestauDBHelper.java:41)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at com.example.expdb.MainActivity.onCreate(MainActivity.java:22)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.Activity.performCreate(Activity.java:5008)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.os.Looper.loop(Looper.java:137)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 11:31:08.053: E/SQLiteDatabase(8679): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
为什么你需要为数据库提供路径,它会自动从数据/数据中的应用程序包中获取数据库的实例。
您可以使用以下代码段打开并获取数据库实例: -
如果您在扩展SQLiteOpenHelper的类中,则使用
SQLiteDatabase db = this.getWritableDatabase();
如果您在其他地方,您可以实例化SQLiteOpenHelper,如下所示: -
RestauDBHelper dbHelper = new RestauDBHelper();
SQLiteDatabase db = dbHelper .getWritableDatabase();
通过这种方式,你可以打开你的数据库,如果你想以READ_ONLY模式打开,那么而不是
getWritableDatabase() make use of getReadableDatabase()
希望这会帮助你。祝你好运