При переходе в activity, в которой находится БД выскакивает ошибка: Unable to open the database file. Код данной activity
package com.example.chernovik;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class ActivityList extends AppCompatActivity {
TextView txtListView;
Button b1;
Button b2;
Button b3;
EditText eT;
ListView listView;
DataBaseHelper databaseHelper;
SimpleCursorAdapter userAdapter;
Cursor userCursor;
SQLiteDatabase db_sort;
long userId=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
b1 = findViewById(R.id.b1);
txtListView = findViewById(R.id.txtListView);
Intent intent = getIntent();
String outputString = intent.getStringExtra("name");
txtListView.setText(outputString);
databaseHelper = new DataBaseHelper(getApplicationContext());
//создаём БД
databaseHelper.create_db();
}
@Override
public void onResume() {
super.onResume();
// открываем подключение
db_sort = databaseHelper.open();
//получаем данные из бд в виде курсора
userCursor = db_sort.rawQuery("select * from "+ DataBaseHelper.TABLE, null);
// определяем, какие столбцы из курсора будут выводиться в ListView
String[] headers = new String[]{DataBaseHelper.COLUMN_NAME};
// создаем адаптер, передаем в него курсор
userAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
userCursor, headers, new int[]{android.R.id.text1}, 0);
listView.setAdapter(userAdapter);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues cv = new ContentValues();
cv.put(DataBaseHelper.DB_NAME, String.valueOf(eT.getText()));
db_sort.update(DataBaseHelper.TABLE, cv, DataBaseHelper.COLUMN_ID + "=" + String.valueOf(userId + 1), null);
userId = userId + 1;
db_sort.insert(DataBaseHelper.TABLE, null, cv);
}
});
b2 = findViewById(R.id.b2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db_sort.delete(DataBaseHelper.TABLE, "_id = ?", new String[]{String.valueOf(userId)});
}
});
b3 = findViewById(R.id.b3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
databaseHelper.close();
userCursor.close();
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
А так же ниже приведу код использования уже существующей БД
package com.example.chernovik;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH; // полный путь к базе данных
static String DB_NAME = "AESDataBase.db";
private static final int SCHEMA = 1; // версия базы данных
static final String TABLE = "users"; // название таблицы в бд
// названия столбцов
static final String COLUMN_ID = "_id";
static final String COLUMN_NAME = "name";
private Context myContext;
DataBaseHelper(Context context) {
super(context, DB_NAME, null, SCHEMA);
this.myContext=context;
DB_PATH =context.getFilesDir().getPath();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
void create_db(){
InputStream myInput;
OutputStream myOutput;
try {
File file = new File(DB_PATH);
if (!file.exists()) {
this.getReadableDatabase();
//получаем локальную бд как поток
myInput = myContext.getAssets().open(DB_NAME);
// Путь к новой бд
String outFileName = DB_PATH;
// Открываем пустую бд
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();
}
}
catch(IOException ex){
Log.d("DatabaseHelper", ex.getMessage());
}
}
SQLiteDatabase open()throws SQLException {
return SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
}
И лог самой ошибки
436-436/com.example.chernovik E/SQLiteDatabase: Failed to open database '/data/user/0/com.example.chernovik/files'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
#################################################################
Error Code : 14 (SQLITE_CANTOPEN)
Caused By : Unable to open the database file.
(unknown error (code 14): Could not open database)
#################################################################
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:242)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:203)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:518)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:209)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1073)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1018)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:799)
at com.example.chernovik.DataBaseHelper.open(DataBaseHelper.java:71)
at com.example.chernovik.ActivityList.onResume(ActivityList.java:54)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1277)
at android.app.Activity.performResume(Activity.java:7058)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3765)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2991)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
03-20 13:36:12.601 436-436/com.example.chernovik E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chernovik, PID: 436
java.lang.RuntimeException: Unable to resume activity {com.example.chernovik/com.example.chernovik.ActivityList}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
#################################################################
Error Code : 14 (SQLITE_CANTOPEN)
Caused By : Unable to open the database file.
(unknown error (code 14): Could not open database)
#################################################################
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3788)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2991)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
#################################################################
Error Code : 14 (SQLITE_CANTOPEN)
Caused By : Unable to open the database file.
(unknown error (code 14): Could not open database)
#################################################################
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:242)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:203)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:518)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:209)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1073)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1018)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:799)
at com.example.chernovik.DataBaseHelper.open(DataBaseHelper.java:71)
at com.example.chernovik.ActivityList.onResume(ActivityList.java:54)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1277)
at android.app.Activity.performResume(Activity.java:7058)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3765)
... 10 more
Виртуальный выделенный сервер (VDS) становится отличным выбором
Поставил последнюю версию драйвера для Postgesql, больше ничего не ставилПодключаю: