SQLite/execution.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2021-03-05 17:20:12 +01:00
#include <stdio.h>
#include "sqlite3.h"
#include <iostream>
#include <string.h>
#include <fstream>
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;
}
int main(int argc, char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
bool run = true;
bool movies_db_loaded = false;
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
while(run)
{
std::string input;
std::cout << "Insert command please" << std::endl;
std::getline(std::cin,input);
//std::cout << input << std::endl;
if(input == "exit")
{
run = false;
}
else if(input == "load movies")
{
std::cout << "Loading movies" << std::endl;
std::ifstream moviesfile;
2021-03-26 16:13:17 +01:00
moviesfile.open("transaction.txt");
2021-03-05 17:20:12 +01:00
std::string instruction;
while (std::getline(moviesfile, instruction))
{
//std::cout << instruction << std::endl;
char *instructionchars = new char[instruction.length() + 1];
strcpy(instructionchars, instruction.c_str());
rc = sqlite3_exec(db, instructionchars, callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
2021-03-26 16:13:17 +01:00
delete(instructionchars);
2021-03-05 17:20:12 +01:00
}
std::cout << "Done" << std::endl;
}
else
{
std::cout << "Executing input" << std::endl;
char *inputchars = new char[input.length() + 1];
strcpy(inputchars, input.c_str());
rc = sqlite3_exec(db, inputchars, callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
2021-03-26 16:13:17 +01:00
delete(inputchars);
2021-03-05 17:20:12 +01:00
}
}
sqlite3_close(db);
return 0;
2021-03-26 16:13:17 +01:00
}