'没有这样的表'SqliteDatabase

时间:2018-06-29 06:08:44

标签: java android sqlite gradle

我总是会收到此错误。我的数据库是我自己创建的。我不是用我的意思创建代码的。它是从资产文件夹创建的,如果我通过手机上的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) {

    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用大写的表名“ CATEGORY”。如果这样做不起作用,请查看您是要真正打开自己创建的数据库还是刚刚打开一个新数据库。

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

SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name;

更新

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

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

} 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);
相关问题