-1

In my app I need a database with different tables that I would write before and then just read in the app. I know how to create a new database, write and read it in the app using SQL but I actually don't know with what program (and in which format) I can write the database before adding it in the assets folder.

Would anyone help me with that, please?

gcorso
  • 154
  • 8

4 Answers4

1

You can use http://sqlitebrowser.org/ It run on Mac OS , Windows and Linux

an_droid_dev
  • 1,136
  • 14
  • 18
0

You can use SQLite, which is an on-device SQL implementation. Be aware that this will only support a subset of SQL operations found in most traditional relational DBs. If you need full SQL support you'll have to write back-end web services hosted elsewhere.

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
0

You can use SQLiteOpenHelper for creating your database.

Here is my example to create simple database that have store City with 2 filed is City_ID and City_Name:

public class DiLiBusDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "DiLiBusDB";
private static final String TBL_CITY = "City";
private static final String CITY_ID_FIELD = "City_ID";
private static final String CITY_NAME_FIEDL = "City_Name";
private SQLiteDatabase database;
private static DiLiBusDatabaseHelper mInstance = null;
Context context;
//synchronized to make sure have only one thread is run at the same time
public static DiLiBusDatabaseHelper getInstance(Context context){
    if(mInstance == null){
        mInstance = new DiLiBusDatabaseHelper(context.getApplicationContext());
    }
    return mInstance;
}

private DiLiBusDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);
    this.context = context;
    this.database = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE"+" "+TBL_CITY+"("+CITY_ID_FIELD+" "+"TEXT PRIMARY KEY,"+CITY_NAME_FIEDL+" "+"TEXT"+");";
    String queryIndex = "CREATE INDEX city_name_idx ON"+ " "+TBL_CITY +"("+CITY_NAME_FIEDL+");";
    db.execSQL(query);
    db.execSQL(queryIndex);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

} p/s: I using Singleton pattern to create my database. Moreover, if you want to save your db in SDCard, u can check this link

Community
  • 1
  • 1
Xiao King
  • 369
  • 1
  • 13
  • I'm sorry I it's not what I was searching for: I need to have in my app a database already created before outside the application. Thank you, anyway – gcorso Aug 23 '16 at 16:02
  • 1. Do you want to make a server that save your database and your application connect with server to get data ? Or Do you want to create SQLite outside application then embed it to your app for using? – Xiao King Aug 23 '16 at 16:08
  • I just want to create some tables that the users can only read from inside the application – gcorso Oct 31 '16 at 19:09
0

There are many libraries for database:

This is the simplest way for creating database because you operates on your defined data models.

MrOnyszko
  • 847
  • 9
  • 13