FORUMS
Remove All Ads from XDA

Help with db manipulation

2 posts
Thanks Meter: 0
 
By pkkr, Junior Member on 3rd May 2017, 08:48 PM
Post Reply Email Thread
Hello everyone, I have a problem, i made an app in android studio and i want to use an existing db which i already transferred in assets and project them into a list where every table will be a "category" and every record will be able to have its values manipulated i.e. "Name" "Value" [Item1,1] [Item2,2] Item1(value)+Item2(value)=3. I don't know if i'm being understandable...thanks anyway below i will attach my DatabaseHelper


Code:
import android.content.Context;
import android.database.sqlite.SQLiteAbortException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "";
    private static String DB_NAME = "my.db";
    private SQLiteDatabase myDataBase;
    private final Context myContext;


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null,1);
        this.myContext = context;
        DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
    }

    //Create an empty db to overwrite your own db on it

    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //does nothing cause db already exists
        }else{

            //Overwrites db
            this.getWritableDatabase();

            try{
                copyDataBase();
            } catch(IOException e){
                throw new Error("Error copying database");
            }
        }
    }

    // Check if db exists every time app opens

    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH;
            checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
        }catch (SQLiteException e){

            //db doesn't exist yet
        }

        if (checkDB!=null){

            checkDB.close();
        }

        return checkDB != null ? true : false;
    }


    //Copies your db from assets to the new db

    private void copyDataBase() throws IOException{

        //open your local db as input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        //Path to the empty db
        String outFileName = DB_PATH;

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

        //transfer bytes from I to O
        byte[] buffer = new byte[1024];
        int length;
        while((length=myInput.read(buffer))>0){
            myOutput.write(buffer,0,length);
        }
        //Close streams
        myOutput.flush();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes