简体   繁体   English

SQLITE 在更新数据库时切断零

[英]SQLITE cuts off zeros, when updating database

Im working with a Sqlite database in C#, when I insert my date into it, everything works fine (in the format MM.YYYY ).我在 C# 中使用 Sqlite 数据库,当我将日期插入其中时,一切正常(格式MM.YYYY )。 Nevertheless when I update the database, sqlite cuts off leading zeros , so 09.2019 appears as 9.2019 .尽管如此,当我更新数据库时, sqlite 会切断前导零,因此09.2019显示为9.2019 But it's important for further operations that the leading zero remains in it's place.但是对于进一步的操作来说,重要的是前导零保持在原位。

When creating the table "credits", I use TEXT for the date to store it.创建表“credits”时,我使用 TEXT 作为日期来存储它。

I show you some code how I update the database:我向您展示了一些如何更新数据库的代码:

SQLiteCommand command = new SQLiteCommand(databaseConnection);

command.CommandText = "Update credits Set lastDate = " + date +
     " WHERE ID=" + Convert.ToString(index);

command.ExecuteScalar();

Try parametrizing your query;尝试参数化您的查询; all you should provide is date and let RDBMS do its work (formats, representation etc. included)您应该提供的只是date并让 RDBMS 完成其工作(包括格式、表示等)

// using - do not forget to Dispose (i.e. free resources)
using (SQLiteCommand command = new SQLiteCommand(databaseConnection)) {
  // Do not build query, but parametrize it
  command.CommandText = 
     @"update credits 
          set lastDate = @prm_Date
        where ID = @prm_ID";

  // Parameters instead hardcoding
  //TODO: better put command.Parameters.Add(Name, Value, Type)
  command.Parameters.AddWithValue("@prm_Date", date);
  command.Parameters.AddWithValue("@prm_ID", index);

  // Non query : we don't want to return even a single value, right?
  command.ExecuteNonQuery();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM