如何将日期插入到 SQLite 中?
How to insert date into SQLite?
我遇到了这个错误:
Cannot resolve method 'put(java.lang.String, java.util.Date)'
在下面的代码中:
lsd
和 nsd
是数据类型为 DATE 的列名。
public void onCreate(SQLiteDatabase db)
{
String query="CREATE TABLE"+c_tablename+"(c_id int AUTO_INCREMENT primary key,name varchar(20),contact double ,address varchar(50)," +
"bike_number varchar(16),bike_type varchar(10),lsd date,nsd date,lwd varchar(100),cost int,message varchar(100))";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String query= "DROP TABLE IF EXIST "+c_tablename;
String query2="DROP TABLE IF EXIST "+h_tablename;
db.execSQL(query);
db.execSQL(query2);
onCreate(db);
}
public void saveData(String name , int contact , String address , String bike_number , String bike_type , java.util.Date lsd ,
java.util.Date nsd , String lwd , int cost , String message )
{
ContentValues contentValues=new ContentValues();
contentValues.put("name",name);
contentValues.put("contact",contact);
contentValues.put("address",address);
contentValues.put("bike_number",bike_number);
contentValues.put("bike_type",bike_type);
contentValues.put("lsd",lsd);
// The error is in the following line ("Cannot resolve method 'put(java.lang.String, java.util.Date)'"):
contentValues.put("nsd",nsd);
contentValues.put("lwd",lwd);
contentValues.put("cost",cost);
contentValues.put("message",message);
}
嗨,你能试试这样吗,
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ContentValues contentValues=new ContentValues();
contentValues.put("lsd", dateFormat.format(lsd));
Akshay 显示了您问题的正确解决方案。
背景信息是SQLite不支持日期类型:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values
的 SQLite 文档
我遇到了这个错误:
Cannot resolve method 'put(java.lang.String, java.util.Date)'
在下面的代码中:
lsd
和 nsd
是数据类型为 DATE 的列名。
public void onCreate(SQLiteDatabase db)
{
String query="CREATE TABLE"+c_tablename+"(c_id int AUTO_INCREMENT primary key,name varchar(20),contact double ,address varchar(50)," +
"bike_number varchar(16),bike_type varchar(10),lsd date,nsd date,lwd varchar(100),cost int,message varchar(100))";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String query= "DROP TABLE IF EXIST "+c_tablename;
String query2="DROP TABLE IF EXIST "+h_tablename;
db.execSQL(query);
db.execSQL(query2);
onCreate(db);
}
public void saveData(String name , int contact , String address , String bike_number , String bike_type , java.util.Date lsd ,
java.util.Date nsd , String lwd , int cost , String message )
{
ContentValues contentValues=new ContentValues();
contentValues.put("name",name);
contentValues.put("contact",contact);
contentValues.put("address",address);
contentValues.put("bike_number",bike_number);
contentValues.put("bike_type",bike_type);
contentValues.put("lsd",lsd);
// The error is in the following line ("Cannot resolve method 'put(java.lang.String, java.util.Date)'"):
contentValues.put("nsd",nsd);
contentValues.put("lwd",lwd);
contentValues.put("cost",cost);
contentValues.put("message",message);
}
嗨,你能试试这样吗,
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ContentValues contentValues=new ContentValues();
contentValues.put("lsd", dateFormat.format(lsd));
Akshay 显示了您问题的正确解决方案。
背景信息是SQLite不支持日期类型:
的 SQLite 文档SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values