Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have string like that


SQL
update std set name=test,phone=8787898 where id = 10

i want to split it into tokens using space and =, delmiters i'm using the follwoing code

the probleme is that i can not select where token the pointer at that stage is NULL
how to make it point to where

C++
pch=strtok(str," ");

while(pch !=NULL)
{

   if(strcmpi("update",pch)==0)// check update 
    {
       pch=strtok(NULL," ");
       if(strcmpi("std",pch)==0)
          {
            pch=strtok(NULL," ");
            if(strcmpi("set",pch)==0)
             {
               pch=strtok(NULL," ");//pointer(pch) holds name=test,phone=8787898
               pch= strtok(pch,"=,");//pointer holds name
               if(strcmpi("name",pch)==0)
                 {
                    pch=strtok(NULL,"=,");//pointer holds test
                    pch=strtok(NULL,"=,");//pointer holds phone
                  }
               if(strcmpi("phone",pch)==0)
                  {
                    pch=strtok(NULL,"=,");//pointer holds 8787898 
                    pch=strtok(NULL,"=,");//pointer holds NULL
                   }
                pch=strtok(NULL," ");//pointer holds NULL
                if(strcmpi("where",pch)==0)
                 {
                    printf("where selected");
                  }
              }
           }
    }
}
Posted
Updated 9-Nov-14 14:49pm
v2

quite frankly, I think strtok approach is a little simplistic for something like a sql statement

you'll see Ive put a code tag for sql around your statement - interesting, it highlights the sql keywords update, set, and where ... if I wanted a 'simpler' approach, I would 'split up the string into sections', each section being the update, set and where 'clauses', and then maybe strtok for the pieces (but that may still be too simplistic)

are you open to using any other tools, or do you have to do this the hard way ?
 
Share this answer
 
Comments
TheSniper105 9-Nov-14 21:29pm    
other tools like what ??
Garth J Lancaster 9-Nov-14 21:43pm    
ok apart from a hybrid method of what you started with, but using sscanf to get the positions of the keywords, you could take a look at http://www.hwaci.com/sw/lemon/ and have a look at the SQLite source code - they use their Lemon parser to parse SQLite's sql
TheSniper105 9-Nov-14 23:56pm    
i prefer hard way as i' not actually run sql statments
Hi,
I tried strtok with all delims listed and all tokens where separated into a list.


C#
void P(char *p)
{
    printf("%s\n", p);
}
int main()
{
    char str[]="update std set name=test,phone=8787898 where id = 10";
    char *pch=strtok(str," =,");
    P(pch);
    while(pch)
    {
        pch = strtok (NULL, " =,");
        if(pch != NULL)
          P(pch);
    }

    return 0;
}
 
Share this answer
 
Comments
TheSniper105 9-Nov-14 23:55pm    
can you print them all ??
BacchusBeale 11-Nov-14 19:27pm    
That's what the code does. strtok looks for any delim in the list " =," and returns next token on each call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900