我想保存并显示一个 sql 条目的创建日期。但是我的应用程序不断因这段代码而崩溃。任何帮助,将不胜感激!
这是保存笔记的保存状态方法:
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
String color = mColor;
String date=(DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE ));
//String date = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis( ));
if(title != null && !title.isEmpty() || body != null && !body.isEmpty()){
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, color, date);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, color, date);
}
}
这是数据库适配器:
public class NotesDbAdapter {
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
public static final String KEY_COLOR = "color";
public static final String KEY_DATE = "date";
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null, color text not null, date text not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 4;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createNote(String title, String body, String color, String date) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
initialValues.put(KEY_COLOR, color);
initialValues.put(KEY_DATE, date);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_COLOR, KEY_DATE}, null, null, null, null, null);
}
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_BODY, KEY_COLOR, KEY_DATE}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateNote(long rowId, String title, String body, String color, String date) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
args.put(KEY_COLOR, color);
args.put(KEY_DATE, date);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
更新 运行后好的,这是我得到的错误日志:
08-03 16:15:52.816: I/Process(29938): 发送信号。PID: 29938 SIG: 9 08-03 16:15:53.046: E/SQLiteLog(30048): (1) no such column: date 08-03 16:15:53.046: D/AndroidRuntime(30048): 关闭 VM 08 -03 16:15:53.046: W/dalvikvm(30048): threadid=1: 线程退出未捕获异常 (group=0x40d23a08) 08-03 16:15:53.056: E/AndroidRuntime(30048): 致命异常: main 08 -03 16:15:53.056:E/AndroidRuntime(30048):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.android.demo.notepad2/com.android.demo.notepad2.Notepadv2}:android.database。 sqlite.SQLiteException: no such column: date (code 1): , while compile: SELECT _id, title, body, color, date FROM notes 08-03 16:15:53.056: E/AndroidRuntime(30048): at android.app .ActivityThread.performLaunchActivity(ActivityThread.java:2312) 08-03 16:15:53.056: E/AndroidRuntime(30048):