我讀了幾本教程和一本書中的sqlite部分,並應用它,它工作得很好。現在我搞砸了它,試圖理解它,但現在它只會迫使每一次關閉,我不知道爲什麼。我已經一次又一次地結束了我的代碼,但也許我找不到問題,因爲我一般都是新手到sqlite /數據庫。任何幫助,將不勝感激:Android的sqlite問題
public class Events extends Activity {
private TextView tv;
private DataHelper dh;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
dh = new DataHelper(this);
dh.deleteAll();
//dh.addEvent("James");
//Cursor c = dh.getEvent();
//startManagingCursor(c);
//tv.setText(dh.showEvent(c));
setContentView(tv);
}
}
public class EventsData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE" + TABLE_NAME + "(" + TITLE + " TEXT)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public EventsData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}
}
public class DataHelper {
private Context context;
private SQLiteDatabase db;
private EventsData eventsData;
public DataHelper(Context context) {
this.context = context;
eventsData = new EventsData(this.context);
}
public void addEvent(String name) {
db = eventsData.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TITLE, name);
db.insertOrThrow(TABLE_NAME, null, cv);
}
}
註釋掉的addEvent()將運行應用程序很好,但一旦我取消它,它迫使關閉。
謝謝! =] – semajhan 2011-01-07 05:59:20