如何检索包含SQLite TIMESTAMP列的行并将这些列强制转换为Android中的Java日期类型参数。
我想使用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时间戳列转换或转换为Java Date参数。经过多次尝试,我尝试使用游标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 Date参数,如
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);
无论哪种方式,当我尝试在我的Activity中访问这些值时,我得到一个类似的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列的行并将这些列转换为Java Date类型参数。
这是创建表语句
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)
答案 0 :(得分:0)
问题是在我尝试在Activity中使用数据库之前数据库没有打开,因此我在进行第一次数据库查询之前就调用了db.getwritableDatabase()
。