在SQLite数据库中插入TimeStamp列时出错

时间:2013-12-19 06:33:29

标签: android sqlite

我正在尝试在我的SQLite表中添加TimeStamp字段,请参阅下面的代码:

myDBClass.java

   @Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    // Create Table Name
    db.execSQL("CREATE TABLE " + TABLE_MEMBER + 
              "(OperatorID INTEGER PRIMARY KEY AUTOINCREMENT," +
              " DeviceID TEXT(100)," +
              " EmailID TEXT(100)," +
              " Operator TEXT(100)," +
              " Created_at DATETIME DEFAULT CURRENT_TIMESTAMP);");

    Log.d("CREATE TABLE","Create Table Successfully.");
}

// Insert Data
public long InsertData(String strDeviceID, String strEmailID, String strEventOperator, String strCreated_at) {
    // TODO Auto-generated method stub

     try {
        SQLiteDatabase db;
        db = this.getWritableDatabase(); // Write Data

        ContentValues Val = new ContentValues();
        Val.put("DeviceID", strDeviceID);
        Val.put("EmailID", strEmailID);
        Val.put("Operator", strEventOperator);
        Val.put("Created_at", getDateTime());

        long rows = db.insert(TABLE_MEMBER, null, Val);

        db.close();
        return rows; // return rows inserted.

     } catch (Exception e) {
        return -1;
     }

}

private String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
  }


// Select Data
public String[] SelectData(String strOperatorID) {
    // TODO Auto-generated method stub

     try {
        String arrData[] = null;    

         SQLiteDatabase db;
         db = this.getReadableDatabase(); // Read Data

         Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" }, 
                    "OperatorID=?",
                    new String[] { String.valueOf(strOperatorID) }, null, null, null, null);

            if(cursor != null)
            {
                if (cursor.moveToFirst()) {
                    arrData = new String[cursor.getColumnCount()];
                    /***
                     *  0 = DeviceID
                     *  1 = EmailID
                     *  2 = EventOperator
                     */
                    arrData[0] = cursor.getString(0);
                    arrData[1] = cursor.getString(1);
                    arrData[2] = cursor.getString(2);
                }
            }
            cursor.close();
            db.close();
            return arrData;

     } catch (Exception e) {
        return null;
     }

}

AddActivity.java

public boolean SaveData()
    {       
        final EditText editDeviceID = (EditText) findViewById(R.id.editDeviceID);
        final EditText editEmailID = (EditText) findViewById(R.id.editEmailID);
        final EditText editEveOpe = (EditText) findViewById(R.id.editEveOpe);       
        // Dialog
        final AlertDialog.Builder adb = new AlertDialog.Builder(this);
        AlertDialog ad = adb.create();

        // new Class DB
        final myDBClass myDb = new myDBClass(this);

        // Save Data
        long saveStatus = myDb.InsertData(
                editDeviceID.getText().toString(),
                editEmailID.getText().toString(),
                editEveOpe.getText().toString()
                );
        if(saveStatus <=  0)
        {
            ad.setMessage("Error!! ");
            ad.show();
            return false;
        }

         Toast.makeText(AddActivity.this,"Added Data Successfully. ",
                 Toast.LENGTH_SHORT).show();   

        return true;
    }

}

日志:

12-19 01:39:08.147: E/SQLiteLog(6963): (1) table members has no column named Created_at
12-19 01:39:08.203: E/SQLiteDatabase(6963): Error inserting DeviceID=safdasfewrwesd23432 Created_at=2013-12-19 01:39:08 EventOperator=adsfewvweceEvent EmailID=sdfasdf@desa.cond
12-19 01:39:08.203: E/SQLiteDatabase(6963): android.database.sqlite.SQLiteException: table members has no column named Created_at (code 1): , while compiling: INSERT INTO members(DeviceID,Created_at,EventOperator,EmailID) VALUES (?,?,?,?)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at com.example.database.myDBClass.InsertData(myDBClass.java:59)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at com.example.database.AddActivity.SaveData(AddActivity.java:49)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at com.example.database.AddActivity$1.onClick(AddActivity.java:26)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.view.View.performClick(View.java:4240)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.view.View$PerformClick.run(View.java:17721)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.os.Handler.handleCallback(Handler.java:730)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.os.Looper.loop(Looper.java:137)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at java.lang.reflect.Method.invoke(Method.java:525)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 01:39:08.203: E/SQLiteDatabase(6963):     at dalvik.system.NativeStart.main(Native Method)
12-19 01:39:08.434: D/dalvikvm(6963): GC_FOR_ALLOC freed 138K, 7% free 3169K/3384K, paused 28ms, total 38ms

我正在表格成员没有列名为Created_at

我对插入查询* * (AddActivity.java) *

* Created_at列的用法有点混淆

仍然在我的成员表中,字段为:OperatorID,DeviceID,EmailID,Operator但我也尝试在成员表中添加 Created_at 字段。但是面临问题

3 个答案:

答案 0 :(得分:1)

sqlite中没有像 DATETIME 这样的数据类型。所以只需更改下面的代码:

db.execSQL("CREATE TABLE " + TABLE_MEMBER + 
              "(OperatorID INTEGER PRIMARY KEY AUTOINCREMENT," +
              " DeviceID TEXT(100)," +
              " EmailID TEXT(100)," +
              " Operator TEXT(100)," +
              " Created_at TIMESTAMP NOT NULL DEFAULT current_timestamp);");

答案 1 :(得分:1)

Sqlite不支持DATETIME类型。相反,您可以将时间戳存储为整数(毫秒)或文本。

答案 2 :(得分:0)

更改数据库架构时,例如代码中的CREATE TABLE,您必须确保对实际数据库文件也进行相同的更改。

SQLiteOpenHelper将对您的数据库文件进行版本控制。它是传递给SQLiteOpenHelper构造函数的版本号,它告诉当前的代码架构版本。更改架构时,应增加该版本号。

当代码中的架构版本高于数据库文件时,

SQLiteOpenHelper将调用onUpgrade()。您可以调用ALTER TABLE等来升级数据库,或者只调用现有表上的DROP TABLE并调用onCreate()以使用最新架构创建它们。在后一种情况下,您将丢失数据库中的所有数据。

另一种更简单的方法:只需删除旧的数据库文件,而不是增加架构版本号。您可以通过设置清除应用数据来执行此操作 - &gt;管理应用程序或者只是卸载并重新安装应用程序。不过,对于已发布的版本,请不要依赖此技巧,只能在开发期间进行架构更新。