我想将数据库与apk文件一起存储,但当我尝试对路径进行硬编码时,应用程序似乎找不到数据库。
我使用它来对路径进行硬编码但不起作用。
private final String DB_PATH="/data/data/com.example.shopkart/databases/";
private final String Db_NAME = "dbshopkart.db";
如何使用Environment.getExternalStorageDirectory().getAbsolutePath()
请帮助..
编辑:我的数据管理员课程
public class openingclass extends SQLiteOpenHelper {
public openingclass(Context c) {
super(c, Db_NAME, null, DB_VERSION);
createDataBase();
}
public void createDataBase() {
boolean dbExist;
try {
dbExist = checkDataBase();
} catch (SQLiteException e) {
e.printStackTrace();
throw new Error("database dose not exist");
}
if (dbExist) {
//do nothing - database already exist
} else {
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Error copying database");
}
//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();
}
}
/**
* 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.
throw new Error("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 {
//copyDataBase();
//Open your local db as the input stream
InputStream myInput = c1.getAssets().open(Db_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + "/" + Db_NAME;
File databaseFile = new File(DB_PATH);
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()) {
databaseFile.mkdir();
}
//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();
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
logcat的:
05-03 11:26:21.713: E/Database(4612): sqlite3_open_v2("/data/data/com.example.shopkart/databases/dbshopkart.db", &handle, 1, NULL) failed
05-03 11:26:21.713: W/dalvikvm(4612): threadid=1: thread exiting with uncaught exception (group=0x40015578)
05-03 11:26:21.721: E/AndroidRuntime(4612): FATAL EXCEPTION: main
05-03 11:26:21.721: E/AndroidRuntime(4612): java.lang.Error: database does't exist yet.
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.example.shopkart.datamanager$openingclass.checkDataBase(datamanager.java:114)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.example.shopkart.datamanager$openingclass.createDataBase(datamanager.java:65)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.example.shopkart.datamanager$openingclass.<init>(datamanager.java:57)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.example.shopkart.datamanager.<init>(datamanager.java:48)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.example.shopkart.MainActivity.onCreate(MainActivity.java:50)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.os.Looper.loop(Looper.java:130)
05-03 11:26:21.721: E/AndroidRuntime(4612): at android.app.ActivityThread.main(ActivityThread.java:3689)
05-03 11:26:21.721: E/AndroidRuntime(4612): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:26:21.721: E/AndroidRuntime(4612): at java.lang.reflect.Method.invoke(Method.java:507)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-03 11:26:21.721: E/AndroidRuntime(4612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-03 11:26:21.721: E/AndroidRuntime(4612): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您应该使用方法getDatabasePath()
。
由于您在构造函数中将参数c
作为参数发送,因此可以使用c.getDatabasePath()`获取路径。如果您需要在其他地方使用它,只需将其保存为类成员即可。或者,您可以将上下文保存为成员,并稍后在调用中使用该成员。
答案 1 :(得分:1)
所有应用程序(根或不是)都有一个默认数据目录,即/data/data/<package_name>
。默认情况下,应用程序数据库,设置和所有其他数据都会显示在此处。
默认情况下,您的数据库仅存储在data/data/com.example.shopkart/databases/
目录中。您可以使用FileExplorer
查看它。无需指定显式路径。
因此,您不需要指定路径"/data/data/com.example.shopkart/databases/";
。关于Environment.getExternalStorageDirectory().getAbsolutePath()
的问题因此用于指定外部存储路径。
如果您希望将数据库存储在SD卡中,那么只有您可以使用Environment.getExternalStorageDirectory().getAbsolutePath()
,否则您的数据库默认存储在/data/data/com.example.shopkart/databases/"
,但您没有指定。
查看Directory structure您将了解它。
<强>编辑:强>
为您的数据库助手类试用以下代码。
public class DataBaseHelper extends SQLiteOpenHelper{
private Context mycontext;
private static String DB_NAME ="dbshopkart.db"; //the extension may be .sqlite or .db
public SQLiteDatabase myDataBase;
public DataBaseHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
ContextWrapper cw =new ContextWrapper(getApplicationContext());
private static String DB_PATH =cw.getFilesDir().getAbsolutePath() + "/Database/";
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println("Database exists");
opendatabase();
}
else
{
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException{
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copydatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath =DB_PATH +DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
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 byte to inputfile to 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_READWRITE);
}
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}