dimanche 13 décembre 2015

SQLite database from assets does not show the table

I am trying to access the database from the assets folder.
I used the following code for that.
I am able to read the DB from assets, but while I am checking the table in the Db, it shows only the the android_metadata and sqlite_sequence tables.

What should I do to fix this?
I am using the getImageData() method to read the table from the DB.

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/********/databases/";
private Context mycontext;
private static String DB_NAME = "STATE_DB";
public SQLiteDatabase myDataBase;


public DataBaseHelper(Context context, String name,         SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);

    this.mycontext=context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        System.out.println("Database exists");
        opendatabase();
    } else {
        System.out.println("Database doesn't exist");
        try {
            createdatabase();

        }catch (IOException e){
        }
       }
       }

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

public boolean checkdatabase() {

    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();
    } catch(SQLiteException e) {
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}


public void createdatabase() throws IOException {
        this.getReadableDatabase();
        try {
            copydatabase();
        } catch(IOException e) {
            throw new Error("Error copying database");

    }
}

public void opendatabase() throws SQLException {
    //Open the database
    String mypath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null,    SQLiteDatabase.OPEN_READWRITE);
}


public void copydatabase() throws IOException {
    //Open your local db as the input stream
    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("/data/data/com.medical.trofii/databases/STATE_DB");

    // transfer byte to inputfile to 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();
}


public ArrayList<String> getImageData() {

    //Cursor cursor = db.rawQuery("SELECT * FROM satateTable WHERE countryId = '21'", null);
    // imageData = cursor.getString(cursor.getColumnIndex("name"));
    //  countries.add(imageData);


    List<String> tables = new ArrayList<String>();

    ArrayList<String> countries = new ArrayList<String>();
    String imageData = null;
   try{
    SQLiteDatabase db = this.getReadableDatabase();
    db.isOpen();
    Cursor cursor = db.rawQuery("SELECT * FROM sqlite_master WHERE    type='table';", null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {

                String tableName = cursor.getString(1);

                if (!tableName.equals("android_metadata") &&  !tableName.equals("sqlite_sequence"))
                    tables.add(tableName);
                cursor.moveToNext();

            } while (cursor.moveToNext());

            for(String tableName:tables) {
                String d = "";
            }
        }
    }}catch (Exception e){


    }


    return countries;

}

}

Aucun commentaire:

Enregistrer un commentaire