清除表中的所有行时出现SQLite错误?

时间:2012-02-17 10:13:34

标签: android sqlite

public class SQLiteTestActivity extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "MP_DB.db";
private static final String MP_TABLE_NAME = "MPDetails";

SQLiteTestActivity (Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}// end of 

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
            BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " name TEXT, " +
            " lat REAL, "+
            " lng REAL,"+
            " roll REAL,"+
            " pitch REAL,"+
            " yaw REAL,"+
            " path TEXT"+
            ");" );
}

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

}


public String getMP_Name(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM MP_Data WHERE "+
                                                BaseColumns._ID+" = "+
                                                Long.toString(id), null);
    c.moveToFirst();
    String r = c.getString(0);
    return r;       
}

public Double getMP_Lat(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT lat FROM MP_Data WHERE "+
                                                BaseColumns._ID+" = "+
                                                Long.toString(id), null);
    c.moveToFirst();
    Double r = c.getDouble(0);
    return r;       
}

public int getRowsCounter() {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT COUNT (*) FROM MPDetails ", null);
    c.moveToFirst();
    int r = c.getCount();
    return r;       
}

public void deleteMP(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(MP_TABLE_NAME, BaseColumns._ID+"="+Long.toString(id),null);   
}

public void deleteALLMP() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(MP_TABLE_NAME,null, null);    
}

}

OnClickListener save_btnListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        db =  mphelper.getWritableDatabase();
        cv = new ContentValues();

        String str1 = et01.getText().toString();
        String str2 = et02.getText().toString();

        if( (("").equals(str1) ||("").equals(str2))) {
            Toast.makeText(getBaseContext(), "both or either fields are empty", Toast.LENGTH_SHORT).show();
        } else{

        cv.put("name", str1);
        cv.put("lat", Double.valueOf(str2));


        long newID = db.insert("MPDetails", null, cv);
        count = mphelper.getRowsCounter();

        if (newID != -1){
            Toast.makeText(getBaseContext(),
                          "data saved ", 
                           Toast.LENGTH_SHORT).show();              
        }// end if
        }txt04.setText(String.valueOf(count));      
    }
};

OnClickListener clear_btnListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        txt04 = (TextView) findViewById(R.id.txt04);
        mphelper.deleteALLMP();
        count = mphelper.getRowsCounter();
        txt04.setText(String.valueOf(count));
    }
};

1 - 当我使用“clear_btnListener”清除数据库并单击保存“save_btnListener”时 尽管我写了if语句来检查要保存的字段是否为空,但应用程序崩溃了。

2 - 当我清除DB并显示表中的行数时,我希望得到零,但实际上,我得到1 ???

2 个答案:

答案 0 :(得分:1)

您应该在模拟器/手机上删除现有的MP_DB.db. 并重新创建具有以下更改的数据库 在SQLiteTestActivity中:

private static final String MP_TABLE_NAME = "MPDetails";

private static final String MP_TABLE_NAME = "MP_Data";   

并在SQLSaveData中:

long newID = db.insert("MPDetails", null, cv);

long newID = db.insert("MP_Data", null, cv);

并设置以下内容

et02.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);

否则你会得到NumberFormatException


干杯

答案 1 :(得分:0)

据我所知......所有与sqlite相关的东西你最好在Sqlite Activity上做,并制作该类的对象并使用它。