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.

77 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,
  25. //traiter la courante avec int/char* sqlite3_column_int/text(stmt, n_col);
  26. //int sqlite_column_count(stmt) retourne nb_col (0 si stmt sans retour de data)
  27. //int sqlite_column_type(stmt, n_col) retourne le type SQLITE_INTEGER/FLOAT/TEXT/BLOB/NULL
  28. printf("Step OK\n");
  29. else
  30. {
  31. printf("Erreur step = %i\n",rc);
  32. exit(0);
  33. }
  34. if ((rc = sqlite3_finalize(stmt)) == SQLITE_OK)
  35. printf("Finalize OK\n");
  36. else
  37. {
  38. printf("Erreur finalize = %i\n",rc);
  39. exit(0);
  40. }
  41. 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
  42. printf("Exec OK\n");
  43. else
  44. {
  45. printf("Erreur exec = %i\n",rc);
  46. exit(0);
  47. }
  48. //rc = sqlite3_exec(db, query, sqlite3_callback, 0, &errMsg) (char* query, errMsg) argument 0 est passé en premier à callback
  49. //if rc!=SQLITE_OK printf("Err %s\n", errMsg); if (errMsg) free (errMesg);
  50. if ((rc = sqlite3_close(db)) == SQLITE_OK)
  51. printf("Close OK\n");
  52. else
  53. {
  54. printf("Erreur close = %i\n",rc);
  55. exit(0);
  56. }
  57. return 0;
  58. }
  59. static int sqlite_callback(void* notUsed, int nb_col, char** colonne, char** col_name) //tout est obtenu avec sqlite3_column_text()
  60. {
  61. int i;
  62. for (i=0; i<nb_col; i++)
  63. {
  64. printf("%s = %s\n", col_name[i], colonne[i]);
  65. }
  66. return 0;
  67. }