0
我可以知道爲什麼當我運行這個項目它出了這個錯誤和設備上的應用程序是不幸的,應用程序已停止。錯誤是。 android.database.sqlite.SQLiteCantOpenDatabaseException:未知錯誤(代碼14):無法打開數據庫錯誤與Android工作室打開SQLite數據庫
package dijkstra.app.com.demodistancev8;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
//http://cariprogram.blogspot.com
public class SQLHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "schoolnav.sqlite";
private static final int DATABASE_VERSION = 1;
private static String DB_PATH = "/data/data/com.app.dijkstra/databases/";
private Context myContext;
public SQLHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
myContext=context;
}
public void createDataBase() throws IOException{
if(DataBaseisExist()){
//do nothing - database already exist
Toast.makeText(myContext, "Database Sudah Ada", Toast.LENGTH_LONG).show();
}
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();
Toast.makeText(myContext, "Database Berhasil Diimport Dari Assets", Toast.LENGTH_LONG).show();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean DataBaseisExist(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
if(checkDB != null)return true ;else return false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_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();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}}
<?xml version="1.0" encoding="utf-8"?>
<permission android:name="dijkstra.app.com.demodistancev8.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="dijkstra.app.com.demodistancev8.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我使用Android工作室建設項目 –
你不應該固定DB_PATH作爲。相反,您可以在運行時構建DB_PATH以提取您的應用程序數據路徑。 – GiapLee
@GiapLee你能教我什麼我需要糾正? –