Ошибка синтаксиса в инструкции insert into
Syntax error in INSERT INTO statement
I am trying to insert some information in an MS Access database. In my database I have the following columns and types:
The error that I get:
System.Data.OleDb.OleDbException: ‘Syntax error in INSERT INTO statement.’
I tried several posts but no post helped me. I believe there might be a problem with the autonumber column ( log_order ). Because of what I remember I don’t have to include it in the query.
PS: I know I have to pass the values as parameters.
Thank you in advance
Создан 29 июл. 17 2017-07-29 05:56:09 kdem
Try your query directly in Access and see what errors you get. I would guess maybe having quotes around the verifiedUser might cause a problem, if that’s actually a numeric field. – PhillipXT 29 июл. 17 2017-07-29 06:03:36
@marc_s please don’t do it.. I realise that many users come to SO for the quick fix their crap solution needs, but sometimes they really need to be told to start it over properly. You never know who might be writing an ecommerce solution that one day will store YOUR credit card number 😉 – Caius Jard 29 июл. 17 2017-07-29 06:10:43
3 ответа
Probably one of your variables (msg?) contains an apostrophe
The way you’ve written your SQL is a massive security risk. Please immediately look up «parameterized queries» and never, ever, ever write an sql like this again (where you use string concatenation to tack the values into the query). Your code has a proliferation of issues and using parameterized queries will solve all of them; they aren’t difficult to write
Создан 29 июл. 17 2017-07-29 05:59:01 Caius Jard
Thank for the advice. But msg variable is not the mistake. – kdem 29 июл. 17 2017-07-29 06:03:21
That entire sql is the mistake.. – Caius Jard 29 июл. 17 2017-07-29 06:04:27
It seems your data in some of the variables passed in INSERT may be causing this error. Try debugging the value in command.CommandText before executing it. If any of the variables have a single quote they must be escaped. Ref: How do I escape a single quote in SQL Server?
Also brush up on SQL Injection Ref: SQL Injection
Создан 29 июл. 17 2017-07-29 06:15:01 sosspyker
I totally agree with all that has been said, but to answer your question directly, I am pretty sure you will need to put square brackets around your field names. OleDb tends not to like special characters and could well be having a problem for example with date_ ; sending [date_] instead should get round the issue. It will not like time either. Same solution
Addendum on SQL Injection
As an aside, in fact calling Access through OleDb is relatively protected from SQL Injection. This is because any attempt to execute multiple instructions in one command fails. (You get an incorrect formatted string error). So whilst you could argue that what you are doing is safe, it is not for other db providers. The sooner you get into good habits, the less likely you will be to introduce a vulnerability in a case where it could be dangerous. If it seems like you are getting a stream of abuse, it is just because everyone here wants to keep the net safe.
Создан 29 июл. 17 2017-07-29 06:20:25 Jonathan Willcock
Ошибка синтаксиса в инструкции insert into
При попытке вставить запись выдается ошибка:
Ошибка синтаксиса в инструкции INSERT INTO.
If Right$(db_file, 1) <> «» Then db_file = db_file & «»
db_file = db_file & «Dispetcherskaj.mdb»
Set conn = New ADODB.Connection
«Persist Security Info=False»
For i = 1 To SQL_Text_day
SQL_string = «INSERT INTO Gurnal (year, month, day) VALUES (» & SQL_Text_year & «,» & SQL_Text_month & «,» & i & «)»
Ответы | Всего ответов: 4 |
Номер ответа: 1 Автор ответа: ![]() Вопросов: 43 | Добавлено: 04.01.03 22:29 |
А разве можно использовать в INSERT INTO такой синтаксис. Этот способ у тебя когда-нибудь работал? или это ты впервые применяешь. ![]() Ответить |
Номер ответа: 2 Автор ответа: ![]() Вопросов: 21 | Добавлено: 04.01.03 23:30 |
А где Ты заполняешь переменные SQL_Text_day, SQL_Text_year и SQL_Text_month? Это во-первых. Во-вторых проверь тип данных этих переменных. Если они Integer, то запись верна, а если String то нужно добавлять «‘» .Т.е. Values (‘» & SQL_Text_year & «‘,'» & SQL_Text_month & «‘,» & i & «)» ![]() Ответить |
Номер ответа: 3 Автор ответа: ![]() Вопросов: 21 | Добавлено: 05.01.03 10:06 |
Синтаксис у Тебя вроде бы правильный. Но я не видел где Ты определяешь и заполняешь переменные SQL_Text_day, SQL_Text_month и SQL_Text_year. поставь BreakPoint на строчку conn.Execute SQL_string и проверь какой String Ты получил. Потому, что Ты подставляешь переменные SQL_*** как Integer-ы, а в таблице поля определены тоже как Integer-ы или String-и или Date ? Ты должен заносить данные в таблицу в соответсвии с типом данных поля. Кроме того проверь нет ли у Тебя определение какого-либо поля как поля принимающего данные Unique (запрещает вносить повторно одинаковые записи) учитывая, что запись у Тебя идет в цикле может быть в этом причина? ![]() Ответить |
Номер ответа: 4 Автор ответа: ![]() Вопросов: 5 | Добавлено: 05.01.03 19:51 |