C# 系统日期在Sqlite数据库中保存为4位数字
在我的Unity游戏中,我获取系统日期并将其发送到Sqlite数据库。但在sqlite数据库中,日期保存为4位数字 这是我用来获取日期的代码C# 系统日期在Sqlite数据库中保存为4位数字,c#,sqlite,unity3d,C#,Sqlite,Unity3d,在我的Unity游戏中,我获取系统日期并将其发送到Sqlite数据库。但在sqlite数据库中,日期保存为4位数字 这是我用来获取日期的代码 PlayerPrefs.SetString("date_time", System.DateTime.Now.ToString("yyyy-MM-dd")); CurrentDate = PlayerPrefs.GetString("date_time"); 这就是我将其插入数据库的方式 //Add new event with o timer at
PlayerPrefs.SetString("date_time", System.DateTime.Now.ToString("yyyy-MM-dd"));
CurrentDate = PlayerPrefs.GetString("date_time");
这就是我将其插入数据库的方式
//Add new event with o timer at the start of the game
public void AddEvent(){
using (IDbConnection dbConnection = new SqliteConnection(connectionString)){
dbConnection.Open();
using(IDbCommand dbCmd = dbConnection.CreateCommand()){
string sqlQuery = String.Format("INSERT INTO events(date,timer_count) VALUES({0},0)",System.DateTime.Now.ToString("yyyy-MM-dd"));
dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbConnection.Close();
}
}
PlayerPrefs.SetString("date_time", System.DateTime.Now.ToString("yyyy-MM-dd"));
Debug.Log(PlayerPrefs.GetString("date_time"));
}
这是我在unity editor中打印日期时的样子:
但这是Sqlite数据库上的情况:
当日期增加时,数字会减少
示例:2018-10-28=>1982 |
2018年10月29日=>1981年
我想以Unity editor中显示的格式将日期存储在sqlite数据库中。yyyy-mm-dd将占位符添加到查询中,如“{0}”
注意:使用预先准备好的语句来防止sql注入。什么是2018减去10减去28确定你没有看到1980?为什么不在insert语句中为日期字符串使用占位符?请尝试以下操作=>string sqlQuery=string.FormatINSERT到eventsdate,timer\u计数值{0},0,System.DateTime.Now.date@肖恩:是的,它的2018减去10减去28,当它转换成字符串时怎么会发生呢?当我将其更改为2018/10/29时,它在数据库中存储了6。我将System.DateTime.Now.TOSTRINGYYY-MM-dd更改为System.DateTime.Now.ToString.Substring0,10以仅获取日期。现在它将0存储在sqlite数据库的日期字段中。@SNoV,您是否尝试过我上面的注释代码语句?
string sqlQuery = String.Format("INSERT INTO events(date,timer_count) VALUES('{0}',0)", System.DateTime.Now.Date);