How to access data in ISFSArray result?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

How to access data in ISFSArray result?

Postby m0rr0ws0n » 29 Sep 2017, 11:26

Can I get some sample code for this? I want to check that each value returned from a select query is "nochar".

Code: Select all

ISFSArray result2 = dbManager.executeQuery(query, new Object[] {userName});
            
            trace(result2);
            
            if( result2.getText(0) == "nochar" && isAdded == false)
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char1 = ? WHERE Username = ?";
            }
            
            if( result2.getText(1).toString() == "nochar" && isAdded == false)
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char2 = ? WHERE Username = ?";
            }
            
            if( result2.getText(2).toString() == "nochar" && isAdded == false)
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char3 = ? WHERE Username = ?";
            }
            
            if( result2.getText(3).toString() == "nochar" && isAdded == false)
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char4 = ? WHERE Username = ?";
            }
            
            if( result2.getText(4).toString() == "nochar" && isAdded == false)
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char5 = ? WHERE Username = ?";
            }
            
            dbManager.executeUpdate(query, new Object[] {charName, userName});
User avatar
Lapo
Site Admin
Posts: 20048
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to access data in ISFSArray result?

Postby Lapo » 29 Sep 2017, 13:32

Hi,
I am not sure what you're asking for. You've already posted a code example.
What are you trying to do exactly?

Is something not working? Please clarify.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: How to access data in ISFSArray result?

Postby m0rr0ws0n » 29 Sep 2017, 19:23

Lapo wrote:Hi,
I am not sure what you're asking for. You've already posted a code example.
What are you trying to do exactly?

Is something not working? Please clarify.

Thanks


It gives me this exception:

Exception: java.lang.ClassCastException
Message: com.smartfoxserver.v2.entities.data.SFSObject cannot be cast to java.lang.String
Description: Error while handling client request in extension: { Ext: WarExtension, Type: JAVA, Lev: ZONE, { Zone: WarCatalyst }, {} }
Extension Cmd: CharCreate
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.entities.data.SFSArray.getUtfString(SFSArray.java:305)
com.smartfoxserver.v2.entities.data.SFSArray.getText(SFSArray.java:311)
war.CreateCharHandler.handleClientRequest(CreateCharHandler.java:80)
com.smartfoxserver.v2.extensions.SFSExtension.handleClientRequest(SFSExtension.java:208)
com.smartfoxserver.v2.controllers.v290.ExtensionReqController.processRequest(ExtensionReqController.java:174)
com.smartfoxserver.v2.controllers.v290.ExtensionReqController$1.run(ExtensionReqController.java:68)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

on this line

Code: Select all

if( result2.getText(0) == "nochar" && isAdded == false)
User avatar
Lapo
Site Admin
Posts: 20048
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to access data in ISFSArray result?

Postby Lapo » 30 Sep 2017, 07:27

Thanks.
The SFSArray contains a number of SFSObjects. Each SFSObject contains a row of result from the database query.

The problem here is that you're trying to access an element of the array as a String while they are all SFSObjects.

You can learn all the details here:
http://smartfoxserver.com/blog/querying ... esultsets/

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: How to access data in ISFSArray result?

Postby m0rr0ws0n » 03 Oct 2017, 02:43

It's pretty weird this code doesn't work

Code: Select all

if( (result2.getSFSObject(0).getUtfString("char1") == "nochar") && (isAdded == false))
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char1 = ? WHERE Username = ?";
            }
            
            if( (result2.getSFSObject(0).getUtfString("char2")  == "nochar") && (isAdded == false))
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char2 = ? WHERE Username = ?";
            }
            
            if( (result2.getSFSObject(0).getUtfString("char3")  == "nochar") && (isAdded == false))
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char3 = ? WHERE Username = ?";
            }
            
            if( (result2.getSFSObject(0).getUtfString("char4")  == "nochar") && (isAdded == false))
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char4 = ? WHERE Username = ?";
            }
            
            if( (result2.getSFSObject(0).getUtfString("char5")  == "nochar") && (isAdded == false))
            {
               isAdded = true;
               query = "UPDATE tbl_charselect SET char5 = ? WHERE Username = ?";
            }


If I trace result2.getSFSObject(0).getUtfString("char2"), it shows the correct value and so do all the other ones. My if statements aren't working though. isAdded is false by the end this set of loops when it shouldn't be meaning its not going into the body of one of the if statements. char2, char3, char4, char5 are "nochar" that should trigger it to change the query and set isAdded to true but it's not going into any of them. :shock:

The first string has a name the other 4 don't. The other 4 are "nochar". isAdded is false at the beginning of the block. Why is this not working?
User avatar
Lapo
Site Admin
Posts: 20048
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to access data in ISFSArray result?

Postby Lapo » 03 Oct 2017, 08:02

I think the best idea is to check the structure of the SFSArray.
You can dump the structure of an SFSObject/Array like this:

Code: Select all

trace(result2.getDump());

This will show all the content of the object in a tree-like form.
Lapo

--

gotoAndPlay()

...addicted to flash games
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: How to access data in ISFSArray result?

Postby m0rr0ws0n » 03 Oct 2017, 15:49

Ahh I fixed it. You have to use the equals() String method. equals() and == are two different things in Java.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests