[RESOLVED] SQL INSERT error
I am trying to INSERT new data into an MsACCESS 2003 DB. The field am trying to populate is comments field, with a field type of memo. When I type my comments with an apostrophe (') I get an error when submitting the info but without the apostrophe, it is Ok. How can I code to make sure that the apostrophe does not create an error?
Re: [RESOLVED] SQL INSERT error
Its already assigned to the command object's CommandText property SeanK.
Re: [RESOLVED] SQL INSERT error
No, and the sSQL is wrong anyways.... in your example, there isn't any placeholders for the data... you've concatenated them back in... this thread is the perfect example of why string concatenation is not recommended for SQL strings.... as evidence has shown problems arise when string delimeters show up in the data. By using parameterized queries, the problem goes away as ADO will take care of the translation for you. Also prevents the possibility of a SQL Injection attack....
-tg
Re: [RESOLVED] SQL INSERT error
Quote:
Originally Posted by techgnome
No, and the sSQL is wrong anyways.... in your example, there isn't any placeholders for the data... you've concatenated them back in... this thread is the perfect example of why string concatenation is not recommended for SQL strings.... as evidence has shown problems arise when string delimeters show up in the data. By using parameterized queries, the problem goes away as ADO will take care of the translation for you. Also prevents the possibility of a SQL Injection attack....
-tg
Ok..geez. I have slapped myself. :D
However, I don't understand, given all of this, how the VALUES clause would work. You posted
VB Code:
sSQL = "INSERT INTO movieInfo (title, actor, producer, comment) VALUES (?,?,?,?)
objCommand.CommandText = sSQL
objCommand.CommandType = adCommandText
objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, txtTitle.Text)
objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, txtactor.Text)
objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, txtproducer.Text)
objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, txtComment.Text)
objCommand.Execute
Would I actually put question marks in the VALUES clause? :confused:
Re: [RESOLVED] SQL INSERT error
Yes, and its interpreted as placeholders for parameters... the values of which are provided by the parameters collection of the command object.
VB Code:
objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, [b]txtTitle.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, [b]txtactor.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, [b]txtproducer.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, [b]txtComment.Text[/b])
The replacement/update of the values is done by FIFO (first parameter object provides data for the first ?)
After all parameters have values then the query executes without an error.
Re: [RESOLVED] SQL INSERT error
Quote:
Originally Posted by leinad31
Yes, and its interpreted as placeholders for parameters... the values of which are provided by the parameters collection of the command object.
VB Code:
objCommand.Parameters.Add objCommand.CreateParameter("title",advarchar,50, [b]txtTitle.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("actor",advarchar,50, [b]txtactor.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("producer",advarchar,50, [b]txtproducer.Text[/b])
objCommand.Parameters.Add objCommand.CreateParameter("comment",advarchar,50, [b]txtComment.Text[/b])
The replacement/update of the values is done by FIFO (first parameter object provides data for the first ?)
After all parameters have values then the query executes without an error.
Ok...cool. I think I'm actually starting to get this (and like it :) )
So, I would use advarchar for text. What would I use to insert values into a int field?
Also, is this something that is confined to INSERTS or would I do the same thing with UPDATES?
Re: [RESOLVED] SQL INSERT error
adinteger .... when you press the , to move from the parameter name to the data type, you *should* be getting intellisense that will give you a list of the ad datatypes....
you can do this on inserts, deletes, updates, and even selects....
-tg
Re: [RESOLVED] SQL INSERT error
Quote:
Originally Posted by techgnome
adinteger .... when you press the , to move from the parameter name to the data type, you *should* be getting intellisense that will give you a list of the ad datatypes....
you can do this on inserts, deletes, updates, and even selects....
-tg
Cool.
Is it possible for a thread to be closed twice by two different people? :D
Thanks techgnome and leinad31. I learned a lot from this.
And, thank you osemollie for asking the question in the first place. :bigyello: