|
- #include <stdlib.h>
- #include <stdio.h>
- #include <sqlite3.h>
-
- static int sqlite_callback(void* notUsed, int nb_col, char **colonne, char **col_name);
-
- int main(int argc, char **argv)
- {
- int rc;
- sqlite3* db = NULL;
- sqlite3_stmt* stmt = NULL;
-
- if ((rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) == SQLITE_OK)
- printf("Ouverture OK\n");
- else
- {
- printf("Erreur open = %i\n",rc);
- exit(0);
- }
-
- 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)
- printf("Prepare OK\n");
- else
- {
- printf("Erreur prepare = %i\n",rc);
- exit(0);
- }
-
- 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);
- //int sqlite_column_count(stmt) retourne nb_col (0 si stmt sans retour de data)
- //int sqlite_column_type(stmt, n_col) retourne le type SQLITE_INTEGER/FLOAT/TEXT/BLOB/NULL
- printf("Step OK\n");
- else
- {
- printf("Erreur step = %i\n",rc);
- exit(0);
- }
-
- if ((rc = sqlite3_finalize(stmt)) == SQLITE_OK)
- printf("Finalize OK\n");
- else
- {
- printf("Erreur finalize = %i\n",rc);
- exit(0);
- }
-
- 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
- printf("Exec OK\n");
- else
- {
- printf("Erreur exec = %i\n",rc);
- exit(0);
- }
-
- //rc = sqlite3_exec(db, query, sqlite3_callback, 0, &errMsg) (char* query, errMsg) argument 0 est passé en premier à callback
- //if rc!=SQLITE_OK printf("Err %s\n", errMsg); if (errMsg) free (errMesg);
-
- if ((rc = sqlite3_close(db)) == SQLITE_OK)
- printf("Close OK\n");
- else
- {
- printf("Erreur close = %i\n",rc);
- exit(0);
- }
- return 0;
- }
-
- static int sqlite_callback(void* notUsed, int nb_col, char** colonne, char** col_name) //tout est obtenu avec sqlite3_column_text()
- {
- int i;
- for (i=0; i<nb_col; i++)
- {
- printf("%s = %s\n", col_name[i], colonne[i]);
- }
- return 0;
- }
|