保存sql数据库条目创建日期

时间:2013-08-03 10:53:04

标签: java android sql database date

我想保存并显示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)没有这样的列:日期 08-03 16:15:53.046:D / AndroidRuntime(30048):关闭VM 08-03 16:15:53.046:W / dalvikvm(30048):threadid = 1:线程退出时未捕获异常(组= 0x40d23a08) 08-03 16:15:53.056:E / AndroidRuntime(30048):致命异常:主要 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:没有这样的列:date(代码1):,同时编译:SELECT _id,title,body,color,date FROM notes 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread.access $ 600(ActivityThread.java:156) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1250) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.os.Handler.dispatchMessage(Handler.java:99) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.os.Looper.loop(Looper.java:137) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread.main(ActivityThread.java:5234) 08-03 16:15:53.056:E / AndroidRuntime(30048):at java.lang.reflect.Method.invokeNative(Native Method) 08-03 16:15:53.056:E / AndroidRuntime(30048):at java.lang.reflect.Method.invoke(Method.java:525) 08-03 16:15:53.056:E / AndroidRuntime(30048):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:799) 08-03 16:15:53.056:E / AndroidRuntime(30048):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 08-03 16:15:53.056:E / AndroidRuntime(30048):at dalvik.system.NativeStart.main(Native Method) 08-03 16:15:53.056:E / AndroidRuntime(30048):引起:android.database.sqlite.SQLiteException:没有这样的列:date(代码1):,同时编译:SELECT _id,title,body,color,日期来自笔记 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 08-03 16:15:53.056:E / AndroidRuntime(30048):at com.android.demo.notepad2.NotesDbAdapter.fetchAllNotes(NotesDbAdapter.java:134) 08-03 16:15:53.056:E / AndroidRuntime(30048):at com.android.demo.notepad2.Notepadv2.fillData(Notepadv2.java:64) 08-03 16:15:53.056:E / AndroidRuntime(30048):at com.android.demo.notepad2.Notepadv2.onCreate(Notepadv2.java:58) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.Activity.performCreate(Activity.java:5108) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-03 16:15:53.056:E / AndroidRuntime(30048):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266) 08-03 16:15:53.056:E / AndroidRuntime(30048):... 11 more

2 个答案:

答案 0 :(得分:1)

您的查询出现错误:使用其他名称更改date(以及_id);通常日期是保留关键字。

答案 1 :(得分:0)

我会把它放在评论中,但我的代表不够高。 我很确定有一种方法可以使SQL表在创建新条目时自动设置当前日期和时间。也许你可以尝试一下?自从我使用SQL以来已经有一段时间了,所以我可能错了。