Strange problem with string coming back from MySQL

You think you've found a bug? Please report it here.

Moderators: Lapo, Bax

Post Reply
NateDog
Posts: 52
Joined: 26 Jun 2006, 13:25

Strange problem with string coming back from MySQL

Post by NateDog »

I don't know if this is a problem in the jdbc driver or in SFS but we can recreate it quite easily on our systems here.

The problem is the string.split() method not functioning properly for some "|" delimeted strings if the strings come back from the database.

Example Code:

Code: Select all

	

var testStr = "34|1|1|1|22|32|4|1|1|15|1|10|27|1|1|1|1|9|1|1|1|38";
var sql = "UPDATE error_test SET val = '" + testStr + "' WHERE id = 1";
	application.db.executeCommand(sql);
	
	var sql = "SELECT * FROM error_test WHERE id = 1";
	var q = application.db.executeQuery(sql);
	var tempRow = q.get(0);
	var tStr = tempRow.getItem("val");
	trace("Str:" + tStr);
	var t = tStr.split("|");
	trace("length:" + t.length);
To run this code, make a database table called error_test with a primary key column "id" and a varchar(255) column "val"

insert 1 row with id=1

run the code snippet and note that the split function is returning an array of length 51 instead of the correct 22.

if you call the split function with a variable containing the same string it works. If you change the delimeter to anything other than "|" it works also.

unlucky for us, "|" is our standard delimeter :(

just in case it's in SFS and not jdbc drivers, thought I'd pass it along.

<edit>
forgot to mention we're using mySQL 5 and the beta 5.0 jdbc driver
</edit>
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I think that the String object you're getting is a java.lang.String not a native Actionscript String.

Try casting it to the correct type before you call the split() method.

Like this:

Code: Select all

var tStr = String( tempRow.getItem("val") );
Lapo
--
gotoAndPlay()
...addicted to flash games
NateDog
Posts: 52
Joined: 26 Jun 2006, 13:25

Post by NateDog »

Interesting. Is it consistent with the datatype it's returning? In other words, are all varchar db columns coming back as java strings always?

I'm not totally sure but I believe this problem isn't happening in all of our queries. Is there a benefit to ever having this come back as a java string vs. an actionscript string? Seems to definitely leave a big door open for programming errors because it isn't at all expected that we would have to cast the sql results to String() on all of the columns before using them.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

All datatypes returned by a query are Java datatypes, and you usually don't need to care about it.

In the particular case of the split() method there's however a difference in the expected behaviour.

In case you are unsure about what data type you're dealing with you can use this simple technique:

Code: Select all

trace( typeof(myVar) )
If the variable is a java object you will get an "object"
So if you expected a number or string and you get an "object" you're dealing with a Java object

To know what kind of object it is, use this:

Code: Select all

trace("type: " + myVar.getClass().getName())
It should display "java.lang.String" in the case of a Java String

Casting all the datatypes coming from java into their relative AS versions behind the scenes could cause a performance hit, especially on large data sets etc... We will try to extend our documentation to better indicate when and where this Java/Actionscript ambiguities may occur
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply