当前位置: 首页 > 知识库问答 >
问题:

代码14:无法打开数据库

危飞文
2023-03-14

我知道以前有人问过这个问题。但是,问题是,相同的代码(用于数据库处理程序)适用于另一个应用程序,但不是我目前正在开发的应用程序。我甚至通过在“设置”中检查权限来确保授予了权限。下面是日志:

05-13 15:35:45.693 29696-29696/com。实例乱劈校正器E/SQLiteLog:(14)无法打开[5a3022e081](14)os\U unix第31282行的文件。c: 31282:(21)打开(/data/user/0/com.example.hack.corrector/databases/)-05-13 15:35:45.694 29696-29696/com。实例乱劈更正程序E/SQLiteDatabase:未能打开数据库“/data/user/0/com”。实例乱劈校正器/数据库/'。Android数据库sqlite。SqliteContoPenDatabaseException:未知错误(代码14):无法在android上打开数据库。数据库sqlite。SQLITE连接。android上的nativeOpen(本机方法)。数据库sqlite。SQLITE连接。在android上打开(SQLiteConnection.java:207)。数据库sqlite。SQLITE连接。在android上打开(SQLiteConnection.java:191)。数据库sqlite。SQLiteConnectionPool。android上的openConnectionLocked(SQLiteConnectionPool.java:463)。数据库sqlite。SQLiteConnectionPool。在android上打开(SQLiteConnectionPool.java:185)。数据库sqlite。SQLiteConnectionPool。在android上打开(SQLiteConnectionPool.java:177)。数据库sqlite。SQLITE数据库。android上的openInner(SQLiteDatabase.java:806)。数据库sqlite。SQLITE数据库。在android上打开(SQLiteDatabase.java:791)。数据库sqlite。SQLITE数据库。android上的openDatabase(SQLiteDatabase.java:694)。数据库sqlite。SQLITE数据库。com上的openDatabase(SQLiteDatabase.java:669)。实例hakc。校正器。语音数据库。com上的openDataBase(VocabDatabase.java:127)。实例hakc。校正器。擦伤服务。在com上创建数据库(scrapeservice.java:31)。实例hakc。校正器。擦伤服务。android上的onStartCommand(scrapeservice.java:23)。应用程序。活动线程。android上的handleServiceArgs(ActivityThread.java:3049)。应用程序。活动线程。在Android上获得2300美元(ActivityThread.java:154)。应用程序。android上的ActivityThread$H.handleMessage(ActivityThread.java:1479)。操作系统。处理程序。android上的dispatchMessage(Handler.java:102)。操作系统。活套。android上的loop(Looper.java:157)。应用程序。活动线程。java上的main(ActivityThread.java:5571)。郎。反思。方法在com上调用(本机方法)。Android内部的操作系统。ZygoteInit$MethodandArgscaler。在com上运行(ZygoteInit.java:745)。Android内部的操作系统。合子岩。main(ZygoteInit.java:635)

下面是数据库处理程序代码:

package com.example.hack.corrector;

