系统日期另存为Sqlite数据库Unity中的4位数字

时间:2018-10-29 07:12:38

标签: 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 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"));
    }

这是我打印出来的日期在统一编辑器中的样子: enter image description here

但是这是在Sqlite数据库上的样子: enter image description here

当日期增加时,数字减少。

  

例如:2018-10-28 => 1982 |            2018-10-29 => 1981

我想将日期存储在sqlite数据库中,格式与Unity编辑器中显示的格式相同。 (yyyy-mm-dd)

1 个答案:

答案 0 :(得分:1)

为查询添加占位符,例如('{0}')

string sqlQuery = String.Format("INSERT INTO events(date,timer_count) VALUES('{0}',0)", System.DateTime.Now.Date);

注意:使用准备好的语句可以防止sql注入。