SQLite测试活动收到错误

时间:2012-02-16 15:56:41

标签: android sqlite

我正在学习SQLite,我写了以下课程

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 Double[] getMP_RPY(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT roll,pitch,yaw FROM MP_Data WHERE "+
                                                BaseColumns._ID+"="+
                                                Long.toString(id), null);
    c.moveToFirst();
    Double[] r = { c.getDouble(0), c.getDouble(1), c.getDouble(2) };
    return r;       
}

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

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 void deleteMP(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(MP_TABLE_NAME, BaseColumns._ID+"="+Long.toString(id),null);   
}

以及以下活动:

public class SQLSaveData extends Activity{

EditText et01, et02;
Button btn;
SQLiteTestActivity mphelper;
SQLiteDatabase db ;
ContentValues cv;

OnKeyListener et01Listener = new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if ( (event.getAction() == KeyEvent.ACTION_DOWN) &&
              ( keyCode == KeyEvent.KEYCODE_ENTER ) ) {

            save("name", et01.getText().toString());

            return true;
        }
        return false;
    }
};

OnKeyListener et02Listener = new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if ( (event.getAction() == KeyEvent.ACTION_DOWN) &&
                  ( keyCode == KeyEvent.KEYCODE_ENTER ) ) {

            save("lat", et02.getText().toString()); 
                return true;
            }
        return false;
    }
};


public void save(String key, String value)
{
    cv.put(key, Double.valueOf(value));
    long newID = db.insert("MPDetails", null, cv);
}

@Override    
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et01 = (EditText) findViewById(R.id.et01);
    et02 = (EditText) findViewById(R.id.et02);
    btn = (Button) findViewById(R.id.btn01);

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

    et01.setOnKeyListener(et01Listener);
    et02.setOnKeyListener(et02Listener);
}

但我总是收到错误,上面写着“App。意外停止”; 当我禁用“评论”以下三行时: APP。不会给出错误,但是当我启用以下三行时 它崩溃了

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

1 个答案:

答案 0 :(得分:0)

我认为您需要在CREATE字符串的末尾添加一个空格,除非您在MP_TABLE_NAME的开头有空格,这将是奇怪的。

"CREATE TABLE " + MP_TABLE_NAME

这可能无法解决您的问题,但这是一个开始。