Hi Guys,

I am trying to read each line from a file into a Sybase database table.

I'm trying to implement string tokenization so i can format each field correctly because i was getting datatype violations :S

The code I have is: -

if ((infile=fopen("datafile","r")) == NULL)
        {
                printf("Unable to open file 'datafile'.\n");
                exit(ERREXIT);
        }

        printf("Inserting rows into the 'my_table' table.\n");


        while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
        {
                printf("%s -> ", cmdbuf);
                char *token = strtok(cmdbuf, ",");
                while (token != NULL) {
                        ret = dbfcmd(dbproc,"insert into my_table \n");
                        printf("%s ", (ret == SUCCEED) ? "OK" : "NOK");
                        ret = dbfcmd(dbproc,"values(%s, %s, %s, %s, %f, %d, %f, %d) \n",cmdbuf);
                }
                printf("%s\n", (ret == SUCCEED) ? "OK" : "NOK");
                ret = dbsqlexec(dbproc);
        }

I've tried to use string tokenization. The output i get is as follows: -

$ ./inserts
inserting data

Inserting rows into the 'my_table' table.
20060714,ZZ,041510,QP,0.0000,0,15.8100,4
Memory fault

Any ideas anybody please??? :-((

Recommended Answers

All 4 Replies

Hi,

I just posted earlier but realised that the thread was closed, so here goes again!

Hi Guys,

I am trying to read each line from a file into a Sybase database table.

I'm trying to implement string tokenization so i can format each field correctly because i was getting datatype violations

The code I have is: -

if ((infile=fopen("datafile","r")) == NULL)
{
printf("Unable to open file 'datafile'.\n");
exit(ERREXIT);
}

printf("Inserting rows into the 'my_table' table.\n");


while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
{
printf("%s -> ", cmdbuf);
char *token = strtok(cmdbuf, ",");
while (token != NULL) {
ret = dbfcmd(dbproc,"insert into my_table \n");
printf("%s ", (ret == SUCCEED) ? "OK" : "NOK");
ret = dbfcmd(dbproc,"values(%s, %s, %s, %s, %f, %d, %f, %d) \n",cmdbuf);
}
printf("%s\n", (ret == SUCCEED) ? "OK" : "NOK");
ret = dbsqlexec(dbproc);
}

I've tried to use string tokenization. The output i get is as follows: -

$ ./inserts
inserting data

Inserting rows into the 'my_table' table.
20060714,ZZ,041510,QP,0.0000,0,15.8100,4
Memory fault

Any ideas anybody please??? :-((

<email snipped>

Thanks in advance!

why don't you format the string and then send it to sybase. The code below won't be 100% correct because it does not account for the floats and integers. The main thing is that you have to surround all strings with quotes, and nothing for floats/integers. You can not pass the strings to sybase without the quotes or sybase will puke up all over you.

char command[1024];
strcpy(command,"insert into my_table values( ");
while (token != NULL) 
{
    strcat(command, "'"); // surround text with single quotes
    strcat(command,token);
    strcat(command,"', '"); 
    token = strtok(NULL, ",");
}

Now the resulting string in the above will be terminated with one too many commas, such you will have to strip it back off

command[strlen(command)-1] = 0; 
ret = dbfcmd(dbproc,command);
if(ret == SQL_SUCCESS)
{
    ret = dbsqlexec(dbproc);
}

you cant keep using strtok like that

the first use of strtok must call out the buffer as you did: my_ptr = strtok(buf,","); but each subsequent use of strtok on this file handle must use NULL instead of the buffer name. the buffer name is kept track internally. recalling it will screw it up. my_ptr = strtok(NULL, ","); see here: http://www.cplusplus.com/reference/clibrary/cstring/strtok.html

digiman: I merged your two threads because they asked the same question.

>>I just posted earlier but realised that the thread was closed
I don't think so. It didn't look closed to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.