问题与我的android db文件?

时间:2013-06-29 14:41:27

标签: android sqlite

在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):正在关闭VM
  13. 06-30 11:22:44.439:W / dalvikvm(869):threadid = 1:线程退出  未捕获的异常(组= 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。

1 个答案:

答案 0 :(得分:0)

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

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

如果您想在运行时将资产文件夹中的数据库复制到您的应用程序中,您必须执行Open SQLite database fails Android

中的答案。

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