Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include"sqlite3.h"
- int main(int argc, char *argv[]){
- sqlite3 *database_connection; //initialize connection handle
- sqlite3_stmt *database_statement; //initialize statement variable
- char query[101] = {0};
- int error = 0; //initialize error indicator
- if(argc != 2){ //arguments == 1 ? (database name)
- fprintf(stderr,"error: invalid number of parameters\n");
- return 1;
- }
- error = sqlite3_open(argv[1], &database_connection); //open or create requested database
- if(error){
- fprintf(stderr,"error: could not open file\n");
- return 1;
- }
- strcpy(query, "CREATE TABLE IF NOT EXISTS stock (product_name TEXT PRIMARY KEY, product_count INTEGER DEFAULT 0)"); //prepare sql command: CREATE TABLE
- error = sqlite3_exec(database_connection, query, 0, 0, 0); //execute prepared command
- if(error){
- fprintf(stderr,"error: could not create stock table\n");
- return 1;
- }
- char command[21] = {0}; //initialize array for commands
- char product_name[21] = {0}; //initialize array for product names
- printf("> ");
- scanf("%20s", command);
- if(command[0] == '\0'){
- fprintf(stderr, "error: invalid command");
- return 1;
- }
- if(!strcmp(command, "NOVY")){ //received "NOVY"
- if(!scanf("%20s", product_name)){ //invalid product name received
- fprintf(stderr, "error: invalid product name");
- return 1;
- }
- //sprintf(query, "SELECT product_name FROM stock WHERE product_name = %s", product_name); //prepare sql command: SELECT to check whether requested already product exists
- sprintf(query, "INSERT INTO stock VALUES(%s, 0)", product_name);
- //printf("debug: query = %s\n", query);
- error = sqlite3_prepare_v2(database_connection, query, -1, &database_statement, NULL);
- if(error){
- printf("sqlite prepare error: %d\n", error);
- fprintf(stderr,"error: could not prepare operation SELECT product_name -0\n");
- return 1;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment