Основное действие класса
Код: Выделить всё
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowBD = findViewById(R.id.btnShowBD);
listBD = findViewById(R.id.listBD);
bdHelper = new BDHelper(this);
try {
database = bdHelper.getWritableDatabase();
} catch (SQLException e) {
throw e;
}
}
public void onClickBtnShowBD(View view) {
ArrayList persons = new ArrayList();
HashMap person;
Cursor cursor = database.rawQuery("SELECT * FROM person", null);
if (cursor.moveToFirst()) {
do {
person = new HashMap();
person.put("name", cursor.getString(1));
person.put("achievement", cursor.getString(2));
persons.add(person);
} while (cursor.moveToNext());
}
cursor.close();
SimpleAdapter adapter = new SimpleAdapter(this, persons, R.layout.listview_item,
new String[]{"name", "achievement"},
new int[]{R.id.textPerson, R.id.textAchievement});
listBD.setAdapter(adapter);
}
Код: Выделить всё
public BDHelper(@Nullable Context context) {
super(context, BD_NAME, null, BD_VERSION);
this.myContext = context;
BD_LOCATION = context.getApplicationInfo().dataDir + "/databases/";
copyBD();
}
private boolean checkBD() {
File fileBD = new File(BD_LOCATION + BD_NAME);
return fileBD.exists();
}
private void copyBD() {
if (!checkBD()) {
this.getReadableDatabase();
try {
copyBDFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void copyBDFile() throws IOException {
InputStream inputStream = myContext.getAssets().open(BD_NAME);
OutputStream outputStream = new FileOutputStream(BD_LOCATION + BD_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Код: Выделить всё
(1) no such table: person in "SELECT * FROM person"Подробнее здесь: https://stackoverflow.com/questions/790 ... rom-person
Мобильная версия