Hello I have an issue with this function recup(). The purpose of recup() is to insert all of the words found in the string chaine, word by word, into a database. The words come from chaine which is set by the user using a fgets. The problem I am facing is that it is only inserting words that have the same length as the first word of chaine.
For example, if I write "I want a cat", only "I" and "a" will be inserted.
int recup(char *chaine){
char tab[30] ={0};
char delim[] = " ";
char *ptr = strtok(chaine,delim);
int rc =0;
char *sql ="INSERT INTO newTable SELECT * FROM dbTable where lemme =?";
sqlite3_stmt *stmt = 0;
rc = sqlite3_prepare_v2(db,sql,-1,&stmt,0);
do {
// Expected word is printed here
printf("%s\n",ptr);
strcpy(tab,ptr);
// For some reason, only words that match the length
// of the first word are being inserted
rc = sqlite3_bind_text(stmt,1,tab,-1,SQLITE_STATIC);
rc = sqlite3_step(stmt);
ptr = strtok(NULL,delim);
} while(ptr != NULL);
rc = sqlite3_finalize(stmt);
return 0;
}
#include <ansi_c.h>
#include <sqlite3.h>
#include "fdata.h"
sqlite3 *db;
int main(int argc, char* argv[]) {
int rc = 0;
char chaine[100] = {0};
rc = sqlite3_open("lexique.db",&db);
check(rc);
createNewT();
question(&chaine);
recup(&chaine);
sqlite3_close(db);
return 0;
}
rcreturn value. It is bad not to do it, and very bad to not even print it when things go wrong. And anyway, why do you use that strange SQL statement if you just want to insert words from the input text?SQLITE_TRANSIENTinstead ofSQLITE_STATICin your call tosqlite3_bind_text. Perhaps it's related to the problems you're experiencing.