I am writing an application that should work with the database. I create a database through SQLiteStudio, I fill it, and how to use it is not clear. On the Internet, on this occasion, I came across mainly articles designed to create an empty database, to which the user subsequently adds data and works with it. But I didn’t find a clear answer about working with the existing database.

I started programming with Android recently, so I’m interested in the right approach to solving this problem.

  • Here, it seems, there are a couple of tips: stackoverflow.com/a/2211290/3212712 - Yuriy SPb
  • and, it seems, no ... - mtrfnv
  • It seems that there is just described how to open a DB in a couple of lines along the custom path instead of the default one. Those. put the database in the assets folder and open it from there. - Yuriy SPb
  • Don't you need to move the database to data \ data * \ databases before that? And isn't there some standard way to solve this problem? After all, there are a lot of applications in which a ready-made database is incorporated and some standard solution should be - mtrfnv
  • So I’m talking about this - put your finished database in the путь_к_проекту/папка_проекта/assets - now when you build the apk database 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 an apk file, and opening it from another place — from disk, memory cards, etc examples by reference speak of any path to the database file. - JuriySPb

1 answer 1

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.

  • thank! Works) - mtrfnv
  • @mtrfnv if you want to upgrade the database, then in the DBAssetHelper class, in the DBAssetHelper method DBAssetHelper write setForcedUpgrade(2); предварительно изменив версию БД на 2 setForcedUpgrade(2); предварительно изменив версию БД на 2 - McDaggen