C++/SQLite only outputting one row of data - c++

I'm having an issue where only one of my 5 test rows will output in C++. My code is:
#include <cstdio>
#include <sqlite3.h>
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/*Definitions*/
sqlite3 *db;
void *arg;
char *err;
const char* stmt = "SELECT * from table";
/*End of Definitions*/
int exec(void *arg, int argc, char **argv, char **column) {
int i;
for(i = 0; i < argc; i++) {
cout << column[i] << ": " << argv[i] << endl;
}
cout << "------" << endl;
}
int main() {
int rc = sqlite3_open("test.sqlite", &db); /*Open db "test.sqlite"*/
if(!rc) {
while(true) {
sqlite3_exec(db, stmt, exec, arg, &err);
if(err) {
break;
}
}
}
/*Ending Stuffz (NOTHING BEYOND THIS POINT)*/
cin.get();
return 0;
}
I am not getting any errors; it is purely just outputting the first row. Any help is appreciated, thanks.

Just return 0 in the end of exec function
Explanation:
In function 'exec" when it return nothing, the database server acts like the function is terminated by somehow (like something corrupt the function and made an error) so it just stop sending data because the call back function terminated with an error.... so when we return 0 we say that the function "exec" is working normally so it keeps sending data ... and if you return any other integer it will deal with it like the function terminated by an error and stop sending data. I faced the same issue and I solved it by making the callback function (exec) return 0.

Related

Is there a function for sleeping a set amount in C++ for windows?

I know there is one in C, Sleep(ms), but is there one for C++? I am trying to return an error, then print to the console, then sleep enough for the user to read it before returning the errorcode. Code in C would be:
#include <stdio.h>
#include <windows.h>
int main (int argc, const char *argv[]) {
char *err = "Have an error!";
printf("Error: %s. Program terminating in 5 seconds...", err);
Sleep(5000);
return 1;
}
You could include <windows.h> and just call the WinApi function Sleep just as you would from C. This is mostly pure C++ :
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
int main (int argc, const char *argv[]) {
string err = "Have an error!";
cout << "Error: " << err << ". Program terminating in 5 seconds..." << endl;
std::chrono::milliseconds timespan(5000);
std::this_thread::sleep_for(timespan);
return 1;
}

how to reboot the application when there is a change in last modification time of an accesed file in that application

I'm new to thread programming. I'm trying to create an application which continually checks the Last Modification time of some file and exits the program when that time has changed.
Please find my code below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <cerrno>
#include <unistd.h>
using namespace std;
#define NUM_THREADS 2
void *getFileCreationTime(void *path) {
const char *pt;
pt=(const char *)path;
struct stat attr;
stat("/home/utthunga/shmrp.cpp", &attr);
while(1){
char *timestamp= ctime(&attr.st_mtime);
if(timestamp)
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"No changes has been made to the file"<<endl;
sleep(4);
}
else
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"Time stamp has been changed"<<endl;
exit(0);
}
}
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
int i;
int rc;
for( i = 0; i < NUM_THREADS-1; i++ )
rc = pthread_create(&threads[i], NULL, getFileCreationTime, (void *)i);
pthread_exit(NULL);
return 0;
}
Can anyone please tell me what changes I have to implement in order to check the last modification time of that file continually and exit the application when that time has changed?
After you retrieve the file's modification time the first time, you need to save it so you can compare it to subsequent values retrieved afterwards.
Try something more like this instead:
void* getFileCreationTime(void *) {
const char *path = "/home/utthunga/shmrp.cpp";
struct stat attr;
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
time_t mtime = attr.st_mtime;
cout << "Last modified time: " << ctime(&mtime) << endl;
while(1) {
sleep(4);
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
if (attr.st_mtime != mtime) {
cout << "Time stamp has been changed" << endl;
exit(0);
} else {
cout << "No changes have been made to the file" << endl;
}
}
pthread_exit(NULL);
}

SQLite3/C++ Only outputting one row of data [duplicate]

This question already has an answer here:
C++/SQLite only outputting one row of data
(1 answer)
Closed 5 years ago.
I'm having an issue where only one of my 2 test rows will output in C++. My code is:
#include <cstdio>
#include <sqlite3.h>
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/*Definitions*/
sqlite3 *db;
void *arg;
char *err;
const char* stmt = "SELECT * from table";
/*End of Definitions*/
int exec(void *arg, int argc, char **argv, char **column) {
int i;
for(i = 0; i < argc; i++) {
cout << column[i] << ": " << argv[i] << endl;
}
cout << "------" << endl;
}
int main() {
int rc = sqlite3_open("test.sqlite", &db); /*Open db "test.sqlite"*/
if(!rc) {
while(true) {
sqlite3_exec(db, stmt, exec, arg, &err);
if(err) {
break;
}
}
}
/*Ending Stuff (NOTHING BEYOND THIS POINT)*/
cin.get();
return 0;
}
I am not getting any errors; it is purely just outputting the first row. cout << err << endl; will not output anything. Any help is appreciated, thanks. Also, I made a post about this 2 days ago but due to me not receiving help, I am reposting. #CL. happened to make a comment on my previous post saying my SQL was incorrect but I am still yet to figure out what he meant by that
sqlite3_exec () is used to execute insert update statements. For select use sqlite3_prepare (...)

C++ Program Compiles in Windows and Linux. But Doesn't Work on Linux

