Я всегда получаю эту ошибку. Моя база данных создана мной. Я не создаю это с кодом, я имею в виду. Он создает его из папки активов, и если я открываю его с помощью приложения для чтения 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) {
}
}