简体   繁体   中英

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 ). Nevertheless when I update the database, sqlite cuts off leading zeros , so 09.2019 appears as 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.

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)

// 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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