import android.content.ContentValues;
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;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class VocabDatabase extends SQLiteOpenHelper {

//The Android's default system path of your application database.
private static String DB_PATH = "";

private static String DB_NAME = "ztr.db";

private SQLiteDatabase myDataBase;

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 VocabDatabase(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    this.DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}

/**
 * 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.
        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() {
    this.getReadableDatabase();

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    } catch (SQLiteException e) {
        e.printStackTrace();
    }

    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 {

    //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 bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length = 0;
    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;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion) {
        try {
            copyDataBase();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.

//add your public methods for insert, get, delete and update data in database.

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}

public long insert(String table, String nullColumnHack, ContentValues contentValues) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.insert(table, nullColumnHack, contentValues);
}

public Cursor rawQuery(String string, String[] selectionArguments) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.rawQuery(string, selectionArguments);
}

}

我已经检查了文件资源管理器,数据库已经复制到位。但是,错误仍在发生。我从未遇到过其他实现相同数据库处理程序代码(Vocabdatabase)的应用程序的问题。我花了一天半的时间试图解决这个问题,但什么都没用。。。

共有1个答案

贺懿轩
2023-03-14

您的问题是,您试图使用不包含数据库名称的路径打开。

i、 e.无法打开14用于

/data/user/0/com。实例乱劈校正器/数据库/

同时打开应该尝试打开/data/user/0/com.example.hack.corrector/databases/ztr.db

不使用完整路径将导致两个问题,这很可能导致混淆。

  1. 当您检查数据库是否存在时,将发出消息(请注意,每次都会复制数据库,因为数据库永远不会被找到(打开))
  2. 当您尝试打开数据库时,也会发出这些消息,后者失败

在这两种情况下,正确的使用应该DB_PATHDB_NAME,而不仅仅是DB_PATH。

以下是重写数据库处理程序以合并上述内容,但也将对数据库的检查更改为对文件的检查,以便不显示非错误的打开错误14(从资产文件复制数据库时)。

  • 注释<代码>//

:-

public class VocabDatabase extends SQLiteOpenHelper {

    //The Android's default system path of your application database.
    //private static String DB_PATH = ""; //<<<< RMVD
    private static String DB_PATH_ALT; //<<<< ADDED
    private static String DB_NAME = "ztr.db";
    private SQLiteDatabase myDataBase;
    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 VocabDatabase(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
        //this.DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; //<<<< RMVD
        this.DB_PATH_ALT = context.getDatabasePath(DB_NAME).getPath(); //<<<< ADDED

    }

    /**
     * Creates a empty database on the system and rewrites it with your own database.
     */
    public void createDataBase() throws IOException {

        //boolean dbExist = checkDataBase();  //<<<< RMVD 
        boolean dbExist = checkDataBaseAlt(); //<<<< CHANGED
        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.
            this.getWritableDatabase();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }
    //<<<< ADDED Alternative method checks the file rather than database
    //<<<<       as such no open error 14 messages
    /**
     * 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 checkDataBaseAlt() {
        //File chkdb = new File(myContext.getDatabasePath(DB_NAME).getPath()); //<<<< RMVD
        File chkdb = new File(DB_PATH_ALT); //<<<< ADDED
        return chkdb.exists();
    }

    /**
     * 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() {
        this.getReadableDatabase();
        SQLiteDatabase checkDB = null;
        try {
            //String myPath = DB_PATH; //<<<< RMVD so no open error 14 uses alt method
            checkDB = SQLiteDatabase.openDatabase(
                    DB_PATH_ALT, //<<<< CHANGED
                    null, 
                    SQLiteDatabase.OPEN_READWRITE
            ); 

        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null; //<<<< simplified
    }

    /**
     * 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 {

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME); //<<<< CHANGED

        // 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(DB_PATH_ALT); //<<<< CHANGED

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length = 0;
        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; //<<<< RMVD
        myDataBase = SQLiteDatabase.openDatabase(
                DB_PATH_ALT, //<<<< CHANGED
                null, 
                SQLiteDatabase.OPEN_READWRITE
        );

    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            try {
                copyDataBase();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.

//add your public methods for insert, get, delete and update data in database.

    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }

    public long insert(String table, String nullColumnHack, ContentValues contentValues) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.insert(table, nullColumnHack, contentValues);
    }

    public Cursor rawQuery(String string, String[] selectionArguments) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.rawQuery(string, selectionArguments);
    }
}

 类似资料:
  • 问题内容: 问题:为什么我不能打开数据库? 信息:我正在使用数据库进行项目。我编写了一个测试程序,该程序运行并传递数据库: 单元测试程序可以使之毫无问题。但是,当我实际使用将相同位置传递给它的程序时,出现以下错误: OperationalError:无法打开数据库文件 我试着用: 在三种情况下,我遇到了以上错误。最令人沮丧的部分是事实可以做到这一点,但实际程序却做不到。 关于到底发生了什么的任何线

  • 问题内容: 在Django中设置服务器时出现此错误。它是sqlite3,这意味着它应该创建.db文件,但似乎没有这样做。我已经将SQLite规定为后端,并且将其放置在绝对的文件路径中,但是没有运气。 这是错误还是我做错了什么?(只是在想,在Ubuntu中指定的绝对文件路径是否有所不同?) 这是我的settings.py文件的开头: 问题答案: 问题你正在使用SQLite3,你的DATABASE_N

  • 我的具有: 有人知道吗? 更新:我再次尝试了一个教程在这里,并在一个异常页面中得到了相同的错误。 “处理请求时发生未处理的异常。SqliteException:SQLite错误14:”无法打开数据库文件“。Microsoft.Data.SQLite.SqliteException.ThrowExceptionForRC(int rc,sqlite3 db)”

  • 问题内容: 我正在尝试使用Java程序在本地控制我的Lotus Notes,以便为我自动发送电子邮件。尝试获取数据库对象时遇到以下问题。 我收到以下异常: 我已经将我的nsf文件和Notes.jar复制到了我的类路径中,谁知道这是什么问题? 问题答案: 需要检查的几件事。 第一次更改: 至: 如果仍然无法正常工作,请更改: 至: 我还建议养成回收Domino对象的习惯。

  • 我使用的是Oracle Database 12c Standard Edition 12.1.0.2.0-64位版本,在打开PDB数据库时遇到问题。 错误: 我正在使用下面提到的步骤创建Oracle数据库,这在Oracle企业版上运行良好。 http://dbarahul.blogspot.in/2017/02/manual-cdb-pdb-database-creation-steps.html

  • 我用的是Android平板,上面有Xamarin/C#应用。我试图用这款平板电脑访问驻留在我的Windows 10机器上的SQLite数据库。我选择使用USB/Android而不是Android模拟器,应用程序仍然驻留在Windows 10机器上。这似乎无关紧要,但我也尝试过映射驱动器、共享文件夹和创建网络连接。项目参考 这是代码: 命名空间CicoAndroid{公共部分类MainPage: C