aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 6de5b43ff28b0c30775917bcc3c30e8b4a044e46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h> 

static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
  int i;
  for(i = 0; i<argc; i++) {
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

void initDb(sqlite3 *db) {
  char *zErrMsg = 0;
  char *sql;
  int rc;

  /* Create SQL statement */
  sql = "CREATE TABLE PROGRAM("                  \
    "id INT PRIMARY KEY     NOT NULL,"           \
    "name           TEXT    NOT NULL);";
  
  /* Execute SQL statement */
  rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

  if( rc != SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  } else {
    fprintf(stdout, "Table created successfully\n");
  }  
  sql = "CREATE TABLE CONFIG("                   \
    "id INT PRIMARY KEY     NOT NULL,"           \
    "path           TEXT    NOT NULL,"           \
    "secret         INT     NOT NULL,"           \
    "prime          INT     NOT NULL);";

  /* Execute SQL statement */
  rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

  if( rc != SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  } else {
    fprintf(stdout, "Table created successfully\n");
  }
  sql = "CREATE TABLE REL("                      \
    "pId INT    NOT NULL,"                       \
    "cId INT    NOT NULL);";
  
  /* Execute SQL statement */
  rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

  if( rc != SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  } else {
    fprintf(stdout, "Table created successfully\n");
  }   
}

 int main(int argc, char* argv[]) {
  sqlite3 *db;
  int rc;

  /* Open database */
  rc = sqlite3_open("ckdb", &db);
   
  if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    return(0);
  }
  else {
    fprintf(stdout, "Opened database successfully\n");
  }

  if (strcmp("init", argv[1]) == 0) {
    initDb(db);
  }

  sqlite3_close(db);
  return 0;
}