-1

我只是尝试创建表,在其中插入几条记录,然后尝试从数据库中检索记录...我编写的代码让我向您展示以获得更清晰的图片

 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

    }

}

使用以下代码调用 db 方法

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();

    }


}

我收到以下错误

06-29 06:55:59.993: E/AndroidRuntime(817): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.istudy/com.example.istudy.Profile}: java.lang.IllegalStateException: getDatabase递归调用

4

1 回答 1

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.
       ...
于 2013-06-29T07:17:22.587 回答