Sqlite3 en C :: resultat de "select ..."

Résolu
goldziko9 Messages postés 39 Date d'inscription lundi 3 janvier 2005 Statut Membre Dernière intervention 5 avril 2010 - 14 nov. 2008 à 14:42
goldziko9 Messages postés 39 Date d'inscription lundi 3 janvier 2005 Statut Membre Dernière intervention 5 avril 2010 - 15 nov. 2008 à 12:19
Bonjours a tous .

      mon probleme et le suivant : 
            j'ai fait un exe de test pour manipuler une base de donner Sqlite3 et voici le code :

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





struct line
{
 char *valeur1;
 char *valeur2;
};



static struct line list_db[100];
static int count_line = 0;



static int callback(void *output, int argc, char **argv, char **azColName)
{
 //printf(" |-------> %s, %s \n", argv[0] ? argv[0] : "Rien", argv[1] ? argv[1] : "Rien");
 list_db[count_line].valeur1 = argv[0];
 list_db[count_line].valeur2 = argv[1];
 count_line += 1;



 return 0;
}



struct info_perso
{
 char nom[30];
 char prenom[30];
 int y_birth;
};



struct info_perso get_info(void)
{
 struct info_perso perso_info;
 int i;
 printf("Votre nom : "); scanf("%s",&perso_info.nom);
 printf("Votre prenom : "); scanf("%s",&perso_info.prenom);
 printf("Votre annee de naissance : "); scanf("%d", &perso_info.y_birth);
 for (i = 1; i<=20; i++)
  printf(" ");
 for (i = 20; i<=60; i++)
  printf("_");
 printf("\n");
 return perso_info;
}



void list_data(sqlite3 *sqlite)
{
 char *zErrMsg;
 sqlite3_exec(sqlite, "select nom,prenom from perso", callback, 0, &zErrMsg);
 printf("En total : %d.\n", count_line);
 for(int i=0; i<=count_line-1; i++)
 {
   printf("%d :: %s, %s \n", i, list_db[i].valeur1, list_db[i].valeur2);
 }
}





int main(int argc, char ** args)
{
 sqlite3 *db;
 int rc;
 char *zErrMsg;
 struct info_perso moi;
 char sql[256];
 



 #ifdef _WIN32
 system("cls");
 #else
 system("clear");
 #endif
 for (int i = 0; i<=79; i++)
  printf("_");





 moi = get_info();



 rc = sqlite3_open("sqlite_test.db", &db);
 if(rc) { printf("Impossible d'ouvrir ou de cree la table !!\n"); return 0; }
 
 if(sqlite3_exec(db, "create table if not exists perso(nom varchar(20), prenom varchar(20));", callback, 0, &zErrMsg))
  printf("Message de retour : %s\n",zErrMsg);



 strcpy(sql,"INSERT INTO perso(nom,prenom) VALUES ('");
 strncat(sql, moi.nom,strlen(moi.nom));
 strncat(sql, "','", 4);
 strncat(sql, moi.prenom, strlen(moi.prenom));
 strncat(sql, "');", strlen("');"));



 printf("sql :%s\n", sql);



 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
 if(rc) { printf("Message de retour : %s\n",zErrMsg); return 0; }



 list_data(db);



 sqlite3_close(db);
 printf("\n\n\rla vie est belle.\n");



 for (int h = 1; h<=80; h++)
  printf("_");
 printf("\n");
 return 0;
}

Le compilateur compile avec success , sauf que le tableau de structure list_db ne contien que la dernier valeur retourné par la SQL : select ... .

si vous avez des remarque sur le code ou une idee, je suis preneur .







ok that's good !!

2 réponses

goldziko9 Messages postés 39 Date d'inscription lundi 3 janvier 2005 Statut Membre Dernière intervention 5 avril 2010
15 nov. 2008 à 12:19
Hello every body !!


      Bon j'ai trouvé une autre solution pour resoudre de problem voici le code :

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



int main(int argc, char **argv)
{
 printf("like an eagle !!\n");



 sqlite3 *sqlite;
 sqlite3_stmt *sqlite_stm;
 char  *gg;
 sqlite3_open("sqlite_test.db",&sqlite);
 sqlite3_prepare(sqlite, "select nom,prenom from perso", 1024, &sqlite_stm, &gg);
 while(sqlite3_step(sqlite_stm)  == 100)
 {
  const unsigned char *s1 = sqlite3_column_text(sqlite_stm, 0);
  const unsigned char *s2 = sqlite3_column_text(sqlite_stm, 1);
  printf("Nom : %s, Prenom : %s.\n", s1, s2);
 }
 sqlite3_finalize(sqlite_stm);
 sqlite3_reset(sqlite_stm);
 sqlite3_close(sqlite);
 return 0;
}



et ça marche nikel !!

bonne chance a ceux qui ont eu le meme probleme.


ok that's good !! The Story is True ...
3
goldziko9 Messages postés 39 Date d'inscription lundi 3 janvier 2005 Statut Membre Dernière intervention 5 avril 2010
14 nov. 2008 à 16:06
Salut
   Personne n'a une idee ??!!!
      c'est grave !!
            ou
                  Y a pas d'autre solution pour utiliser cette base de donner dans SQLITE ??????

Merci d'avance.

ok that's good !!
0
Rejoignez-nous