开发者

why sqlite seem like cannot run in device?

i am using sqlite which is sqliteopenhelper

everything is work fine in my emulator even delete then insert data again. after that show it no problem.

but i installed apk file into device, it got some problem, it can insert data but when i want to show it, it gave me application error (force to enclose).

what is the problem?

my sqliteopenhelper code

@Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE_PROGRAM);
        db.execSQL(DATABASE_CREATE_CANDIDATE);
        db.execSQL(DATABASE_CREATE_VIDEO);
        db.execSQL(DATABASE_CREATE_COMMENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;

        String sqlprogram = null;
        String sqlcandidate = null;
        String sqlvideo = null;
        String sqlcomment = null;

        if (oldVersion == 1) {
            sqlprogram = "alter table " + TABLE_PROGRAM + " add note text;";
            sqlcandidate = "alter table " + TABLE_CANDIDATE + " add note text;";
            sqlvideo = "alter table " + TABLE_VIDEO + " add note text;";
            sqlcomment = "alter table " + TABLE_COMMENT + " add note text;";
        }
        if (oldVersion == 2){
            sqlprogram = "";
            sqlcandidate = "";
            sqlvideo = "";
            sqlcomment = "";
        }

        if (sqlprogram != null)
            db.execSQL(sqlprogram);
        if (sqlcandidate != null)
            db.execSQL(sqlcandidate);
        if (sqlvideo != null)
            db.execSQL(sqlvideo);
        if (sqlcomment != null)
            db.execSQL(sqlcomment);
    }

my another class that access openhelper

public SQLite open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getReadableDatabase();
    return this;
}

my logcat

05-11 16:20:38.354: ERROR/AndroidRuntime(15076): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at android.database.AbstractWindowedCursor.checkPositi开发者_JAVA技巧on(AbstractWindowedCursor.java:172)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at com.android.imammuda.Program.createGroupList(Program.java:104)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at com.android.imammuda.Program.filldata(Program.java:66)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at com.android.imammuda.Program.onCreate(Program.java:30)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
05-11 16:20:38.354: ERROR/AndroidRuntime(15076):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)

this is the function

private List createGroupList() {
    ArrayList result = new ArrayList();
    String[] title = new String[11] ;
    Cursor cursor;
    int datarow = sqlite.getDataRow(DatabaseHelper.TABLE_PROGRAM);
    if(datarow == 0){
        for(int i = 1; i <= 1; i++){
            HashMap m = new HashMap();
            m.put(DatabaseHelper.KEY_PROGRAM_TITLE, "Tiada Rancangan !!!");
            result.add( m );
        }
    }else{
        for( int i = 1 ; i <= datarow; ++i ) { 
            cursor = sqlite.fetchdata(i, DatabaseHelper.TABLE_PROGRAM);
            startManagingCursor(cursor);
            title[i] = cursor.getString(0);
            HashMap m = new HashMap();
            m.put(DatabaseHelper.KEY_PROGRAM_TITLE, title[i]); 
            result.add( m );
        }   
    }  
    return result;
}

first, the apps is empty data then i go call insert function

public void onClick(View view){
            if(!(title.getText().toString().trim().equals("")) && !(date.getText().toString().trim().equals("")) && 
               !(time.getText().toString().trim().equals("")) && !(channel.getText().toString().trim().equals(""))){
                sqlite.createProgramData(titletext, datetext, timetext, channeltext);
                int rownumber = sqlite.getDataRow(DatabaseHelper.TABLE_PROGRAM);
                Toast.makeText(getBaseContext(), "You had successfully added a record! \n It currently have " + rownumber + " program(s)!", Toast.LENGTH_SHORT).show();
                finish();
}

filldata()

private void filldata(){
    Cursor datacursor = sqlite.fetchalldata("Program");
    startManagingCursor(datacursor);

    String[] from = new String[]{DatabaseHelper.KEY_PROGRAM_DATE, DatabaseHelper.KEY_PROGRAM_TIME, DatabaseHelper.KEY_PROGRAM_CHANNEL};
    String[] from1 = new String[]{DatabaseHelper.KEY_PROGRAM_TITLE};

    int[] to = new int[]{R.id.programdate, R.id.programtime, R.id.programchannel};
    int[] to1 = new int[]{R.id.programtitle};

    SimpleExpandableListAdapter SEL = new SimpleExpandableListAdapter(this, 
                    createGroupList(), R.layout.programgroup_row, from1, to1,       
                    createChildList(), R.layout.programchild_row, from, to);
    setListAdapter( SEL );
}


You have not moved the cursor to a specific row of your query results. Try this:

if (cursor.moveToFirst())
    title[i] = cursor.getString(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