2013-01-10 7 views
9

से सभी रिकॉर्ड हो रही है मैं एक डेटाबेस बनाने रहा हूँ और मैं डेटाबेस से सभी रिकॉर्ड पढ़ने की जरूरत है, लेकिन मेरे कार्यक्रम इस बयान पर क्रैश हो रहा है:SQLite एंड्रॉयड

newWord= db.getAllRecords(); 

मुझे लगता है getAllRecords के साथ एक समस्या (वहाँ है), जैसा ग्रहण कोई त्रुटि इंगित करता है।

public Cursor getAllRecords() { 
db = dBHelper.getWritableDatabase();//obtains the writable database 
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,KEY_WORD}, null, null, null,  null, null);//the query used to obtain all records form the table 

} 

कोई विचार?

+5

दुर्घटना को परिभाषित करता है? स्टैक ट्रेस पोस्ट करें – Woot4Moo

+1

नहीं, बस मुझे एक मंदता है:/मैंने निर्माण स्ट्रिंग में कॉलम नाम को गलत लिखा –

उत्तर

13

यहाँ मैं एक मेज से सभी सामग्री को पाने के लिए क्या है ..

public ArrayList<MyObject> getAllElements() { 

    ArrayList<MyObject> list = new ArrayList<MyObject>(); 

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

    SQLiteDatabase db = this.getReadableDatabase(); 
    try { 

     Cursor cursor = db.rawQuery(selectQuery, null); 
     try { 

      // looping through all rows and adding to list 
      if (cursor.moveToFirst()) { 
       do { 
        MyObject obj = new MyObject(); 
        //only one column 
        obj.setId(cursor.getString(0)); 

        //you could add additional columns here.. 

        list.add(obj); 
       } while (cursor.moveToNext()); 
      } 

     } finally { 
      try { cursor.close(); } catch (Exception ignore) {} 
     } 

    } finally { 
     try { db.close(); } catch (Exception ignore) {} 
    } 

    return list; 
} 
-1
Cursor c = ourDatabase.rawQuery("SELECT * FROM table_name", null); 
     if(c!=null){ 
      c.moveToFirst(); 
     } 
     return c; 
2
public void printTableData(String table_name){ 
    SQLiteDatabase db = getReadableDatabase(); 

    Cursor cur = db.rawQuery("SELECT * FROM " + table_name, null); 

    if(cur.getCount() != 0){ 
     cur.moveToFirst(); 

     do{ 
      String row_values = ""; 

      for(int i = 0 ; i < cur.getColumnCount(); i++){ 
       row_values = row_values + " || " + cur.getString(i); 
      } 

      Log.d("LOG_TAG_HERE", row_values); 

     }while (cur.moveToNext()); 
    } 
} 
0
Initialize TABLE: 
    private static String TABLE="your_table_name"; 

सभी रिकॉर्ड की सूची प्राप्त करने के लिए, अपने डेटाबेस वर्ग में विधि निम्न लिखें:

public ArrayList<RecentStickerModel> getAllRecentRecords() { 

    ArrayList<RecentStickerModel> listStickers = new ArrayList<>(); 

    SQLiteDatabase db = getWritableDatabase(); 

    String q = "SELECT name from sqlite_master WHERE type='table' AND name='" + TABLE + "'"; 
    Cursor cursorTable = db.rawQuery(q, null); 

    if (cursorTable != null && cursorTable.moveToFirst()) { 

     String tableName = cursorTable.getString(0); 
     cursorTable.close(); 

     if (TABLE.equals(tableName)) { 

      String query = "Select * from " + TABLE ; 

      Cursor cursor = db.rawQuery(query, null); 

      if (cursor != null && cursor.moveToFirst()) { 

       Logger.e(TAG, "cursor size:" + cursor.getCount()); 

       int catNameCI = cursor.getColumnIndex(KEY_CAT_NAME); 
       int imagePathCI = cursor.getColumnIndex(KEY_IMAGE_PATH); 
       int lastStickerTimeCI = cursor.getColumnIndex(KEY_LAST_STICKER_TIME); 


       do { 

        RecentStickerModel model = new RecentStickerModel(); 
        model.setCatName(cursor.getString(catNameCI)); 
        model.setImgStickerPath(cursor.getString(imagePathCI)); 
        model.setLastStickerTime(cursor.getString(lastStickerTimeCI)); 


        listStickers.add(model); 
       } while (cursor.moveToNext()); 

       cursor.close(); 
      } 
     } 

     db.close(); 
    } 

    return listStickers; 
} 

फिर निम्न विधि को कॉल करें जहां आप चाहते हैं:

arrayListRecent=stickersListDatabase.getAllRecentRecords();