You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sqlite3.h>
  4. static int sqlite_callback(void* notUsed, int nb_col, char **colonne, char **col_name);
  5. int main(int argc, char **argv)
  6. {
  7. int rc;
  8. sqlite3* db = NULL;
  9. sqlite3_stmt* stmt = NULL;
  10. if ((rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) == SQLITE_OK)
  11. printf("Ouverture OK\n");
  12. else
  13. {
  14. printf("Erreur open = %i\n",rc);
  15. exit(0);
  16. }
  17. if ((rc = sqlite3_prepare_v2(db, "CREATE TABLE Consults (id INTEGER PRIMARY KEY ASC AUTOINCREMENT, n_dossier INT UNIQUE, nom VARCHAR(50), prenom VARCHAR(50), responsable VARCHAR(50), theme TEXT, date_cs DATE, nb_result INT, id_results TEXT, publi BOOLEAN, cs_associe BOOLEAN, publi_CHU BOOLEAN);", -1, &stmt, NULL)) == SQLITE_OK)
  18. printf("Prepare OK\n");
  19. else
  20. {
  21. printf("Erreur prepare = %i\n",rc);
  22. exit(0);
  23. }
  24. if ((rc = sqlite3_step(stmt)) == SQLITE_DONE) //SQLITE_ROW si une nouvelle ligne est chopable, traiter la courante avec int/char* sqlite3_column_int/text(stmt, n_col);
  25. //int sqlite_column_count(stmt) retourne nb_col (0 si stmt sans retour de data)
  26. //int sqlite_column_type(stmt, n_col) retourne le type SQLITE_INTEGER/FLOAT/TEXT/BLOB/NULL
  27. printf("Step OK\n");
  28. else
  29. {
  30. printf("Erreur step = %i\n",rc);
  31. exit(0);
  32. }
  33. if ((rc = sqlite3_finalize(stmt)) == SQLITE_OK)
  34. printf("Finalize OK\n");
  35. else
  36. {
  37. printf("Erreur finalize = %i\n",rc);
  38. exit(0);
  39. }
  40. if ((rc = sqlite3_exec(db, "CREATE TABLE Resultats (id INTEGER PRIMARY KEY, titre TEXT, auteurs TEXT, lien TEXT, abstract TEXT, date_publi DATE);", NULL, NULL, NULL)) == SQLITE_OK) //commande sql sans retour
  41. printf("Exec OK\n");
  42. else
  43. {
  44. printf("Erreur exec = %i\n",rc);
  45. exit(0);
  46. }
  47. //rc = sqlite3_exec(db, query, sqlite3_callback, 0, &errMsg) (char* query, errMsg) argument 0 est passé en premier à callback
  48. //if rc!=SQLITE_OK printf("Err %s\n", errMsg); if (errMsg) free (errMesg);
  49. if ((rc = sqlite3_close(db)) == SQLITE_OK)
  50. printf("Close OK\n");
  51. else
  52. {
  53. printf("Erreur close = %i\n",rc);
  54. exit(0);
  55. }
  56. return 0;
  57. }
  58. static int sqlite_callback(void* notUsed, int nb_col, char** colonne, char** col_name) //tout est obtenu avec sqlite3_column_text()
  59. {
  60. int i;
  61. for (i=0; i<nb_col; i++)
  62. {
  63. printf("%s = %s\n", col_name[i], colonne[i]);
  64. }
  65. return 0;
  66. }