2016-11-12 34 views
1

我被困在昨天。研究並沒有多大幫助,因爲我剛剛交出了這個項目,而且我對Android沒有任何瞭解。如何從SQLite數據庫轉儲結果

 myDB = null; 
     try { 
      File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); 
      File file = new File(dir, "database.db"); 
      myDB = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY); 

      String q = "SELECT * FROM companies"; 
      Cursor mCursor = myDB.rawQuery(q, null); 

      Context context = getActivity().getApplicationContext(); 
      CharSequence text = mCursor.toString(); 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

      myDB.close(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

從烤麪包,我得到[email protected]

如何轉儲所有數據庫結果?

回答

1

我不是專家,但試試這個。你的麪包應該一個接一個地扔出名字。確保將名稱更改爲您自己的字段。

String q = "SELECT * FROM companies"; 
      Cursor mCursor = myDB.rawQuery(q, null); 
      mCursor.moveToFirst(); 
      while (!mCursor.isAfterLast()) { 
       String name= mCursor.getString(mCursor.getColumnIndex("name")); 
       mCursor.moveToNext(); 

       Context context = getActivity().getApplicationContext(); 
       CharSequence text = name; 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 

      } 
+0

哇,真棒這個作品謝謝! –