How do I get the Timestamp value from database column via a cursor and store it in a Timestamp variable in android sdk. Im using the java.sql.Timestamp and the value in the sqlite database is of type TIMESTAMP. I have created an class having all the fields as the database column names and wish to create a new object after reading a record from the database.
如何通过游标从数据库列获取时间戳值,并将其存储在android sdk中的时间戳变量中。使用java.sql Im。时间戳和sqlite数据库中的值是时间戳类型。我创建了一个类,它拥有所有字段作为数据库列的名称,并希望在从数据库中读取记录之后创建一个新对象。
1 个解决方案
#1
19
You have to use the Cursor methods to get the value as a string and then use the Timestamp class's static valueOf method to convert it back to a proper Timestamp:
您必须使用游标方法将值作为字符串获取,然后使用Timestamp类的静态valueOf方法将其转换为合适的时间戳:
Timestamp.valueOf(cursor.getString(0))
I use this all over the place and it works like a charm. For example, while iterating over the result set, you can set the Timestamp fields of your object.
我到处都在用它,它就像一个咒语。例如,在遍历结果集时,可以设置对象的时间戳字段。
obj.setId(cursor.getLong(0));
obj.setName(cursor.getName(1));
obj.setAddDate(Timestamp.valueOf(cursor.getString(2)));
I hope that's what you're looking for.
我希望这就是你要找的。
#1
19
You have to use the Cursor methods to get the value as a string and then use the Timestamp class's static valueOf method to convert it back to a proper Timestamp:
您必须使用游标方法将值作为字符串获取,然后使用Timestamp类的静态valueOf方法将其转换为合适的时间戳:
Timestamp.valueOf(cursor.getString(0))
I use this all over the place and it works like a charm. For example, while iterating over the result set, you can set the Timestamp fields of your object.
我到处都在用它,它就像一个咒语。例如,在遍历结果集时,可以设置对象的时间戳字段。
obj.setId(cursor.getLong(0));
obj.setName(cursor.getName(1));
obj.setAddDate(Timestamp.valueOf(cursor.getString(2)));
I hope that's what you're looking for.
我希望这就是你要找的。