2013-06-29 111 views
-1

我只是簡單地嘗試創建表,在其中插入幾條記錄,然後試圖從數據庫中檢索記錄......我編碼的代碼讓我將它展示給您以獲取更清晰的圖片無法從SQLite中檢索數據

package com.example.istudy; 

import java.text.ParseException; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import com.example.dbtable.ProfileTable; 
import com.example.dbtable.Sem5; 

public class DBHandler extends SQLiteOpenHelper { 

    private static final int VERSION = 1; 
    private static final String DB_NAME = "iStudy"; 
    private static final String TABLE_NAME1 = "sem5"; 
    private static final String TABLE_NAME2 = "profile"; 


    //columns 
    private static final String KEY_SUBJ = "sub_name"; 
    private static final String KEY_CHAP = "total_chapters"; 
    private static final String KEY_CHAP_COMPLETED = "chap_completed"; 
    private static final String KEY_CHAP_REMAINING = "chap_remaining"; 

    private static final String KEY_NAME = "name"; 
    private static final String KEY_SEM = "sem"; 
    private static final String KEY_COLG_TIMING = "colg_timing"; 
    private static final String KEY_STUDY_HOURS = "study_hours"; 
    private static final String KEY_TERM_START = "term_start"; 
    private static final String KEY_TERM_END = "term_end"; 


    public DBHandler(Context context) { 
     super(context, DB_NAME, null, VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     String create_table = "CREATE TABLE " + TABLE_NAME1 + " (" + KEY_SUBJ + " TEXT PRIMARY KEY , " + KEY_CHAP + " INTEGER , " + 
     KEY_CHAP_COMPLETED + " INTEGER, " + KEY_CHAP_REMAINING + " INTEGER " + ")"; 
     db.execSQL(create_table); 

     create_table = null; 

     create_table = "CREATE TABLE " + TABLE_NAME2 + " (" + KEY_NAME + " TEXT PRIMARY KEY, " + KEY_SEM + " INTEGER , " + KEY_COLG_TIMING + " TEXT ," + 
     KEY_STUDY_HOURS + " TEXT , " + KEY_TERM_START + " DATE , " + KEY_TERM_END + " DATE )"; 
     db.execSQL(create_table); 

     insertInSem5Table(); 

    } 

    private void insertInSem5Table(){ 
     String[] subName = {"ADBMS","CN","EVS","MP","TCS"}; 
     String insert_Query = ""; 
     SQLiteDatabase db = this.getWritableDatabase(); 

     for (int i = 0; i < subName.length; i++) { 
      ContentValues values = new ContentValues(); 
      values.put(KEY_SUBJ, subName[i]); 

       if(!(subName[i].equals("MP"))){ 
        values.put(KEY_CHAP, 8); 

       } 
       else{ 
        values.put(KEY_CHAP, 7); 
       } 

       values.put(KEY_CHAP_COMPLETED, 0); 

       if(!(subName[i].equals("MP"))){ 
        values.put(KEY_CHAP_COMPLETED, 8); 

       } 
       else{ 
        values.put(KEY_CHAP_COMPLETED, 7); 
       } 
       db.insert(TABLE_NAME1, null, values); 

     } 
     db.close(); 


    } 

    public List<Sem5> getAllRecordsOfSem5Table(){ 
     List<Sem5> list = new ArrayList<Sem5>(); 
     String query = "SELECT * FROM " + TABLE_NAME1; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 
     if(cursor.moveToFirst()){ 
      do{ 
       Sem5 sem5 = new Sem5(); 
       sem5.setKEY_SUBJ(cursor.getString(0)); 
       sem5.setKEY_CHAP(Integer.parseInt(cursor.getString(1))); 
       sem5.setKEY_CHAP_COMPLETED(Integer.parseInt(cursor.getString(2))); 
       sem5.setKEY_CHAP_REMAINING(Integer.parseInt(cursor.getString(3))); 
       list.add(sem5); 
      }while(cursor.moveToNext()); 
     } 

     return list; 

    } 

    public List<ProfileTable> getAllRecordsOfProfileTable(){ 
     List<ProfileTable> list = new ArrayList<ProfileTable>(); 
     String query = "SELECT * FROM " + TABLE_NAME2; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 
     if(cursor.moveToFirst()){ 
      do{ 
       ProfileTable profileTable = new ProfileTable(); 
       profileTable.setKEY_NAME(cursor.getString(0)); 
       profileTable.setKEY_SEM(Integer.parseInt(cursor.getString(1))); 
       Date colgTiming = null, studyHrs = null, termStart = null, termEnd = null; 
       try { 
        colgTiming = java.text.DateFormat.getInstance().parse(cursor.getString(2)); 
        studyHrs = java.text.DateFormat.getInstance().parse(cursor.getString(3)); 
        termStart = java.text.DateFormat.getInstance().parse(cursor.getString(4)); 
        termEnd = java.text.DateFormat.getInstance().parse(cursor.getString(5)); 
        profileTable.setKEY_COLG_TIMING(colgTiming); 
        profileTable.setKEY_STUDY_HOURS(studyHrs); 
        profileTable.setKEY_TERM_START(termStart); 
        profileTable.setKEY_TERM_END(termEnd); 
        list.add(profileTable); 

       } catch (ParseException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      }while(cursor.moveToNext()); 
     } 

     return list; 

    } 




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

    } 

} 

使用下面的代碼調用數據庫的方法

public class Profile extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     DBHandler dbHandler = new DBHandler(this); 
     Log.d("Insert : " , "INserting ..."); 
     List<Sem5> allRecordsOfSem5Table = dbHandler.getAllRecordsOfSem5Table(); 

    } 


} 

我收到以下錯誤

6月6日至29日:55:59.993:E/AndroidR不定時(817):了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.istudy/com.example.istudy.Profile}:java.lang.IllegalStateException:getDatabase遞歸調用

+2

到normaly讀你不使用getwritabledatabase但getreadabledatabase –

+1

http://stackoverflow.com/questions/15955799/getdatabase-called-recursively – Slartibartfast

+0

在getAllRecordsOfProfileTable(),getAllRecordsOfSem5Table()中使用getWritableDatabase()而不是getReadableDatabase()。 –

回答

1

看看這個:

 onCreate(SQLiteDatabase db){ 
      ... 
     insertInSem5Table(); 

     } 

...這:

 private void insertInSem5Table(){ 
     .... 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ... 

你正在試圖獲得一個SQLiteDatabase遞歸

嘗試:

 onCreate(SQLiteDatabase db){ 
      ... 
     insertInSem5Table(db); 

     } 

...這:

 private void insertInSem5Table(SQLiteDatabase db){ 
     .... 
     SQLiteDatabase db = db //this of course is not necessary, just to point out the idea. 
     ... 
+0

k..thanks..but i已經修復它..現在我已經在insertInSem5Table(...)方法中放置了一個log.d(..)語句,但該語句不執行意味着整個方法沒有執行...但仍然我得到了一半來自數據庫的值在getAllRecordsOfSem5Table(...)方法中...我成功獲取列0,1,但第二列的結果應該是第三列的結果。和第三列在空...你可以幫助..如果你想更多的信息只是讓我知道 – user2493303