C# SQLite Insert Datetime type Value

徘徊边缘 提交于 2021-02-17 07:09:25

问题


I want Insert DateTime Type Value.But I got this Error

Source Code

        private void Execute(string query)
        {
            SQLiteCommand cmd = new SQLiteCommand(query, conn);
            int result = cmd.ExecuteNonQuery(); 
            Debug("Executed query: {0} Result: {1}", query, result);
        }
        private string DEscape(DateTime time)
        {
            return time.ToString(@"\'yyyy-MM-dd\'");
        }
        public void InsertVideo(string videoID, DateTime releaseDate, string[] genres)
        {
            string genreStr = "";
            foreach (string g in genres)
            {
                genreStr = genreStr + (genreStr.Length == 0 ? g : (", " + g));
            }
            string query = $@"INSERT INTO `Video` (VideoID, releaseData, genres) VALUES 
            ({Escape(videoID)} {DEscape(releaseDate)} {Escape(genreStr)});";
            Execute(query);
        }
        //EagleSystem.cs
        DBManager dbm = new DBManager();
        dbm.InsertVideo("ABC-123", new DateTime(2021, 10, 30), new string[] 
        {"Natural","Sports" });

Error:System.Data.SQLite.SQLiteException: 'SQL logic error near "'2021-10-30'": syntax error


回答1:


There is an issue with the generated query. There is a syntax error. So the best approach is to put a debug point or log the query and try to fix the query syntax error.

As I can see in your code, you have to remove quotes from table name Video and put , between the values.




回答2:


OK, so this is how we deal with problems like this. There's a problem during the execute - syntax error. It means the SQL is badly formed. Let's take our code, and click in the margin next to the numbers, somewhere near where we form the SQL. A red dot appears and the line goes red:

Then we run the program. The program will freeze when we try to insert a video (so click whatever button needed to do it) and visual studio will come to the foreground. The line is yellow meaning "this is where the executing has paused":

And we can see at the bottom, in the Autos (or Locals) window is a list of th variables we have, what their values are etc:

Press F10 to step one line of code further, and then again, one more time.. Every time you press F10 it advances one step, and the values of all the variables change. So let's get to the point AFTER where we made the query string, and POINT YOUR MOUSE at the word "query":

The tooltip shows us the value of the string (but it also shows invisible characters like carriage returns etc so it might be slightly confusing - you can also look in the Autos or Locals window, or you can click the magnifying glass in the tooltip to open the string visualizer:

So this is the actual SQL query you send to SQLite..

..And we can see it has quite a few syntax errors

INSERT INTO `Video` (VideoID, releaseData, genres) VALUES 
        (ABC-123 '2021-10-30' Natural, Sports);

It has:

  • Wrong column name - vedioid vs videoid
  • Missing ' around strings - is ABC-123 a string? or is it a subtract operation?
  • Missing commas between values
  • Genres, again maybe missing apostrophes, or some other way to represent it - right now it's comma separated which means we've actually supplied 4 values to 3 columns

This is how to use the debugger to find out your problem. I'll leave the exact fix up to you because I don't know exactly what you want to do. Good luck!



来源:https://stackoverflow.com/questions/66183425/c-sharp-sqlite-insert-datetime-type-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!