|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Fri Mar 04, 2005 6:18 pm
including a linefeed char in an Sql statement |
Hi again :)
You all have been so very helpful, I have returned yet again with another question. I am building Sql statements inside zMUD and then passing them on to my database via an ADO object.
This is all fine and good until I wish to, say insert a new record that contains text fields that might include the ascii char for a linefeed (character 10 of the ascii set). When I do this it "breaks" the syntax of the statement the same as if I had actually hit return on my keyboard at the point of the linefeed character.
Anyone know how to escape this character so that it won't break SQL syntax while being passed to the database?
Thanks! |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Fri Mar 04, 2005 7:00 pm |
use the %char command
%char(10) |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Fri Mar 04, 2005 7:28 pm |
I do use the %char(10) command to insert linefeed characters into the SQL statement as I build it -- or more specifically I use %char() to build a variable of a field which in turn is used to build the SQL statement. But, as I said when it comes time to pass on the @Sql variable to the ADO object things go bad.
|
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Fri Mar 04, 2005 7:43 pm |
show us some code and examples,also this may be a case for the %expand function
|
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Fri Mar 04, 2005 8:02 pm |
@object_desc contains:
Under a simple hard brown crust, this roll has a moist, chewy centre filled with#flecks of fruits and vegetables.
*note the "#" above actually is my silly representation for the linefeed character. inside my zmud trigger editor screen widget, it looks like an empty square.
Later, I insert this variable into the much longer @Sql variable. Once the @Sql variable is built, I create a new record in the database.
Code: |
...
Sql = %concat( @Sql, ~'@object_desc~'~,)
...
Sql = %string( @Sql)
#CLASS 0
#VAR ConnStr %comcreate( "ADODB.Connection") "" "Object Database"
#CALL @ConnStr.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\zMUD\TorilMUD\Toril_db.mdb")
#VAR rs @ConnStr.Execute( @Sql) "" "Object Database" |
|
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Fri Mar 04, 2005 9:38 pm |
You were a little stingy with the code so I guessed
Code: |
#CLASS {Test}
#VAR conn {<OLE object: ADODB.Connection>} {}
#ALIAS doinsert {#VAR Sql {INSERT INTO TheTable (TheField) VALUES('}
#VAR Sql %concat(@Sql,"@object_desc')")
#call @conn.Execute(@Sql)}
#VAR SQL {INSERT INTO TheTable (TheField) VALUES('@object_desc')}
#VAR object_desc {Troubleshooting: If you have trouble connecting to a MUD and get the error message "can’t lookup address",
then you probably have an incorrect nameserver setup. }
#CLASS 0 |
my variable object_desc has embedded newlines, double quotes " ,single quotes ' and it goes in no problem;
the SQL variable that I send to the Execute method looks like this
INSERT INTO TheTable (TheField) VALUES('@object_desc')
and really newline characters shouldnt cause a problem syntax wise when
embedded in quote strings.
Anyways if I'm way off, maybe someone else has a better way,
also you could try posting more of the code and maybe show what the final SQL string looks like,what you want it to do, also what exactly is the error your getting.
you're unlikely to give too much info, and the more info we have
the easier it is to help diagnose the problem and or suggest a easier way to get done, what you have in mind. |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Sat Mar 05, 2005 12:59 am |
Hrm. I ran your code on my machine, and it behaves exactly as you say, but that's not the behavior I want. Yes the SQL variable that is sent to the execute method looks like you say, to whit:
INSERT INTO TheTable (TheField) VALUES('@object_desc')
BUT, the behavior I desire is not that the literal "@object_desc" is pasted inside the SQL, but that the CONTENTS of @object_string are pasted inside the SQL variable such that the final product looks like:
INSERT INTO TheTable (TheField) VALUES('{Troubleshooting: If you have trouble connecting to a MUD and get the error message "can’t lookup address",
then you probably have an incorrect nameserver setup. }')
Of course, with the above, perhaps you can see why the newline character is causing a problem: it's not see the closing single quote and parentheses because those exist on a newline. The specific error that I get is:
Error parsing command:
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE' |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Sat Mar 05, 2005 1:05 am |
Furthermore, if I make the equivalent change to my code that you have in yours (that is, surround the @object_desc with quotes):
Code: |
...
Sql = %concat( @Sql, ~'"@object_desc"~'~,)
...
Sql = %string( @Sql)
#CLASS 0
#VAR ConnStr %comcreate( "ADODB.Connection") "" "Object Database"
#CALL @ConnStr.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\zMUD\TorilMUD\Toril_db.mdb")
#VAR rs @ConnStr.Execute( @Sql) "" "Object Database" |
Then, sure, the insert is carried out successfully but the Description field is populated by the value: "@object_desc"
Not exactly what I want. :P |
|
|
|
nexela Wizard
Joined: 15 Jan 2002 Posts: 1644 Location: USA
|
Posted: Sat Mar 05, 2005 2:31 am |
Well first things first if there is a ' in the string it needs to be quoted out for valid SQL, second you prolly want CR (carriage return %char(13)) and not LF and are you saying the @OBJ desc already has newlines in it? That you don't want the newlines? or what Im still not sure what your trying to do.
IF @obj_desc looks like this in the settings editor
Obj_desc=
This is line one
This is line two mm'kay
Then you need this
#VAR sql {INSERT INTO `table` (`field`) VALUES ('%replace(@obj_desc,"'","''")')}
However if it looks like this
obj_desc=
This is line one;this is line two
then you need this
#VAR sql {INSERT INTO `table` (`field`) VALUES ('%replace(%replace(@obj_desc,"'","''"),";",%char(13))')}
Until I know exactly what your trying to do I can't really help you and its hard to tell from your post :p
Ok I think I might understand what your getting at but prolly not......
//For breaks
#VAR sql {INSERT INTO `table` (`field`) VALUES ('%replace(%replace(@obj_desc,"'","''"),"%char(10)",%char(13))')}
//remove LFs
#VAR sql {INSERT INTO `table` (`field`) VALUES ('%replace(%replace(@obj_desc,"'","''"),"%char(10)"," ")')} |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Sat Mar 05, 2005 3:02 am |
for me the value of @object_desc is passed into the field
its obvious your building your strings differently from mine.
also the error you're getting says nothing about newlines to indicate
that newlines are the source of your woes.
Quote: |
Error parsing command:
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE' |
why dont you show us exactly how you're putting your sql strings together , all of it
You may not think its important but I suspect it is, and really what reason
is there not to show it.
the code you gave above mostly shows how you're opening the Connection, and your call to the Execute function which doesnt seem
to be where the problem is. Its seems the SQL string thats the heart of this,and subtle differences in your code, can have big consequences.
Like I said before, you're unlikely to give too much information
so dont hold back post as much of your code as possible.
ps what does the SQL variable hold when you get the Invalid SQL statement error |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Sat Mar 05, 2005 3:06 am |
Hi nexela. I do preface each single quote with another single quote already. I realize you can't tell that from the code posted, but when I build the @object_desc from a trigger I use the %replace to accomplish this.
I tried using %char(13) instead of %char(10) as you suggested with the same results.
I don't understand what part of my explanation is unclear, frankly. Try thinking of it this way, it seems to be as if I was literally typing in the SQL statement at the keyboard to send to the execute method. The execute method won't parse my input until I hit "enter" on my keyboard, but as soon as I do, then (amongst many other things) it checks for correct syntax. My problem is that as part of my input string I need to send a carriage return/linefeed/whatever and so this prematurely causes me to "hit enter" and thus the execute method parses only the part of the SQL statement that preceeds the %char(13) or %char(10).
Is that any clearer? |
|
|
|
nexela Wizard
Joined: 15 Jan 2002 Posts: 1644 Location: USA
|
Posted: Sat Mar 05, 2005 3:41 am |
I was able to pass multi lined Variables into a Mysql text field without a problem and without it breaking prematurly. As dharkeal suggested try posting the complete script and we can see where you are going wrong.
|
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Sat Mar 05, 2005 4:18 am |
Woohooooo!! Success! Thank you very much, Darkhael, your suggestions/code got me thinking and I retried your idea with success this time. Here is what I did.
Old code:
Code: |
...
Sql = %concat( @Sql, ~'@object_desc~'~,)
...
Sql = %string( @Sql)
#CLASS 0
#VAR ConnStr %comcreate( "ADODB.Connection") "" "Object Database"
#CALL @ConnStr.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\zMUD\TorilMUD\Toril_db.mdb")
#VAR rs @ConnStr.Execute( @Sql) "" "Object Database" |
NEW code:
Code: |
...
Sql = %concat( @Sql, "'@object_desc',")
...
;Sql = %string( @Sql)
#CLASS 0
#VAR ConnStr %comcreate( "ADODB.Connection") "" "Object Database"
#CALL @ConnStr.Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\zMUD\TorilMUD\Toril_db.mdb")
#VAR rs @ConnStr.Execute( @Sql) "" "Object Database" |
as you can see, I double-quoted the @object_desc var and commented out the %string() which was adding double quotes around the entire, completed sql statement. This fixed the problem. the contents of @object_desc are now successfully passed to the execute method (special chars and all) :)
Sorry, if I seemed reluctant to post more code. I just didn't think it was relevant (and there is lots and lots and LOTS of very similar concat statements as I add each field) So I was aiming for brevity. Anyhow, looks like what I did post was actually where the problem was! :)
Soooo, all this brings up a follow-up question: how can I append a line of text to the conent of a variable such that the new line of text is added on a new line inside the variable?
Example, suppose I am parsing this output from a mud:
This is the first line of text.
This is the second!
Now I have a variable called @output where I store this info. After I read the first line of text @output would like like:
This is the first line of text.
After I read the second line of text I want it to look like:
This is the first line of text.
This is the second!
The problem with using %char(13) is that inside the var the %char(13) character looks different, it looks like an empty square (because the computer is trying to represent an ascii char for which their isnt really one). So how do I literally force the cursor onto the next line of the @output var before writing in a new line of text rather than inserting a special character that means "I represent a carriage return"? |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Sat Mar 05, 2005 4:45 am |
I'm pleased it worked out for you.
[sermon]As brevity really isnt a virtue in situations like this
even if you think you know what the problem is, you may be wrong
if you post all your code and its irrelevant, nothings hurt by it,
but otherwise it means a lot of people get to examine the code
and maybe catch, something you and most others may miss.
cutting and pasting code isnt really labour intensive
and often results in faster resolution.
[/sermon]
sorry I had to!
Anyways in answer to your follow up:
Code: |
#va Text %concat("This is the first line of text",%crlf)
#va Text %concat(@Text,"This is the second") |
of course you could use %cr depending on what you're after |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
tessellated Beginner
Joined: 19 Feb 2005 Posts: 12
|
Posted: Sat Mar 05, 2005 4:51 am |
Thanks, again Dharkael. You rock. And, yeah, generally speaking I'm in full agreement with your sermon so, worry not, it doesn't fall on deaf ears. Had I not been able to solve the problem I would have spammed you with gajillions of lines of concat statements :P
|
|
|
|
|
|