Исключение в запросе sqlite3 c # - PullRequest
0 голосов
/ 28 сентября 2018

ниже - мой код для вставки данных в базу данных sqlite3, имя таблицы и запрос кажутся правильными, но всякий раз, когда я вставляю данные, возникает исключение.

Image of exception

        query = "INSERT INTO Order (Name, Quantity, SUPrice, Desc, Total, Paid, Remain) VALUES ( '" + orderDto.Name + "', '" + orderDto.Quantity + "', '" + orderDto.SUPrice + "', '" + orderDto.Description + "', '" + orderDto.Total + "', '" + orderDto.Paid + "', '" + orderDto.Remaining  + "')";
        connection.Open();
        command = new SQLiteCommand(query, connection);
        command.ExecuteNonQuery();
        connection.Close();

Ответы [ 2 ]

0 голосов
/ 28 сентября 2018

Я бы порекомендовал использовать string.Format для запросов к БД, он менее беспорядочный.

query = string.Format("INSERT INTO Order (Name, Quantity, SUPrice, Desc, Total, Paid, Remain) VALUES ( '{0}', '{1}', '{2}', '{3}', '{3}', '{4}', '{5}')", orderDto.Name, orderDto.Quantity, orderDto.SUPrice, orderDto.Description, orderDto.Total, orderDto.Paid, orderDto.Remaining);

Также попробуйте выполнить запрос в браузере БД для SQLite, вы можете сначала проверить свои запросы, а затем вставить их в свой код,http://sqlitebrowser.org/

0 голосов
/ 28 сентября 2018

Вы можете попытаться экранировать order с двойными кавычками ", потому что order является ключевым словом.

, но есть проблема в вашем коде SQL-инъекция .

Я бы использовал параметры вместо строки SQL подключения.

query = "INSERT INTO \"Order\" (Name, Quantity, SUPrice, Desc, Total, Paid, Remain) VALUES (@Name,@Quantity,@SUPrice,@Description,@Total,@Paid,@Remain)"
connection.Open();
command = new SQLiteCommand(query, connection);
command.Parameters.Add(new SQLiteParameter("@Name", orderDto.Name);
command.Parameters.Add(new SQLiteParameter("@Quantity", orderDto.Quantity));
command.Parameters.Add(new SQLiteParameter("@SUPrice" , orderDto.SUPrice));
command.Parameters.Add(new SQLiteParameter("@Description" , orderDto.Description));
command.Parameters.Add(new SQLiteParameter("@Total", orderDto.Total));
command.Parameters.Add(new SQLiteParameter("@Paid" , orderDto.Paid));
command.Parameters.Add(new SQLiteParameter("@Remain" , orderDto.Remaining));
command.ExecuteNonQuery();
connection.Close();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...