2016-03-31 9 views
0

VariableA (barTableName) bir SQLite sorgusuna geçiriyorum, "SELECT * FROM" + barTableName + "WHERE DRINKTYPE = 'Beer';". Bir kullanıcının ne seçtiğine göre değişebilmek için barTableName'e ihtiyacım var. Değişkeni kodladığımda çalışır. "Değişken zaman çizelgesinde" ne kadar geri gidersek gideyim, değiştirmemeye çalışırsam, sorgudaki barTableName değeri olarak bana bir boş verir. Bunu nasıl başarabileceğimi bilen var mı?JavaName sorgusuna geçirilmek üzere TableName değiştirilmesi gerekiyor

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 sqlquery ; 
    public static String barTableName = 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>(); 

     splquery="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 
} 

Şu anki durumum diğer sınıftan gelen ne dayalı bir Eğer deyimi ile splquery atamak için deneyin. Çifte, üçlü kontrol ettim. İD'de gelen değişken, if ifadesinde yer alan 3'ünden biri, hepsi kapaklar gibi. Uzun zamandır bunun üzerinde çalışıyorum ve kafamı duvara çarptıyorum. Herhangi bir yardım çok takdir edilecektir.

+0

Tam olarak nerede barTableName ayarlanır? Bu ayarlamayı görebildiğim tek şey var firstBarTableName. BarTableName değil. – IdleGandalf

+0

Düzenlendi. Ben çalışmayı denemek için yarım düzine farklı şeyler deniyorum ve görünüşe göre iki girişimi arasında – Inessaria

+0

kod yayınlanmıştır yanlış olduğunu düşünüyorum. Örneğin getMixedDrinkDrinkLabels'de barTableName'i kullanın. Ancak bu, sonunda bir NPE ile sonuçlanmadı. BarTableName öğesinin, ´FROM "+ barTableName +" WHERE´de firstBarTableName olarak değiştirilmesini tavsiye ederim, çünkü tablo adının kaydedildiği yer değil, değil mi? – IdleGandalf

cevap

0

DBHelper.java'daki değişken statik idi. DBHelper orijinal olarak yaratıldığında var olmayan Bar.java'dan çekiyordu. Bu yüzden değişkene "Boş" değeri atandı ve daha sonra hiç güncellemedi. Değişkeni DBHelper yerine Bar.java'da statik olarak bildirerek, değişkenin doğru bir şekilde geçmesine ve ihtiyacım olduğu gibi değişmesine izin verir.

İlgili konular