jeudi 31 mars 2016

Need to change TableName being passed in to Java SQLite query

I am passing VariableA (barTableName ) to an SQLite query, "SELECT * FROM " + barTableName + " WHERE DRINKTYPE='Beer';". I need barTableName to be able to change, based on what a user chooses. When I hardcode the variable, it works. If I try to change it at all, no matter how far back in the "variable timeline", it gives me a null point exception. Does anyone know how I could acomplish this?

DBHelper

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

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;

public class DBHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/com.example.sixth/databases/";
    private static String DB_NAME = "BarSample.db"; 
    private final Context myContext;    
    public static String tableName = "BARS";
    String barTableName, sqlquery ;
    public static String firstBarTableName = MainActivity.upperCaseName;//Pass in the specific bar from the spinner choice
    private SQLiteDatabase myDataBase;


    public DBHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     */
    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            // do nothing - database already exist
        } else {

            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            this.getReadableDatabase();

            try {
                this.close();
                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");
            }
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {
            // database does't exist yet.
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     */
    private 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(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();

    }

    public void openDataBase() throws SQLException {

        // Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    public List<String> getAllLabels(){
        List<String> labels = new ArrayList<String>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + tableName;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(1) + " " + cursor.getString(2)+ ", " + cursor.getString(3));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return labels;

    } // will returns all labels stored in database

    public List<String> getBeerDrinkLabels(){
        //naming();
        List<String> allBeerDrinkLabels = new ArrayList<String>();

        if (firstBarTableName.equals("CHANGOS")){
            sqlquery = "SELECT * FROM CHANGOS WHERE DRINKTYPE='Beer';";
        }
        else if(firstBarTableName.equals("LANDOS")){
            sqlquery = "SELECT * FROM LANDOS WHERE DRINKTYPE='Beer';";
        }
        else{
            sqlquery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Beer';";
        }


        //String sqlquery="SELECT * FROM " + barTableName + " WHERE DRINKTYPE='Beer';";
        String selectQuery = sqlquery;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + cursor.getString(2));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return allBeerDrinkLabels;

    } // will returns all labels stored in database

    public List<String> getWineDrinkLabels(){
        List<String> allWineDrinkLabels = new ArrayList<String>();

        // Select All Query
        String sqlquery="SELECT * FROM "+barTableName + " WHERE DRINKTYPE='Wine';";
        String selectQuery = sqlquery;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                allWineDrinkLabels.add(cursor.getString(1) + ", " + cursor.getString(2));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return allWineDrinkLabels;

    } // will returns all labels stored in database

    public List<String> getMixedDrinkDrinkLabels(){
        List<String> allMixedDrinkDrinkLabels = new ArrayList<String>();

        // Select All Query
        String sqlquery="SELECT * FROM "+barTableName + " WHERE DRINKTYPE='Mixed Drink';";
        String selectQuery = sqlquery;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                allMixedDrinkDrinkLabels.add(cursor.getString(1) + ", " + cursor.getString(2));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return allMixedDrinkDrinkLabels;

    } // will returns all labels stored in database

    public List<String> getOtherDrinkLabels(){
        List<String> allOtherDrinkLabels = new ArrayList<String>();

        // Select All Query
        String sqlquery="SELECT * FROM "+barTableName + " WHERE DRINKTYPE='Other';";
        String selectQuery = sqlquery;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                allOtherDrinkLabels.add(cursor.getString(1) + ", " + cursor.getString(2));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return allOtherDrinkLabels;

    } // will returns all labels stored in database

    public List<String> getShotsDrinkLabels(){
        List<String> allShotsDrinkLabels = new ArrayList<String>();

        // Select All Query
        String sqlquery="SELECT * FROM "+barTableName + " WHERE DRINKTYPE='Shots';";
        String selectQuery = sqlquery;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                allShotsDrinkLabels.add(cursor.getString(1) + ", " + cursor.getString(2));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return allShotsDrinkLabels;

    } // will returns all labels stored in database
}

I'm currently try to assign it to splquery with an If statement, based on what is coming in from the other class. I have double, triple checked. The variable coming in IS one of the 3 that are in the if statement, all in caps like is in the statement. I've been working on this for a long while and am beating my head against the wall. Any help would be VERY appreciated.

Aucun commentaire:

Enregistrer un commentaire