2012-04-13 202 views
2

我不知道爲什麼這使我的應用程序崩潰。我已經導入了在sqlite上創建的數據庫,現在我只想從中讀取信息。從SQLite數據庫中讀取Android

這裏是我的代碼

public class goodwillDatabase { 

     private static final String DATABASE_NAME="goodwill"; 
     private SQLiteDatabase ourDatabase; 

     //set up variables 
     static final String candidate_id = "_id"; 
     static final String candidate_name = "name"; 
     static final String candidate_street_address = "email"; 

     //set up the table...store different tables in database 
     private static final String DATABASE_TABLE = "candidate"; 
     private static final int DATABASE_VERSION = 1; 

     //create instance of this class 
     //creates, opens and closes database 
     private DbHelper ourHelper; 

     private final Context ourContext; 


     //create database on a different thread 

     private static class DbHelper extends SQLiteOpenHelper{ 

      public DbHelper(Context context) { 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
       // TODO Auto-generated constructor stub 

      } 

      //first time we ever create database, this is called 
      @Override 
      public void onCreate(SQLiteDatabase db) { 



      } 

      //if already created, just update 
      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) { 
       // TODO Auto-generated method stub 

       db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE); 
       onCreate(db); 
      } 


     } 


     public goodwillDatabase(Context c){ 
      //make context of this class equal to whatever is being passed in 
      ourContext = c; 

     } 

     //opens database 
     //sql excpetion for the try catch 
     public goodwillDatabase open() throws SQLException{ 

      //create a new helper, pass in context from the method above 
      ourHelper = new DbHelper(ourContext); 
      ourDatabase = ourHelper.getWritableDatabase(); 


      return this; 
     } 

     //closes the class...sqlite helper, dbhelper 
     public void close(){ 

      ourHelper.close(); 
     } 

     public String getData() { 
      // TODO Auto-generated method stub 

      //create a new helper, pass in context from the method above 


      String [] columns = new String []{ candidate_name, candidate_street_address}; 


      Cursor d = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 


      String result = " "; 
      //String[] result = columns; 
      /* 

      int iRow = c.getColumnIndex(canadiate_id); 
      int iName = c.getColumnIndex(canadiate_name); 
      int iStreet = c.getColumnIndex(canadiate_street_address);*/ 
      /* 
      //start at the beginning, keep moving if you haven't made it to the last 
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) 
      { 
       result = result + c.getString(iRow) + " " + c.getString(iName) + 
      " " + c.getString(iStreet) + "\n"; 

      } 

      */ 
      return result; 

     } 

}

的*光標d = ourDatabase.query(DATABASE_TABLE,列,NULL,NULL,NULL,NULL,NULL); *線是崩潰的地方。是光標不知道指向哪裏的地方?我不是一個超級熟悉數據庫或編程,所以任何幫助,你可以給我,將不勝感激!

+0

請張貼錯誤本身 – Sam 2012-04-13 23:49:12

+0

誤差的logcat的 - -Sorry!應用程序的善意(process goodwill.database)已經意外停止,請再試一次。 - – user1332648 2012-04-14 13:07:43

+0

這裏是一個[logcat trace]的示例(http://stackoverflow.com/questions/10032510/null-pointer- exception-adapter/10032937#10032937)。如果您使用的是Eclipse,則此窗口的默認位置位於屏幕的右下角。沒有這些信息就盲目猜測。 – Sam 2012-04-14 14:11:41

回答

0

嘗試

Cursor d = ourDatabase.rawQuery("select name,email from candidate",null); 

查詢和rawQuery有異曲同工之妙,但有時它們中的任何一個工作和其他不:(