SQLite/execution.cpp

83 lines
2.1 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;
moviesfile.open("movies.txt");
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);
}
}
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);
}
}
}
sqlite3_close(db);
return 0;
}