Re: [sqlite] SQLite3 with C

2018-08-15 Thread Simon Davies
Hi Ricardo,

On 15 August 2018 at 06:26, Ricardo Lima  wrote:
> 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.

in
if( push_stack( s, value, date, info ) ){
addData( db, node );
 }

node is always NULL, and addData checks for that, and does nothing...

> --- 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!

Regards,
Simon
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] SQLite3 with C

2018-08-15 Thread Simon Slavin
On 15 Aug 2018, at 6:26am, Ricardo Lima  wrote:

> 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.

Please use the SQLite shell tool (download from the SQLite site as "Precompiled 
Binaries") rather than SQLite Studio.

When making SQLite API calls, are you checking the result codes ?  Result codes 
from those calls should be SQLITE_OK to indicated successful completion.  If 
you are getting any other value, post it and we'll figure out what's wrong.

Are you using sqlite3_exec() or _prepare(),_step(),_finalize() ?

Are you closing the database file before your application quits ?

Simon.
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] SQLite3 with C

2018-08-15 Thread Ricardo Lima
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 
#include 
#include 
#include 
#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