I built a straightforward program in C. It's just a STACK structure (first in, 
last out) that takes input from the user and stores it into the stack. After I 
close the program, all the data vanishes since I don't have a database 
implemented into the program. Even so, the program runs fine. It allocates 
memory according to the user's input, and I always validate every allocation.

In fact, my issue isn't concerning C code itself. It's the implementation of 
SQLite into the C code. Every stack_node has a value (integer), date (char), 
and info (char). The wallet structure is the "middle-man" between the 
stack_node and the DB, and it has the same attributes plus an ID (integer 
autoincremented). What I'm trying to do is to as soon as the user inputs data 
(while the program is running) this data gets stored not only in the stack but 
also in the DB. After the user is done inputting data, I usually use 
pop_stack() to retrieve the data from the stack onto the screen, but since the 
data is stored in the DB, it makes more sense just to extract all data from the 
DB.

I don't get any compilation errors and the SQLite source files/header files are 
implemented correctly into the code. I know this because I'm using SQLite 
Studio ( SQLite GUI) and I see all the attributes from the wallet DB, but they 
are all empty.
So, that's my issue. I'm doing something wrong that causes the program to not 
store the data correctly into the DB.

--- Is it a waste of memory and running time for the program to create a stack, 
stack_node etc...to only after all that store the information in the DB? Should 
I just skip all that and program just for the DB? If so, how would I do that 
since I would eventually run into the same problem. I'm using the stack because 
I might have more control over what I can do with the data like adding the 
values, calculate average expenditure and so on.



Thank you so much!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#define MAXB 1024
#define MAXDT 64
#define MAXIF 128

#define CREATE_TABLE_WALLET \
    "CREATE TABLE IF NOT EXISTS wallet" \
    "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
    ",value INTEGER NOT NULL"\
    ", date TEXT NOT NULL" \
    ", info TEXT NOT NULL)"

typedef struct Wallet Wallet;

struct Wallet{
    int id;
    int value;
    char date[64];
    char info[128];
    Wallet * next;
};

typedef struct stack_node{
    int value;
    char * date;
    char * info;
    struct stack_node * next;
}stack_node;

typedef struct stack{
    int size;
    stack_node * top;
}stack;

stack * create_stack(){
    stack * s = malloc(sizeof * s);
    if(!s){
        perror("malloc-s");
        return NULL;
    }
    s->size=0;
    s->top=NULL;
    return s;
}

stack_node * push_stack(stack * s,int value,char * date,char * info){
    stack_node * n = malloc(sizeof * n);
    if(!n){
        perror("malloc-n");
        return NULL;
    }
    n->value=value;
    ///-------------
    n->date = malloc(strlen(date) + 1);
    if(!n->date){
        perror("malloc-date");
        return NULL;
    }
    strcpy(n->date,date);
    ///-------------

    ///-------------
    n->info = malloc(strlen(info) + 1);
    if(!n->info){
        perror("malloc-info");
        return NULL;
    }
    strcpy(n->info,info);
    ///-------------
    n->next=s->top;
    s->top=n;
    s->size++;
    return s;
}

stack_node * pop_stack(stack * s){
    if(s->size > 0){
        stack_node * node = s->top;
        s->top=s->top->next;
        return node;
        s->size--;
    }
    return NULL;
}

void free_node(stack_node * n){
    if(n->date && n->info)
        free(n->date);
        free(n->info);

    free(n);
}

void free_stack(stack * s){
    while(s->size > 0){
        stack_node * victim = s->top;
        s->top=s->top->next;
        free_node(victim);
        s->size--;
    }
    free(s);
}

Wallet * newWallet(const Wallet * wallet){
    Wallet * w = (Wallet *)malloc(sizeof(Wallet));
    strcpy(w->date,wallet->date);
    strcpy(w->info,wallet->info);
    w->value = wallet->value;
    w->id=wallet->id;
    w->next = NULL;
    w->id=-1;
    return w;
}

int error(sqlite3 * db){
    fprintf(stderr, "Error: %s \n",sqlite3_errmsg(db));
    return sqlite3_errcode(db);
}

void addData( sqlite3 * db, stack_node * node){
    if(node == NULL){
        return;
        }

    char sql[100];
    while(node != NULL){
        sprintf(sql, "INSERT INTO wallet(value,date, info) VALUES 
(%d,'%s','%s')",
                node->value,node->date,node->info);
        if(sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK){
            error(db);
            return;
        }
        node=node->next;
    }
}

int callback(void *ptr,int numbCol,char **valCell, char **nameCol){
    (void)ptr;
    int ix;
    for(ix=0; ix < numbCol ; ++ix){
        printf("%s: %s \n",nameCol[ix],valCell[ix]);
    }
    printf("\n");
    return 0;
}

void readDB(sqlite3 * db){
    sqlite3_exec(db, "SELECT * FROM wallet", callback, NULL,NULL);
}

int main(void)
{
    stack * s =  create_stack();
    stack_node * node = NULL;

    Wallet wallet;

    sqlite3 * db = NULL;
    const char * filenameDatabase = 
"C:/Users/ricar/Downloads/docs/C/trialszz/wall.db";

    ///open date base
    if(sqlite3_open(filenameDatabase,&db) != SQLITE_OK){
        error(db);
    }

    ///config DB
    if(sqlite3_exec(db, CREATE_TABLE_WALLET, NULL, NULL,NULL) != SQLITE_OK){
        return error(db);
    }


    for(;;){
        char buf[MAXB] = "";
        char date[MAXDT] = "";
        char info[MAXIF] = "";
        int value = 0;

        for(;;){
            fputs("\nenter value: ",stdout);
            if(fgets(buf,MAXB,stdin)){
                if(*buf == '\n'){
                    fputs("**input done**",stderr);
                    goto inputdone;
                }
                else if(sscanf(buf,"%d",&value) == 1)
                    break;
                else{
                    fputs("**invalid integer input**\n",stderr);
                }
            }
            else{
                fputs("**user canceled input** \n",stderr);
                goto inputdone;
            }
        }

        for(;;){
            fputs("enter date: ",stdout);
            if(fgets(date,MAXDT,stdin)){
                size_t lenDT = strlen(date);
                if(lenDT && date[lenDT - 1] == '\n'){
                    date[--lenDT] = 0;
                    if(lenDT)
                        break;
                    else
                        fputs("**empty-line**\n",stderr);
                    }
                    else if(lenDT == MAXDT -1){
                        fputs("**warning: string too long** \n",stderr);
                    }
            }
            else{
                fputs("**user canceled input**\n",stderr);
                goto inputdone;
            }
        }

        for(;;){
            fputs("enter INFO about value: ",stdout);
            if(fgets(info,MAXIF,stdin)){
                size_t lenIF = strlen(info);
                if(lenIF && info[lenIF - 1] == '\n'){
                    info[--lenIF] = 0;
                        if(lenIF)
                            break;
                        else
                            fputs("**empty-line**\n",stderr);
                }
                else if(lenIF == MAXIF - 1){
                    fputs("**warning: string too long**",stderr);
                }
            }
            else{
                fputs("**user canceled input**",stderr);
                goto inputdone;
            }
        }
        if(push_stack(s,value,date,info)){
                addData(db, node);
            }
    }
    inputdone:;


    puts("\n\n|-|-|STACK DATA|-|-|\n\n");
    readDB(db);

    /* while((node = pop_stack(s)) != NULL){
        printf("value: %2d  date: %s  info: %s  
\n",node->value,node->date,node->info);
        free_node(node);
    }
    free_stack(s);   */

    sqlite3_close(db);

    return 0;
}
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to