我正在制作一个应用程序,从另一个应用程序复制db文件,然后编辑数据库.
android.database.sqlite.sqliteCantOpenDatabaseException: unknown error (code 14): Could not open database
尝试了我在INTERNET上看到的很多不同的方法,没有任何作用.
这是我的代码:
public class FirstRunService extends IntentService{
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* Service Used to name the worker thread,important only for debugging.
*/
public FirstRunService() {
super("Service");
}
String filesdir;
SharedPreferences sp;
@Override
protected void onHandleIntent(Intent intent) {
//Cargo SharedPreferences
sp = this.getSharedPreferences("FirstRun",Context.MODE_PRIVATE);
//Asigno filesdir
filesdir = intent.getStringExtra("filesdir");
//Subo XX
Boolean XX = sp.getBoolean("XX",false);
if(!XX) {
uploadxxs(intent);
}
}
protected void uploadXX (Intent intent){
//Preparo
sqliteDatabase db;
myFTP myftp = new myFTP();
FTPClient client;
//Copio la Base de datos
RootTools.deleteFileOrDirectory(filesdir + "/XX.db",true);
RootTools.copyFile("/data/data/com.XX/databases/XX.db",filesdir+"/xx.db",true,false);
try {
RootTools.getShell(true).add(cmd);
} catch (TimeoutException e) {
e.printStackTrace();
Log.e("Root XX.db","Fallo");
FirstRunAlarm.completeWakefulIntent(intent);
stopService(intent);
} catch (RootDeniedException e) {
e.printStackTrace();
Log.e("Root XX.db","Fallo");
FirstRunAlarm.completeWakefulIntent(intent);
stopService(intent);
} catch (IOException e) {
e.printStackTrace();
Log.e("Root XX.db","Fallo");
FirstRunAlarm.completeWakefulIntent(intent);
stopService(intent);
}
//Edito db
//Edito db
File file = new File(filesdir+"/wa.db");
if (file.exists()){
Log.e("File","Existe");
db = sqliteDatabase.openDatabase(filesdir+"/wa.db",null,sqliteDatabase.OPEN_READWRITE);//Error in this line
}else{
db = null;
Log.e("File","No Existe");
}
db.execsql("DROP TABLE android_Metadata;");
logcat的:
12-17 22:38:17.401 15227-15242/com.lstupenengo.primeraapp E/File﹕ Existe
12-17 22:38:17.402 3076-3366/com.lstupenengo.primeraapp E/sqliteLog﹕ (14) cannot open file at line 30192 of [00bb9c9ce4]
12-17 22:38:17.402 3076-3366/com.lstupenengo.primeraapp E/sqliteLog﹕ (14) os_unix.c:30192: (13) open(//data/data/com.lstupenengo.primeraapp/files/XX.db) -
12-17 22:38:17.412 3076-3366/com.lstupenengo.primeraapp E/sqliteDatabase﹕ Failed to open database 'data/data/com.lstupenengo.primeraapp/files/XX.db'.
android.database.sqlite.sqliteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.sqliteConnection.nativeOpen(Native Method)
at android.database.sqlite.sqliteConnection.open(sqliteConnection.java:209)
at android.database.sqlite.sqliteConnection.open(sqliteConnection.java:193)
at android.database.sqlite.sqliteConnectionPool.openConnectionLocked(sqliteConnectionPool.java:463)
at android.database.sqlite.sqliteConnectionPool.open(sqliteConnectionPool.java:185)
at android.database.sqlite.sqliteConnectionPool.open(sqliteConnectionPool.java:177)
at android.database.sqlite.sqliteDatabase.openInner(sqliteDatabase.java:804)
at android.database.sqlite.sqliteDatabase.open(sqliteDatabase.java:789)
at android.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:694)
at android.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:669)
at com.lstupenengo.primeraapp.FirstRunService.uploadXX(FirstRunService.java:105)
at com.lstupenengo.primeraapp.FirstRunService.onHandleIntent(FirstRunService.java:49)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.os.HandlerThread.run(HandlerThread.java:61)
12-17 22:38:17.412 3076-3366/com.lstupenengo.primeraapp W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41648d88)
12-17 22:38:17.422 3076-3366/com.lstupenengo.primeraapp E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[Service]
Process: com.lstupenengo.primeraapp,PID: 3076
android.database.sqlite.sqliteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.sqliteConnection.nativeOpen(Native Method)
at android.database.sqlite.sqliteConnection.open(sqliteConnection.java:209)
at android.database.sqlite.sqliteConnection.open(sqliteConnection.java:193)
at android.database.sqlite.sqliteConnectionPool.openConnectionLocked(sqliteConnectionPool.java:463)
at android.database.sqlite.sqliteConnectionPool.open(sqliteConnectionPool.java:185)
at android.database.sqlite.sqliteConnectionPool.open(sqliteConnectionPool.java:177)
at android.database.sqlite.sqliteDatabase.openInner(sqliteDatabase.java:804)
at android.database.sqlite.sqliteDatabase.open(sqliteDatabase.java:789)
at android.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:694)
at android.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:669)
at com.lstupenengo.primeraapp.FirstRunService.uploadXX(FirstRunService.java:105)
at com.lstupenengo.primeraapp.FirstRunService.onHandleIntent(FirstRunService.java:49)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.os.HandlerThread.run(HandlerThread.java:61)
表现:
最佳答案
通过创建一个类来解决,然后直接从服务调用该类.
原文链接:https://www.f2er.com/android/431188.html类
public class xxHandler extends sqliteOpenHelper {
Context context;
sqliteDatabase xxdb;
public wadbHandler(Context context1) {
super(context1,"xx.db",1);
context = context1;
}
// Creating Tables
@Override
public void onCreate(sqliteDatabase db) {
}
// Upgrading database
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
}
public void accion(sqliteDatabase db){
wadb = sqliteDatabase.openDatabase(context.getFilesDir()+"/xx.db",sqliteDatabase.OPEN_READWRITE);
wadb.execsql("SELECT * FROM table;")
}