3

I have a database file in assets folder, when user install this program, in the first time program launcher I will copy database from assets to databases of program

This is my code:

private static final String DATABASE_NAME= "data.db";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
    this.myContext = context;
}

/************************************************
 * Suggested Copy/Paste Done
 ************************************************/

@Override
public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
    createDataBase();
}

public void createDataBase(){
    boolean dbExist = checkDataBase();
    if(!dbExist){
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        e.printStackTrace();
    }

    if(checkDB != null){
        checkDB.close();
    }
    return checkDB != null;
}
private void copyDataBase() throws IOException{
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
    String outFileName = myContext.getDatabasePath(DATABASE_NAME).getPath();
    File f = new File(outFileName);
    f.getParentFile().mkdirs();
    f.createNewFile();
    OutputStream myOutput = new FileOutputStream(f);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

I check databases folder in the device and see that it has data.db file but this file has not table.

The code where I use to copy data have running.

Why data.db is wrong ?


UPDATED:

@DatabaseTable(tableName = "english")
public class Topic {
    @DatabaseField(generatedId = true, columnName = "id")
    private int id;
    @DatabaseField(columnName = "title")
    private String title;
    @DatabaseField(columnName = "question")
    private String question;
    @DatabaseField(columnName = "answer")
    private String answer;
    @DatabaseField(columnName = "audio_url")
    private String audioUrl;
    @DatabaseField(columnName = "local_url")
    private String localUrl;

    public Topic() {
    }
}
CREATE TABLE `english` (
    `id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    `title` NVARCHAR,
    `question`  NVARCHAR,
    `answer`    NVARCHAR,
    `audio_url` NVARCHAR,
    `local_url` NVARCHAR
);
  • Did you set up the original db file correctly? See http://stackoverflow.com/a/5799920/420594 regarding android_metadata – drhr Mar 09 '16 at 07:14
  • @drhr, pls see my config in model file and when I make database use sql command ? –  Mar 09 '16 at 07:21
  • I have information is all command work success when I put data.db override to databases folder by manual –  Mar 09 '16 at 07:28

1 Answers1

0

When

super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);

is called a data.db have been create. so that onCreate will be not call. you shouldn't put createDataBase here.

I think you can put it to getWritableDatabase or getDao function.

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • Thanks, I put that function to get*Dao function and program is worked –  Mar 09 '16 at 09:18