java - “没有这样的表”SqliteDatabase

标签 java android sqlite gradle

我总是收到此错误。我的数据库是我自己创建的。我的意思是,我不是用代码创建它的。它从 Assets 文件夹创建它,如果我通过手机上的 Sqlite 阅读器应用程序打开它,它会完美地显示表格和数据。但是我无法在我的项目中读取该数据库。它创建数据库但无法从应用程序读取。我的连接类是一个 .aar 文件,我将其添加到 gradle 中。这是我的帮助代码:

public class Connection extends SQLiteOpenHelper {
private static String _dbPath;
private static String _dbName;

private final Context _context;

public Connection(Context context, int dbVer, String dbPath, String dbName) {
    super(context, dbName, null, dbVer);
    this._context = context;
    _dbPath = dbPath;
    _dbName = dbName;

    CreateDatabase();
}

public void CreateDatabase() {
    boolean dbExist = CheckDatabase();

    if (!dbExist) {
        this.getReadableDatabase();
        try {
            CopyDatabase();
        } catch (IOException e) {
            throw new Error("Error.");
        }
    }
}

private boolean CheckDatabase() {
    SQLiteDatabase checkDB = null;

    try {
        String myPath = _dbPath + "/" + _dbName;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        if (checkDB != null) {
            checkDB.close();
        }
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void CopyDatabase() throws IOException {
    InputStream myInput = _context.getAssets().open(_dbName);

    String outFileName = _dbPath + "/" + _dbName;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}}

这是我的访问代码:

public static Cursor Select(Context context, Select select, ArrayList<Where> whereList) {
    String dbPath, dbName;
    Integer dbVer;
    SQLiteDatabase database;
    Cursor cursor;

    dbVer = (Integer) TDTools.ReturnResource(context, R.integer.dbVer, TDTools.ResType.Integer);
    dbName = (String) TDTools.ReturnResource(context, R.string.dbName, TDTools.ResType.String);
    dbPath = Environment.getExternalStorageDirectory() + (String) TDTools.ReturnResource(context, R.string.dbPath, TDTools.ResType.String);

    Connection con = new Connection(context, dbVer, dbPath, dbName);
    database = con.getReadableDatabase();

    WhereArgs whereArgs = WhereArgs.CreateArguments(whereList);

    if (select == null) {
        select = new Select();
    }

    //Here is where i get 'No Such Table' error...
    cursor = database.query("Category", select.getColumns(), whereArgs.getWhereClauses(), whereArgs.getArguments(), null, null, null, null);

    return cursor;
}

更新(帮助程序代码)

public class Connection extends SQLiteOpenHelper {
    public static SQLiteDatabase DataBase;

    private String _dbPath;
    private String _dbName;

    private final Context _context;

    public Connection(Context context, int dbVer, String dbPath, String dbName) {
        super(context, dbName, null, dbVer);
        this._context = context;
        this._dbPath = dbPath;
        this._dbName = dbName;

        CreateDatabase();
    }

    public void CreateDatabase() {
        boolean dbExist = CheckDatabase();

        if (!dbExist) {
            //this.getReadableDatabase();
            try {
                CopyDatabase();
            } catch (IOException e) {
                throw new Error("Veritabanı kopyalanamadı...");
            }
        }
    }

    private boolean CheckDatabase() {
        SQLiteDatabase checkDB = null;

        try {
            String myPath = this._dbPath + "/" + this._dbName;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            checkDB = null;
        }

        return checkDB != null ? true : false;
    }

    private void CopyDatabase() throws IOException {
        InputStream myInput = this._context.getAssets().open(this._dbName);

        String outFileName = this._dbPath + "/" + this._dbName;

        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

最佳答案

尝试大写表名“CATEGORY”。如果这不起作用,请查看您是否真正打开了创建的数据库或只是打开了一个新数据库。

您可以使用以下命令检查数据库的示例:

SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name;
<小时/>

更新

一般建议:将 logmeseages 添加到您的代码中,以便您可以了解正在发生的情况。

关于您的代码的一些想法:

} catch (SQLiteException e) {
    if (checkDB != null) {
        checkDB.close();
    }
}

可能您想在此处设置 chechDB = null,因为数据库无法打开并且已损坏(需要重新复制)。

if (!dbExist) {
    this.getReadableDatabase();
    try {
        CopyDatabase();
    } catch (IOException e) {
        throw new Error("Error.");
    }
}

为什么在复制数据库之前调用 this.getReadableDatabase()

<小时/>

更新2

您调用super(context, dbName, null, dbVer);,这意味着数据库文件“dbName”已打开。但是您将数据库复制到 this._dbPath + "/"+ this._dbName!您必须使用相同的路径。

super(context, dbPath + "/" + dbName, null, dbVer);

关于java - “没有这样的表”SqliteDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51095282/

相关文章:

java - 应用内购买后禁用广告

java - gradle:无法执行 run,因为属性 "mainClass"未定义或为空

Android:在 adb shell 上执行程序

sqlite - 如何从 sqlite 中的另一个数据库导入表?

linux - 如何判断 SQLite 在 Linux 上是否正确安装?

python从sqlite数据库导出csv文件,大写 "L"的值被截断

java - org.xml.sax.SAXParseException : The entity name must immediately follow the '&' in the entity reference when tomcat server run in eclipse

java - 有哪些 Java 库提供了从给定字符集生成唯一随机字符串组合的功能?

android - 没有单元 ID(AdMob 发布商 ID)的 Google adMob,仅用于测试

java - 客户端无法连接到服务器套接字 - Android