Add a library to open a database:
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
In the java directory of your project, create a package , name it for example, DBSetup in it, create a class DBAssetHelper inherited from SQLiteAssetHelper :
public class DBAssetHelper extends SQLiteAssetHelper { private static final int DBVersion = 1; private static final String DBName = "NameDB"; public DBAssetHelper(Context context) { super(context, DBName, null, DBVersion); } }
Next, in the main directory of your application, create a class, for example, DBSQLiteOpenHelper inherited from SQLiteOpenHelper:
class DBSQLiteOpenHelper extends SQLiteOpenHelper { private static final int DBVersion = 1; private static final String DBName = "NameDB"; private static final String TableName = "NAME_TABLE"; private static final String mTable = "CREATE TABLE " + TableName + "(" + "_id" + " INTEGER PRIMARY KEY AUTOINCREMENT, " + "CONTENT_ONE" + " TEXT, " + "CONTENT_TWO" + " TEXT)"; DBSQLiteOpenHelper(Context context) { super(context, DBName, null, DBVersion); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(mTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TableName); this.onCreate(db); } }
Create the assets folder, right-click on the src/new Folder/Assets Folder directory in the created assets folder, create the databases directory, attach the databases file there, first cut out .db , that is, NameDB should be NameDB instead of it data:
DBAssetHelper dbSetup = new DBAssetHelper(this); dbSetup.getWritableDatabase(); try{ SQLiteOpenHelper databaseHelper = new DBSQLiteOpenHelper(this); SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query( "NAME_TABLE", new String[]{"CONTENT_ONE", "CONTENT_TWO"}, "_id = ?", new String[]{Сюда передаем переменную с длиной высоты соответствующей длине нашей БД}, null, null, null); if (cursor.moveToFirst()) { final String CONTENT_ONE = cursor.getString(0); final String CONTENT_TWO = cursor.getString(1); TextView textOne = (TextView) findViewById(R.id.text_one); TextView textTwo = (TextView) findViewById(R.id.text_two); textOne.setText(CONTENT_ONE); textTwo.setText(CONTENT_TWO); } cursor.close(); db.close(); } catch (SQLiteException e) { Toast.makeText(this, "База данных недоступна", Toast.LENGTH_SHORT).show(); }
This is the best option that I could find to read data from the finished database.
assetsfolder and open it from there. - Yuriy SPb ♦путь_к_проекту/папка_проекта/assets- now when you build theapkdatabase will be in it. And you can, as described in the link above, open this database. The path you specify is the default path to the default database. It is default, not mandatory. In principle, nothing prevents you from also packing the finished database into anapkfile, and opening it from another place — from disk, memory cards,etcexamples by reference speak of any path to the database file. - JuriySPb ♦