1

i write a function for find table name in sql query string.

my code

char* SQLParser_GetTable(char *query)
{
char *str = "";
char *FROM="FROM";    

if(strstr(query, FROM))
{
    char *e;
    int index;      
    e = strchr(query, 'F');
    index = (int)(e - FROM);

    str=substring(str,index+4,5);
}

return  str;
}

main.c

     query = "SELECT * FROM TABLE1";
     char *tbl=SQLParser_GetTable(query);

but this code retun full string not table name.

MY code must return "TABLE1".

1
  • 1
    What are you expecting the following two lines to do: e = strchr(query, 'F'); index = (int)(e - FROM);? Commented Jan 1, 2015 at 12:35

2 Answers 2

1

I don't know what substring() is but this code does what you think yours does

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *SQLParser_GetTable(char *query)
{
    char *str;
    char *FROM = "FROM ";

    /* point to the start of FORM */
    if ((str = strstr(query, FROM)) == NULL)
        return NULL;
    /* If there is only one space between FROM and TABLE1 point to it */
    str = strchr(str, ' ');
    if (str == NULL)
        return NULL;
    /* move past the ' ' character */
    str += 1;
    /* return a copy of the string */
    return strdup(str);
}

int main ()
{
    char *table = SQLParser_GetTable("SELECT * FROM TABLE1");
    if (table != NULL)
    {
        printf("%s\n", table);
        free(table);
    }
    return 0;
}

notice that this is not robust at all since there can be any number of whitespaces between FROM and the table name.

Sign up to request clarification or add additional context in comments.

2 Comments

how to can get columns and where condition .ex: select col1,col2 from table1 where col1=1 ? and save in array or linked list ? can i contact to me tehhitech [at] live .com . i have many project to you now.
I wouldn't recommend that, you should use a parser generator. see bison and flex otherwise you should use a state machine and write a tokenizer, since sql sentences can have any number of whitespaces and there are many other situations to handle too. You need to write a lexer and specify a grammar, that can be done with the programs in the links, not in that order. And sure, [email protected]
0

I have written the following long function but compared with functions in other answers it is the only one that is more or less correct.:). The function does not take into account the case of letters.:)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char * SQLParser_GetTable( const char *query )
{
    const char FROM[] = "FROM";    
    char *p;
    int found;

    found = ( p = strstr( query, FROM ) ) != NULL;
    found = found && ( p == query || isblank( ( unsigned char )p[-1] ) );
    found = found && ( *( p += sizeof( FROM ) - 1 ) == '\0' ||
                       isblank( ( unsigned char )p[0] ) );

    if ( found )
    {
        const char *q = p;
        size_t n = 0;

        while ( isblank( ( unsigned char )*q ) ) ++q;
        while ( q[n] && !isblank( ( unsigned char )q[n] ) ) ++n;

        p = malloc( ( n + 1 ) * sizeof( char ) );

        memcpy( p, q, n );
        p[n] = '\0';
    }
    else
    {
        p = NULL;
    }

    return p;
}

int main(void) 
{
    char *query = "SELECT * FROM TABLE1";
    char *p = SQLParser_GetTable( query );

    if ( p ) puts( p );

    free( p );

    return 0;
}

The output is

TABLE1

4 Comments

This is a good approach, though your code is not very readable and you should use isspace instead, because in SQL newlines are also ignored. +1.
@iharob Only now I have found that the code contains a defect. For example a SQL statement can contain more than one "FROM" characters and the first one that will be found is not necessary the right "FROM".:) So the search should be continued until there is no any "FROM".
And there are many more issues, that's why I recommended a parser generator.
than you Vlad from moscow about better answer .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.