1

I have a pre-existing sqlite database that is about 5mb in size. I use this code to copy from my assets folder to the packages/databases folder on the device.

The database copies to the device but the data inside the database has been deleted or is missing. The only table inside is called android_metadata.

Here is the code:

try{
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        }catch(SQLiteException e){
erik
  • 11
  • 1
  • 2

2 Answers2

0

Make sure your database is in YourAppName\assets\databases folder.

For example, if your database name is: app.db, it should be in that folder to be copied.

You need to reinstall your app whenever you want to release a new copy of the database.

If you are using the SQLiteAssetHelper, your class file will look somewhat like this:

package com.mycompany.myapp;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "app.db";
    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        // you can use an alternate constructor to specify a database location
        // (such as a folder on the sd card)
        // you must ensure that this folder is available and you have permission
        // to write to it
        // super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);

    }

    public Cursor mymethod (String param) {
        SQLiteDatabase db = getReadableDatabase();

        String sql;
        sql = "...";

        Cursor c = db.rawQuery(sql, new String[] { param  });

        c.moveToFirst();
        return c;
    }}
live-love
  • 48,840
  • 22
  • 240
  • 204
0

If your database (tables) is (are) not appearing / loading, then it is likely that you have created it inproperly. I would go back and ensure that you have set it up correctly.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • If I go to the DDMS and push the database file to the device the app works. It can see all the tables. – erik May 17 '11 at 20:27
  • Found it! If you are planning to create a new SQLite database then over ride and implement the onCreate() Method as shown in the tutorial. But if you are using a SQLite database that is created by another external source and you are going to pull it down, then leave the onCreate() method empty. – erik May 17 '11 at 20:50