2014-05-08 57 views
0

如何檢索包含SQLite TIMESTAMP列的行並將這些列轉換爲Android中的Java日期類型參數。使用Android遊標從SQLite中檢索日期列db.query

我想要檢索採用Android光標db.query從SQLite數據庫行,這裏是表定義

private final static String TABLE_NAME = "custTable"; 
private final static String COLUMN_ID = "_id"; 
private final static String COLUMN_NAME = "name"; 
private final static String COLUMN_EMAIL = "email"; 
private final static String COLUMN_PHONE = "phone"; 
private final static String COLUMN_UID = "card_uid2"; 
private final static String COLUMN_INITIAL_VALUE = "initial_value"; 
private final static String COLUMN_CURRENT_VALUE = "current_value"; 
private final static String COLUMN_DATE_CREATED = "enrollment_date"; 
private final static String COLUMN_LAST_UPDATED = "last_transaction_date"; 

這裏是我的光標db.query聲明

Cursor cursor = database.query(TABLE_NAME, new String[]{ COLUMN_ID,COLUMN_NAME,COLUMN_EMAIL, COLUMN_PHONE + 
       COLUMN_UID, COLUMN_INITIAL_VALUE, COLUMN_CURRENT_VALUE,COLUMN_DATE_CREATED,COLUMN_LAST_UPDATED}, null, null, null, null, null); 

如果我這樣做這如何將我的兩個SQLite Timestamp列轉換或轉換爲Java日期參數。在多次嘗試之後,我再試圖用光標db.rawquery像這樣

Cursor cursor = database.rawQuery(
       "SELECT _id, name, email, phone, card_uid2, initial_value, current value," + 
       " (strftime('%s', enrollment_date) * 1000) AS enrollment_date, " + 
       " (strftime('%s', last_transaction_date) * 1000) AS last_transaction_date, +" + 
       "FROM custTable", new String[0]); 

隨着db.rawquery選項,我開始改變對Java日期參數喜歡

 long millis_enrolled = cursor.getLong(cursor.getColumnIndexOrThrow("enrollment_date")); 
     Date enrolledDate = new Date(millis_enrolled); 
     customer.setEnrollmentDate(enrolledDate); 

     long millis_updated = cursor.getLong(cursor.getColumnIndexOrThrow("last_transaction_date")); 
     Date updatedDate = new Date(millis_updated); 
     customer.setEnrollmentDate(updatedDate); 

無論哪種方式,我試着在我的活動來訪問這些值,我得到一個Java NullPointerException異常,像這樣

05-08 15:52:45.484: W/dalvikvm(1950): threadid=1: thread exiting with uncaught exception (group=0xb1f07648) 
05-08 15:52:45.484: E/AndroidRuntime(1950): FATAL EXCEPTION: main 
05-08 15:52:45.484: E/AndroidRuntime(1950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txsystems.valuecard/com.txsystems.valuecard.app.ReportActivity}: java.lang.NullPointerException 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.os.Looper.loop(Looper.java:137) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-08 15:52:45.484: E/AndroidRuntime(1950):  at java.lang.reflect.Method.invokeNative(Native Method) 

所以我的問題又是我如何檢索包含SQLite的TIMESTAMP列行和投那些列s到Java日期類型參數。

這裏是CREATE TABLE語句

private static final String CREATE_CUSTOMER_TABLE = 
       "CREATE TABLE " + TABLE_NAME + "(" + 
       COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       COLUMN_NAME + " TEXT NOT NULL, " + 
       COLUMN_EMAIL + " TEXT, " + 
       COLUMN_PHONE + " TEXT, " + 
       COLUMN_UID + " TEXT, " + 
       COLUMN_DATE_CREATED + " TIMESTAMP NOT NULL DEFAULT current_timestamp, " + 
       COLUMN_LAST_UPDATED + " TIMESTAMP NOT NULL DEFAULT current_timestamp, " + 
       COLUMN_INITIAL_VALUE + " NUMERIC, " + 
       COLUMN_CURRENT_VALUE + " NUMERIC " + 
       ")";  

更多的logcat輸出

05-08 17:12:46.857: I/Enrollment(2398): Calling the GetAll Customers method 
05-08 17:12:46.857: I/Enrollment(2398): Inside getAllCustomer function 
05-08 17:12:46.857: D/AndroidRuntime(2398): Shutting down VM 
05-08 17:12:46.857: W/dalvikvm(2398): threadid=1: thread exiting with uncaught exception (group=0xb1f07648) 
05-08 17:12:46.857: E/AndroidRuntime(2398): FATAL EXCEPTION: main 
05-08 17:12:46.857: E/AndroidRuntime(2398): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txsystems.valuecard/com.txsystems.valuecard.app.ReportActivity}: java.lang.NullPointerException 
05-08 17:12:46.857: E/AndroidRuntime(2398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
+2

我建議你看看完整的堆棧跟蹤,看看NPE在哪裏。 – njzk2

+0

我認爲我們需要看到create table語句。我不認爲有一個時間戳數據類型僅用於日期操作。 – danny117

+0

我已經添加了create table語句,並且可以使用sqlite3命令驗證該表是否已創建,並且正在將數據寫入表中。 –

回答

0

問題是,數據庫未打開之前,我試圖在活動中使用它,所以我發出的呼叫到db.getwritableDatabase()之前,我做了第一個數據庫查詢。

相關問題