I think it might be something related to the way my pointers are used/initialized in my struct, but I'm not completely sure. I use 'g++ -lpthread main.cpp' to compile. The program just hangs in Linux, while executing properly in windows. The program doesn't even spit out a cout I put in the beginning of the code for debugging purposes.
#include "pthread.h"
#include "semaphore.h"
#include "time_functions.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct vars {
char buffer[10][1000];
int put;
int take;
sem_t openSlot;
sem_t slotInactive;
sem_t newData;
ifstream readFile;
ofstream writeFile;
};
void *write(void *in) //consumer, writes data to file and deletes data from buffer
{
vars* writeVars = (vars*)in;
while (true)
{
sem_wait(&(*writeVars).newData);
sem_wait(&(*writeVars).slotInactive);
if ((*writeVars).buffer[(*writeVars).take % 10][0] != '$')
{
(*writeVars).writeFile << (*writeVars).buffer[(*writeVars).take % 10];
if ((*writeVars).readFile.eof() != true)
{
(*writeVars).writeFile << endl;
}
else
{
break;
}
}
(*writeVars).take++;
sem_post(&(*writeVars).openSlot);
sem_post(&(*writeVars).slotInactive);
}
pthread_exit(0);
return 0;
}
void *read(void *in) //producer, reads data into buffer
{
vars* readVars = (vars*)in;
char read_line[1000];
while ((*readVars).readFile.getline(read_line, 1000))
{
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
strcpy((*readVars).buffer[(*readVars).put % 10], read_line);
(*readVars).put++;
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
}
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
(*readVars).buffer[(*readVars).put % 10][0] = '$';
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
pthread_exit(0);
return 0;
}
int main(int argc, char* argv[])
{
char pause[10];
vars *varsPointer, var;
varsPointer = &var;
var.take = 0;
var.put = 0;
var.writeFile.open(argv[2], ios::out);
var.readFile.open(argv[1], ios::in);
start_timing();
sem_init(&var.openSlot, 0, 10);
sem_init(&var.slotInactive, 0, 1);
sem_init(&var.newData, 0, 0);
pthread_t read_Thread, write_Thread;
pthread_create(&read_Thread, NULL, read, varsPointer);
pthread_create(&write_Thread, NULL, write, varsPointer);
pthread_join(read_Thread, NULL);
pthread_join(write_Thread, NULL);
sem_destroy(&var.openSlot);
sem_destroy(&var.slotInactive);
sem_destroy(&var.newData);
stop_timing();
var.readFile.close();
var.writeFile.close();
//Display timer
cout << "wall clock time (ms):" << get_wall_clock_diff() * 1000 << '\n';
cout << "cpu time (ms):" << get_CPU_time_diff() * 1000 << '\n';
cout << "Type Something and Press Enter To Continue";
cin >> pause; //Just used to keep cmd promt open in Windows after program execution
return 0;
}

Storing and manipulating the results of SQLite query in C++

I am a beginner to C++ and SQLlite and trying to compile code which can manipulate the results of a query from SQLite.
I am having difficulties in storing the results to a .txt file, which will enable me to manipulate results, and don't know where to start,
As a result, I can only see one row in MA.txt however I would like to store all of the results,
I want to store results into a .txt file because after I store the results, I have to divide the results into predefined lengths and find max and min values and report the first row and last row as well.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>
#include <locale>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
static int callback2(void *data, int argc, char **argv, char **azColName)
{
ofstream os;
os.open("MA.txt");
os << argv[0] << endl;
return 0;
os.close();
}
int main(int argc, char* argv [])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char* data = "Callback function called";
rc = sqlite3_open("test.db", &db);
if (rc){
cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
exit(0);
}
string sql = "SELECT * from forex;";
rc = sqlite3_exec(db, sql.c_str(), callback2, (void*) data, &zErrMsg);
if (rc != SQLITE_OK){
cout << "SQL error:" << zErrMsg;
sqlite3_free(zErrMsg);
}
return 0;
}
I changed the code however I got "os does not name a type" error while compiling,
where should I put the offsting, sorry I am really a noobie:(
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>
#include <locale>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
ofstream os;
os.open("MA.txt");
static int callback2(void *data, int argc, char **argv, char **azColName)
{
os << argv[0] << endl;
return 0;
}
int main(int argc, char* argv [])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char* data = "Callback function called";
rc = sqlite3_open("test.db", &db);
if (rc){
cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
exit(0);
}
string sql = "SELECT * from forex;";
rc = sqlite3_exec(db, sql.c_str(), callback2, (void*) data, &zErrMsg);
if (rc != SQLITE_OK){
cout << "SQL error:" << zErrMsg;
sqlite3_free(zErrMsg);
}
return 0;
os.close();
}
I think that this is because of the way you are opening the file. You are opening it every time you read a new record and this causes it to go back to the beginning every time.
One way to fix this is to declare the ofstream outside the function and open it outside the function and close it at the very end. Another way to fix it is to open the ofstream with the std::app flag set, so that it will append to the file instead of rewriting it.
Another thing is that you are returning '0' from the call back function. You need to return SQLITE_OK to tell it to continue.
Not sure if this will work because I haven't tested it, but try this code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>
#include <locale>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
static int callback2(void *data, int argc, char **argv, char **azColName) {
ofstream os("MA.txt", ios::app);
os << argv[0] << endl;
os.close();
return SQLITE_OK;
}
int main(int argc, char* argv []) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char* data = "Callback function called";
rc = sqlite3_open("test.db", &db);
if (rc){
cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
exit(0);
}
char sql[21] = "SELECT * from forex;";
rc = sqlite3_exec(db, sql.c_str(), callback2, NULL, &zErrMsg);
if (rc != SQLITE_OK){
cout << "SQL error:" << zErrMsg;
sqlite3_free(zErrMsg);
}
return 0;
}