2013-06-29 114 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