0

在 2 小时之前,我从 eclipse 文件资源管理器中提取了我的 db 文件,当时它拥有所有数据。但是现在当我通过拉动 db 文件进行检查时,它只有一个表 android_metadata 没有别的,我很震惊为什么会发生这种情况?谁能解释一下?

我完整的数据库代码

    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 android.graphics.AvoidXfermode;
import android.util.Log;

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
        Log.d(Profile.getTag(), "oncreate (...) is ececuting ...");

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

        Log.d(Profile.getTag(), "After creating table inserting sem5 table data");

        insertInSem5Table(db);


    }

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

        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, -1);

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

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

                long insert = db.insert(TABLE_NAME1, null, values);
                Log.d(Profile.getTag() , String.valueOf(insert)) ;


        }

        Log.d(Profile.getTag(), "closing the db after inserting sem5 data");
        db.close();

    }

    public void insertProfileData(String name, int sem, String colgTimingFrom, String colgTimingTo, String studyHourFrom, String studyHourTo, 
            Date termStart, Date termEnd ){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name);
        values.put(KEY_SEM, sem);
        values.put(KEY_COLG_TIMING, colgTimingFrom + " " + colgTimingTo );
        values.put(KEY_STUDY_HOURS, studyHourFrom + " " + studyHourTo );
        values.put(KEY_TERM_START, termStart.toString() );
        values.put(KEY_TERM_END, termEnd.toString() );
        db.insert(TABLE_NAME2, null, values);
        Log.d(Profile.getTag(), "values inserted successfully ....");
        db.close();
    }

    public List<Sem5> getAllRecordsOfSem5Table( ){
        List<Sem5> list = new ArrayList<Sem5>();
        String query = "SELECT * FROM " + TABLE_NAME1;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        if( cursor.moveToFirst() ){
            do{
                Sem5 sem5 = new Sem5();

//              int columnIndex = cursor.getColumnIndex(KEY_SUBJ);
//              int columnIndex2 = cursor.getColumnIndex(KEY_CHAP);
//              int columnIndex3 = cursor.getColumnIndex(KEY_CHAP_COMPLETED);
//              int columnIndex4 = cursor.getColumnIndex(KEY_CHAP_REMAINING);
//              
//              
//              
//              Log.d("naved " , cursor.getString(columnIndex));
//              Log.d("naved " , cursor.getString(columnIndex2));
//              Log.d("naved "  , cursor.getString(columnIndex3));
////                Log.d("naved "  , cursor.getString(columnIndex4));
//              String string = cursor.getString(columnIndex4);
//              if( string == null )
//                  Log.d("naved chap_remaing " , " null " );
//              else
//              Log.d("naved chap_remainig " , string);


                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());
        }
        db.close();
        return list;

    }

    public List<ProfileTable> getAllRecordsOfProfileTable( ){
        Log.d(Profile.getTag(), "Getting all records of profile table ....");

        List<ProfileTable> list = new ArrayList<ProfileTable>();
        String query = "SELECT * FROM " + TABLE_NAME2;
        SQLiteDatabase db = this.getReadableDatabase();
        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 (Exception e) {
                    // TODO: handle exception
                    db.close();
                    return null;
                }


            }while(cursor.moveToNext());
        }

        Log.d(Profile.getTag(), "Closing db ....after getting profile table...");
        db.close();
        return list;

    }




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

    }

}

我的活动代码

public class Profile extends Activity implements OnClickListener {

private static final String TAG = "iStudy";

//private DBHandler dbHandler;

public static String getTag() {
    return TAG;
}

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

    DBHandler dbHandler = new DBHandler(this);

    Log.d(TAG , "Retriving profile data ...");

    List<ProfileTable> allRecordsOfProfileTable = dbHandler.getAllRecordsOfProfileTable();


    if( allRecordsOfProfileTable == null || allRecordsOfProfileTable.size() == 0 ){
        Log.d(TAG , "Retrived profile record ..it was either null or allRecordsOfProfileTable.size() == 0 ... so starting profile_activity");
        setContentView( R.layout.profile_screen );
    }else{
        Log.d(TAG , "Retrived profile record ..it was not null so starting home_activity");
        setContentView( R.layout.home_activity ) ;
    }

     Button nxtBtn = (Button) findViewById(R.id.nxtBtn);
     nxtBtn.setOnClickListener(this);

    } }

如果您想了解更多信息,我只向您展示相关代码,请询问。

对于任何想要更多日志信息的人,请参考此

  1. 06-30 11:22:43.579: D/iStudy(869): 检索配置文件数据...
  2. 06-30 11:22:43.579: D/iStudy(869): 获取配置文件表的所有记录......
  3. 06-30 11:22:43.949: D/iStudy(869): oncreate (...) 正在执行...
  4. 06-30 11:22:44.079: D/iStudy(869): 创建表后插入 sem5 表数据
  5. 06-30 11:22:44.093: D/iStudy(869): 1
  6. 06-30 11:22:44.201: D/iStudy(869): 2
  7. 06-30 11:22:44.209: D/iStudy(869): 3
  8. 06-30 11:22:44.309: D/iStudy(869): 4
  9. 06-30 11:22:44.416: D/iStudy(869): 5
  10. 06-30 11:22:44.416: D/iStudy(869): 插入 sem5 数据后关闭数据库
  11. 06-30 11:22:44.419: I/SQLiteConnectionPool(869): /data/data/com.example.istudy/databases/iStudy 的连接池已关闭,但仍有 1 个连接在使用中。当它们被释放回游泳池时,它们将被关闭。
  12. 06-30 11:22:44.439: D/AndroidRuntime(869): 关闭虚拟机
  13. 06-30 11:22:44.439: W/dalvikvm(869): threadid=1: 线程以未捕获的异常退出 (group=0x40a71930)
  14. 06-30 11:22:44.671:E/AndroidRuntime(869):致命异常:主要
  15. 06-30 11:22:44.671: E/AndroidRuntime(869): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.istudy/com.example.istudy.Profile}: java.lang.IllegalStateException: 尝试重新打开一个已经关闭的对象:SQLiteDatabase: /data/data/com.example.istudy/databases/iStudy。
4

1 回答 1

0

从我从您的代码中可以看出,目前资产文件夹中的数据库与您的应用程序之间没有关联,除了数据库位于资产文件夹中。

我希望在某些时候你的应用程序被卸载,可能是通过调试,然后数据库是新鲜的,没有表,如果你没有找到创建新表的代码,结果是一个空数据库。

如果您想在运行时将资产文件夹中的数据库复制到您的应用程序中,您必须执行类似于我在Open SQLite database failed Android上的回答

有几种工具可以使用 sqlite 数据库,例如 sqlitetudio、sqliteadmin、sqlitedatabasebrowser 等,以访问和维护资产文件夹中的数据库。希望这可以帮助。

于 2013-07-30T21:15:19.413 回答