如何在 Android 应用程序中插入日期时间设置为“现在”的 SQLite 记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/754684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a SQLite record with a datetime set to 'now' in Android application?
提问by droidguy
Say, we have a table created as:
假设我们创建了一个表:
create table notes (_id integer primary key autoincrement, created_date date)
To insert a record, I'd use
要插入记录,我会使用
ContentValues initialValues = new ContentValues();
initialValues.put("date_created", "");
long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
But how to set the date_created column to now? To make it clear, the
但是如何将 date_created 列设置为now?为明确起见,
initialValues.put("date_created", "datetime('now')");
Is not the right solution. It just sets the column to "datetime('now')" text.
不是正确的解决方案。它只是将列设置为“datetime('now')”文本。
回答by e-satis
You cannot use the datetime function using the Java wrapper "ContentValues". Either you can use :
您不能使用 Java 包装器“ContentValues”来使用 datetime 函数。您可以使用:
SQLiteDatabase.execSQL so you can enter a raw SQL query.
mDb.execSQL("INSERT INTO "+DATABASE_TABLE+" VALUES (null, datetime()) ");
Or the java date time capabilities :
// set the format to sql date time SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); ContentValues initialValues = new ContentValues(); initialValues.put("date_created", dateFormat.format(date)); long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
SQLiteDatabase.execSQL 以便您可以输入原始 SQL 查询。
mDb.execSQL("INSERT INTO "+DATABASE_TABLE+" VALUES (null, datetime()) ");
或 java 日期时间功能:
// set the format to sql date time SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); ContentValues initialValues = new ContentValues(); initialValues.put("date_created", dateFormat.format(date)); long rowId = mDb.insert(DATABASE_TABLE, null, initialValues);
回答by Gautier Hayoun
In my code I use DATETIME DEFAULT CURRENT_TIMESTAMP
as the type and constraint of the column.
在我的代码中,我DATETIME DEFAULT CURRENT_TIMESTAMP
用作列的类型和约束。
In your case your table definition would be
在您的情况下,您的表定义将是
create table notes (
_id integer primary key autoincrement,
created_date date default CURRENT_DATE
)
回答by Rikin Patel
Method 1
方法一
CURRENT_TIME – Inserts only time
CURRENT_DATE – Inserts only date
CURRENT_TIMESTAMP – Inserts both time and date
CREATE TABLE users(
id INTEGER PRIMARY KEY,
username TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Method 2
方法二
db.execSQL("INSERT INTO users(username, created_at)
VALUES('ravitamada', 'datetime()'");
Method 3Using java Date functions
方法三使用java日期函数
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
ContentValues values = new ContentValues();
values.put('username', 'ravitamada');
values.put('created_at', getDateTime());
// insert the row
long id = db.insert('users', null, values);
回答by sooniln
There are a couple options you can use:
您可以使用以下几个选项:
- You could try using the string "(DATETIME('now'))" instead.
- Insert the datetime yourself, ie with System.currentTimeMillis()
- When creating the SQLite table, specify a default value for the created_date column as the current date time.
- Use SQLiteDatabase.execSQLto insert directly.
- 您可以尝试使用字符串 "(DATETIME('now'))" 代替。
- 自己插入日期时间,即使用 System.currentTimeMillis()
- 创建 SQLite 表时,将 created_date 列的默认值指定为当前日期时间。
- 使用SQLiteDatabase.execSQL直接插入。
回答by Jared Harley
To me, the problem looks like you're sending "datetime('now')"
as a string, rather than a value.
对我来说,问题看起来像是"datetime('now')"
作为字符串而不是值发送。
My thought is to find a way to grab the current date/time and send it to your database as a date/time value, or find a way to use SQLite's built-in (DATETIME('NOW'))
parameter
我的想法是找到一种方法来获取当前日期/时间并将其作为日期/时间值发送到您的数据库,或者找到一种使用 SQLite 内置(DATETIME('NOW'))
参数的方法
Check out the anwsers at this SO.com question- they might lead you in the right direction.
在这个 SO.com 问题中查看答案- 他们可能会引导您走向正确的方向。
Hopefully this helps!
希望这会有所帮助!
回答by Marco Gallella
You can use the function of java that is:
您可以使用 java 的功能,即:
ContentValues cv = new ContentValues();
cv.put(Constants.DATE, java.lang.System.currentTimeMillis());
In this way, in your db you save a number.
This number could be interpreted in this way:
这样,在你的数据库中你保存了一个数字。
这个数字可以这样解释:
DateFormat dateF = DateFormat.getDateTimeInstance();
String data = dateF.format(new Date(l));
Instead of l into new Date(l), you shoul insert the number into the column of date.
So, you have your date.
For example i save in my db this number : 1332342462078
But when i call the method above i have this result: 21-mar-2012 16.07.42
您应该将数字插入到日期列中,而不是将 l 插入到 new Date(l) 中。
所以,你有你的约会。
例如,我在我的数据库中保存了这个数字:1332342462078
但是当我调用上面的方法时,我得到了这个结果:21-mar-2012 16.07.42
回答by Cedric Simon
Based on @e-satis answer I created a private method on my "DBTools" class so adding current date is now really easy:
基于@e-satis 的答案,我在“DBTools”类上创建了一个私有方法,因此添加当前日期现在非常容易:
...
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
...
private String getNow(){
// set the format to sql date time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
...
Use it now like this: values.put("lastUpdate", getNow());
现在像这样使用它: values.put("lastUpdate", getNow());
回答by Stefan Beike
Works for me perfect:
对我来说很完美:
values.put(DBHelper.COLUMN_RECEIVEDATE, geo.getReceiveDate().getTime());
Save your date as a long.
将您的日期保存为长时间。
回答by Kristy Welsh
This code example may do what you want:
此代码示例可能会执行您想要的操作:
回答by AlokJoshiOfAarmax
Make sure that the field is not marked as 'not null' at the same time as you are trying to insert a default time stamp using the expression "(DATETIME('now'))"
在您尝试使用表达式“(DATETIME('now'))”插入默认时间戳时,请确保该字段未标记为“非空”