【问题标题】:SQLite database: unknown error (code 14): Could not open databaseSQLite 数据库:未知错误(代码 14):无法打开数据库
【发布时间】:2015-02-16 18:58:49
【问题描述】:

我正在制作一个从另一个应用程序复制数据库文件然后编辑数据库的应用程序。

当我尝试打开 de SQLite 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)

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lstupenengo.primeraapp" >
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".FirstRunAlarm"/>
        <service android:name=".FirstRunService"/>
    </application>


</manifest>

【问题讨论】:

  • 尝试检查文件是否已创建或是否存在.. 并在清单中添加 WRITE_EXTERNAL_STORAGE PERMISSION
  • 添加清单以便您阅读,我有权限。尝试了 if (file.exist) 并且文件存在。还有其他想法吗?
  • 可以参考this吗?它说超级方法 shud 正确引用了路径。
  • 我们通常做的是创建一个扩展 Sqliteopenhelper 的类。在其中调用 super 方法...所以所有的复制 uv 都可以在那里完成,你所要做的就是使用此类的 obj 从服务中调用其中的方法..
  • 是的,你可以从thisthis 那里得到一个想法

标签: android sqlite opendatabase


【解决方案1】:

通过创建一个类,然后直接从服务调用该类来解决。

public class xxHandler extends SQLiteOpenHelper {

    Context context;
    SQLiteDatabase xxdb;

    public wadbHandler(Context context1) {
        super(context1,"xx.db", null, 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", null,
                SQLiteDatabase.OPEN_READWRITE);
        wadb.execSQL("SELECT * FROM table;")

     }

【讨论】:

    【解决方案2】:

    首先检查数据库文件是否存在使用:

    File file = new File(myPath);    
    if (file.exists() && !file.isDirectory())
    

    【讨论】:

    • 我已经添加了同样的错误出现的行。文件存在返回 TRUE
    【解决方案3】:

    在 OpenDatabase 之前添加:

    file.setWritable(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多