logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
LIKE operator and '%'

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
Fiz



Joined: 13 Nov 2010
Posts: 5

PostPosted: Sat Nov 13, 2010 19:39    Post subject: LIKE operator and '%' Reply with quote

Hi, I'm quite new to NWNX2 and I just begun to build my own SQLite DB.

Everything works fine, except for one thing: I can't make 'LIKE' statements because for some reason if I write '%' in the SQL string to be executed, I see in odbc log that it is converted into '~'.

Here's an example:

SCRIPT COMMAND:
sSql = "SELECT quantita FROM ed_disp WHERE tag = '" + sTag + "' AND resref LIKE '" + sRes + "%'";

LOG MESSAGE:
o Got request: SELECT quantita FROM ed_disp WHERE tag = 'DagAlc' AND resref LIKE 'ristsup~'

Could you explain what I'm doing wrong? Sad
Back to top
View user's profile Send private message
Fiz



Joined: 13 Nov 2010
Posts: 5

PostPosted: Tue Nov 16, 2010 20:50    Post subject: Reply with quote

Anyone knows how to help me, please? Sad
Back to top
View user's profile Send private message
Squatting Monk



Joined: 28 Jun 2007
Posts: 76

PostPosted: Sat Nov 20, 2010 6:25    Post subject: Reply with quote

I can't see what you're doing wrong. I've used the wildcard with LIKE before with no problems, and it looks like your quotation marks work out fine. What sort of datatype are you getting? I know % is used by NWNX when retrieving object data from a text or BLOB field, so that could cause conflicts. Is that what you're doing?
Back to top
View user's profile Send private message
Fiz



Joined: 13 Nov 2010
Posts: 5

PostPosted: Sat Nov 20, 2010 14:28    Post subject: Reply with quote

Thank you for the answer, here is the code where I use the LIKE statement:

Code:
 
    int iQty;
    int iResult;
    int iPrice
    string sTag = GetTag(oMerc);
    string sRes = GetResRef(oProd);
    string sSql;

    sSql = "SELECT price FROM ed_main WHERE resref = '" + sRes + "'";

    SQLExecDirect(sSql);

    iResult = SQLFetch();

    if (iResult == SQL_SUCCESS)
    {
        iPrice = StringToInt(SQLGetData(1));

        sSql = "SELECT quantity FROM ed_disp WHERE tag = '" + sTag + "' AND resref LIKE '" + sRes + "%'";

        SQLExecDirect(sSql);

        iResult = SQLFetch();

        if (iResult == SQL_SUCCESS)
        {
            iQty = StringToInt(SQLGetData(1));
        }
        else
        {
            iQty = 0;
        }
    }
Back to top
View user's profile Send private message
Fireboar



Joined: 17 Feb 2008
Posts: 323

PostPosted: Sun Nov 21, 2010 16:59    Post subject: Reply with quote

What does your SQLExecDirect look like?
Back to top
View user's profile Send private message
Fiz



Joined: 13 Nov 2010
Posts: 5

PostPosted: Mon Nov 22, 2010 1:53    Post subject: Reply with quote

Is the 'standard' one, directly from aps_include:

Code:

void SQLExecDirect(string sSQL)
{
    SetLocalString(GetModule(), "NWNX!ODBC!EXEC", sSQL);
}


It doesn't seem to convert the string in any way at this point... But I don't know how it works after setting the local string.

Maybe you could give me some example that works, so I could try it in my system? I really don't know what to do, except maybe thinking about some strange arrangement using something like FindSubString outside the SQL code... Confused
Back to top
View user's profile Send private message
Gryphyn



Joined: 20 Jan 2005
Posts: 431

PostPosted: Thu Nov 25, 2010 9:40    Post subject: Reply with quote

It's a result of a Token replacement, that is part of the SCO/RCO (object) functionality, and 'safe' string encoding.

'%s' is used as C string placeholder. Any % that is not part of the SCO/RCO code will be replaced with a ~ character.

Basically, the LIKE syntax has been kiboshed.

Cheers
Gryphyn
Back to top
View user's profile Send private message
Fiz



Joined: 13 Nov 2010
Posts: 5

PostPosted: Fri Nov 26, 2010 2:20    Post subject: Reply with quote

Ok, thank you for the answer. I'll try something else Wink
Back to top
View user's profile Send private message
eeriegeek



Joined: 07 Jan 2008
Posts: 59

PostPosted: Thu Dec 16, 2010 3:15    Post subject: Reply with quote

I just tried several simple varieties of like with % using both the windows stable (0.9.2.4) release and a Nov 1 svn trunc build for linux (0.3.2) and both seemed to work fine using SQLExecDirect and both return the expected matching rows. I wonder what the difference is?
Back to top
View user's profile Send private message
JustVoodoo



Joined: 17 Jan 2012
Posts: 4

PostPosted: Tue Jan 17, 2012 15:07    Post subject: Reply with quote

Gryphyn wrote:
It's a result of a Token replacement, that is part of the SCO/RCO (object) functionality, and 'safe' string encoding.

'%s' is used as C string placeholder. Any % that is not part of the SCO/RCO code will be replaced with a ~ character.


Is there a way to avoid or "fix" it?

My problem is, that i want to compare two datetimes. Its not enough to know if time1 is greater then time2, I need the exact value of seconds(or minutes at least) between them. The only way I know is to use "strftime('%s','xxxx') - strftime('%s','now')". But if i cant use %s, i dont know what a can do.

Any function that will convert a datetime to int would be fine as well.

Thx for help
Back to top
View user's profile Send private message
eeriegeek



Joined: 07 Jan 2008
Posts: 59

PostPosted: Tue Jan 17, 2012 16:50    Post subject: Reply with quote

JustVoodoo, time functions are very database dependent, what DB are you using? Under MySQL I can use something like timestampdiff(SECOND,last_login,now()) to get seconds since last login.
Back to top
View user's profile Send private message
JustVoodoo



Joined: 17 Jan 2012
Posts: 4

PostPosted: Tue Jan 17, 2012 17:09    Post subject: Reply with quote

Ok,

i've made my own conversation from date to int.

Code:

int DBTimestamp(string sTime="now"){
    if(sTime == "now")
        sTime = Datetime();

    return     (GetDateTimeByPosition(1, sTime)-2012)* 33177600 //-2012 to keep number small(er)
              +(GetDateTimeByPosition(1, sTime)-   1)*  2.678.400
              +(GetDateTimeByPosition(3, sTime)-   1)*    86.400
              + GetDateTimeByPosition(4, sTime)      *     3.600
              + GetDateTimeByPosition(5, sTime)      *       60
              + GetDateTimeByPosition(6, sTime)      *        1;
}


Basically its simple. I convert everything is seconds, but I don't know if there will be some problems with months, since the seconds a month has depends on the month. I took for a every month 31 Days (30 Days + 23h + 59 min + 59 sec). and a year = 12*32 Days. So I secured that the 31.xx. 23:59:59 can't have a greater value then the 01.xx+1. 00:00:00 and the same with the year.

This idea is based on another method to convert a date to int:

2012-06-06 20:00:00 to
20120606200000, basically deleting all spaces and symbols other then digits. unfortunately that number has 14 digits and i doesn't fit into an integer.
The method to convert the date is
year * 10.000.000.000
+ month* 100.000.000
+ day * 1.000.000
...

But it is not necessary that the numbers are so great. The greatest Value for the maximum number of days, hours,minutes and seconds (31d + 23h + 60m + 60m) has to be smaller then one month. So increasing the month will result in a greater value then all other dates before that date.

The only issue may be that i can't convert the int back to a date, but I don't need that (by now Wink)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group