2012-01-21 33 views
0

中的rawQuery方法得到任何結果我正在做一個簡單的SQlite應用程序,我想在表中保存組名並在用戶點擊添加按鈕時在列表視圖中顯示它。爲什麼我沒有從android

任何機構可以幫助我爲什麼我沒有從rawQuery結果獲得空值?

public class AddData { 

public static final String TAG = DbHelper.class.getSimpleName(); 
public static final String DB_NAME = "Grup.db"; 
public static final int DB_VERSION = 1; 
public static final String TABLE = "Grups"; 
public static final String C_ID = BaseColumns._ID; 
public static final String C_CREATED_AT = "easy_ass_created_At"; 
public static final String C_NAME = "name"; 
private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

public class DbHelper extends SQLiteOpenHelper { 

    // public static final String TAG = DbHelper.class.getSimpleName(); 
    // public static final String DB_NAME = "Grup.db"; 
    // public static final int DB_VERSION = 1; 
    // public static final String TABLE = "Grups"; 
    // public static final String C_ID = BaseColumns._ID; 
    // public static final String C_CREATED_AT = "easy_ass_created_At"; 
    // public static final String C_NAME = "name"; 

    public DbHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String sql = ("create table " + TABLE + " (" + C_ID 
       + " integer primary key autoincrement, " + C_NAME 
       + " text not null" + ");"); 
     db.execSQL(sql); 
     Log.d(TAG, "OnCreate sql" + sql); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("drop table if exists " + TABLE); 
     this.onCreate(db); 
     Log.d("TAG", "********On Upgrate Drop Table*****"); 

    } 

} 

public AddData(Context context) { 
    ourContext = context; 

} 

public AddData open() throws SQLException { 

    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getReadableDatabase(); 
    return this; 
} 

public void close() { 
    ourHelper.close(); 
} 

public long createEntry(String name) { 
    ContentValues cv = new ContentValues(); 
    cv.put(C_NAME, name); 
    return ourDatabase.insert(TABLE, null, cv); 
} 

public String getData() { 

    String[] columns = new String[] { C_ID, C_NAME }; 
    Cursor c = ourDatabase.query(TABLE, columns, null, null, null, null, 
      null); 
    String result = ""; 
    // int iRow=c.getColumnIndex(C_ID); 
    int iName = c.getColumnIndex(C_NAME); 

    // for(c.moveToFirst();!c.isAfterLast();c.moveToLast()){ 
    for (boolean hasItem = c.moveToFirst(); hasItem; hasItem = c 
      .moveToNext()) { 
     result = result + " " + c.getString(iName) + "\n"; 
     c.moveToNext(); 
    } 
    c.close(); 
    return result; 

} 

public ArrayList<String> fatchData() { 



    ArrayList<String> results = new ArrayList<String>(); 
    try { 
     Cursor c = ourDatabase.rawQuery("SELECT * from Grups;", null); 
     if (c != null) { 
      if (c.moveToFirst()) { 
       do { 
        String firstName = c.getString(c.getColumnIndex("name")); 
        results.add("Project: " + firstName); 
       } while (c.moveToNext()); 
      } 
     } 
    } catch (Exception e) { 

     e.printStackTrace(); 
    }finally { 
     if (ourDatabase != null) 
      ourDatabase.execSQL("DELETE FROM Grups"); 
      ourDatabase.close(); 
    } 
    return results; 

} 

} 

回答

0

嘗試刪除c.close()和使用startManagingCursor(c)getData()方法(創建光標之後)

相關問題