-1
我想在Andoird项目的Sqlite数据库中插入datetime字段。尝试了很多方法like this但插入日期时间字段没有运气。以下是事实Android:在Sqlite数据库中插入datetime
1]我的sqlite的字段类型时间戳
2]数据库类
package com.gcm.pushandroid;
import java.util.Date;
public class Notifications {
//private variables
String _type;
String _time_stamp;
// Empty constructor
public Notifications(){
}
// constructor
public Notifications(String type, String timeStamp){
this._type = type;
this._time_stamp = timeStamp;
}
// getting ID
public String getType(){
return this._type;
}
// setting id
public void setType(String type){
this._type = type;
}
// getting timestamp
public String getTimeStamp(){
return this._time_stamp;
}
// setting
public void setTimeStamp(String time_stamp){
this._time_stamp = time_stamp;
}
}
3]这是代码
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TYPE, notificationObj.getType());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
values.put("time_stamp", dateFormat.format(date));
db.insert(TABLE_CONTACTS, null, values);
db.close();
不知道什么错误。帮助很重要。 谢谢
我们不知道什么是无论是走错了。你说“没有运气插入”它......这是什么意思? – Eric
“没有运气”并没有描述你想改正的错误行为。对于它的价值,SQLite没有日期/时间列类型;请参阅http://www.sqlite.org/datatype3.html。您必须决定哪种格式最能代表日期和时间,并自行进行转换。您不一定必须编写转换代码,因为SQLite中有日期/时间转换函数,但是您必须决定是将日期/时间信息表示为字符串,整型风格的UNIX时间还是您自己。如果你描述你看到的不喜欢的东西,你会得到更多的帮助。 – prprcupofcoffee