2010-06-18 6 views
0

때마다 나는 그것의 null을 사용하고 나는 그것을 얻지 못할 때마다.안드로이드 sqlight - 항상 null

나는이 클래스를 호출 할 때마다
public class GameSQLHelper { 
    static final String[] COUNTRIES = new String[] { 
     "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra" 
     }; 

    private static final String DB_PATH = "/countryCityGame/databases/"; 
    private static final String DATABASE_NAME = "events.db"; 
    private static final int DATABASE_VERSION = 1; 
    private final Context mCtx; 
    // Table name 
    public static final String TABLE = "myDataBase"; 

    // Columns 
    public static final String LETTER = "letter"; 
    public static final String TYPE = "type"; 
    public static final String VALUE = "value"; 

    //my database 
    SQLiteDatabase myDataBase; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 
     private static final String TAG = null; 

     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
       String sql = "create table " + TABLE + "(" + BaseColumns._ID 
       + " integer primary key autoincrement, " + LETTER + " text not null, " 
       + TYPE + " text not null," + VALUE + " text not null);"; 
       Log.d("EventsData", "onCreate: " + sql); 
       db.execSQL(sql); 
       insertValuesToDB(db); 

      } 
      private void insertValuesToDB(SQLiteDatabase db) { 


       if (db == null){ 

       } 
       else{ 
       db.execSQL("INSERT INTO " 

               + TABLE 

               + " ("+LETTER+","+ TYPE +"," + VALUE +")" 

               + " VALUES ('A', 'country', 'Angola');"); 

        ContentValues initialValues = new ContentValues(); 


        for (int i = 0 ; i < COUNTRIES.length ; i++){ 

         Character tmp = (Character)COUNTRIES[i].charAt(0); 

         initialValues.put(VALUE, COUNTRIES[i]); 
         initialValues.put(TYPE, "country"); 
         initialValues.put(LETTER,tmp.toString(tmp)); 

        db.insert(TABLE, null, initialValues); 
        } 
       } 
      } 
      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
       db.execSQL("DROP TABLE IF EXISTS notes"); 
       onCreate(db); 
      } 

     } 

     /** 
     * Constructor - takes the context to allow the database to be 
     * opened/created 
     * 
     * @param ctx the Context within which to work 
     */ 
     public GameSQLHelper(Context ctx) { 
      this.mCtx = ctx; 
     } 





     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     if (oldVersion >= newVersion) 
      return; 

     String sql = null; 
     if (oldVersion == 1) 
      sql = "alter table " + TABLE + " add note text;"; 
     if (oldVersion == 2) 
      sql = ""; 

     Log.d("EventsData", "onUpgrade : " + sql); 
     if (sql != null) 
      db.execSQL(sql); 
    } 

    public void openDataBase() throws SQLException{ 

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

     } 


     public boolean existInDataBase(String serchStr){ 

      Cursor c = null ; 
      try{ 
      openDataBase(); 
      c = myDataBase.query(true, TABLE, new String[] {TYPE 
        }, VALUE + "=" + serchStr, null, 
        null, null, null, null); 
      } 
      catch(Exception e){ 
       Log.d("sqlExacption", e.getMessage()); 

      } 
      if (c == null) 
       return false; 

      return true; 
     } 

} 

: 나는 쿼리가있을 때

본인은 SQL이 코드를 사용

I (I가 초기화 그것의 instace을 누르고이 활동은 mDbHelper = new GameSQLHelper(this);) 항상 내 mDbHelper를 null로 가져 오려면 어떻게해야합니까? 그것은 mysql 플랫폼 외부에서 SQL을 사용하여 작업 한 첫 번째 시간이므로 개념을 이해하는 데 도움이되는 일종의 프로 시저이며 안드로이드 메모장 예제가 도움이되지 않습니다.

답변

0

SQLiteDatabase#openDatabase을 통해 데이터베이스를 열지 마십시오. 대신 DatabaseHelper의 인스턴스를 만들고 getWritableDatabase을 호출하십시오.

+0

위의 설명에 약간의 예를 보여 주실 수 있습니까? 고맙습니다 –

+0

누군가 나를 보여 주실 수 있습니까? –

관련 문제