C++ cannot find SQLite3 - c++

I am trying to use SQLite with c++ on windows. My code looks like this
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
which returns the error message
C:\sqlite: No such file or directory
compilation terminated.
There have been several other stackoverflow questions on this but they were all solved by changing #include <sqlite3.h> to #include "sqlite3.h" or #include <full_path_to_sqlite3>, none of which work
One was also solved by including -lsqlite3 when compiling, but this returns
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lsqlite3
collect2.exe: error: ld returned 1 exit status
which is odd to me because sqlite3 works just fine for me normally in cmd
How do I fix this?

This is a linking problem basically.
Example solutions:
Make sure the compiler actually sees the sqlite includes
If you haven't already, add the library headers folder to the Additional
include directories
Etc.
Just go step by step and make sure everything is in place.

Related

database in c++ with sqlite3

I am working on my coursework for my university. It consists on making a database in c++. I have those errors from Monday and I can not solve them.
Can anyone help me to fix this issue, please?
I am working in VSCode and I am a beginner in c++, thank you.
code:
#include <stdio.h>
#include <D:\SSD\Desctop\barethika\sqlite3.h>
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
ERRORS:
c:\users\name\AppData\Local\Temp\ccguCZxk.o:tempCodeRunnerFile.cpp(.text+00xa4)
undefined reference to 'sqlite3_open'
c:\users\name\AppData\Local\Temp\ccguCZxk.o:tempCodeRunnerFile.cpp(.text+00xa4)
undefined reference to 'sqlite3_errmsg'
c:\users\name\AppData\Local\Temp\ccguCZxk.o:tempCodeRunnerFile.cpp(.text+00xa4)
undefined reference to 'sqlite3_close' collect2.exe:error: ld returned
1 exit status
Try to set the absolute path of the library. This kind of problem is usually the wrong path of the library

error occur when I call execvp to run java

I use chdir() to switch the directory, and then use execvp() to execute "java Main". I'm sure there is Main.class, but something went wrong. I want to know why.
#include <cstdio>
#include <unistd.h>
using namespace std;
int main(){
char buf[80];
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
chdir("/home/keane/Judge/temp");
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
char *array[3];
array[0] = "java";
array[1] = "Main";
array[2] = NULL;
execvp("java", array);
return 0;
}
the error is could not find the main class , and I can run java Main in that directory.
What drives me crazy is that I can't use system("java Main"), and the error is that Error: Could not find or load main class Main, and it's just like this on my computer
update:
#include <unistd.h>
#include <cstdlib>
int main(){
chdir("/home/keane/Judge/temp");
system("pwd");
system("ls");
system("java Main");
return 0;
}
the output on console is:
/home/keane/Judge/temp
1.out 3.out 5.out Main.class stdout_spj.txt
2.out 4.out ce.txt Main.java
Error: Could not find or load the main class Main
my final solution is to reboot the computer and add -cp . to the java command.
althought I don't why is necessary.
thanks everyone!
This works as intended on my system, maybe you need to add -cp . to your java call.
EDIT: to elaborate: -cp (for classpath) tells java where to look for user provided .class files. This does not necessarily include the current working directory by default.
The execution of execvp() is non-blocking and takes ownership of the caller, that means that when it starts if the program ends too quickly you will never be able to see the result, to solve this I use fork(). The wait is just to avoid using sleep as I used at the begining. Its all in c.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char** argv){
char buf[80];
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
chdir("/home/");
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
char *array[3] = {"java", "Main", NULL};
if(fork() == 0) {
if(execvp("java", array) < 0) {
fprintf(stderr, "Error spawning command: %s\n", strerror(errno));
}
} else {
printf("Command spawned\n");
wait(NULL); // Wait to the forked process to end (avoid using sleep)
}
return 0;
}

Sqlite: Execute Spatialite Functions in Qt5.4.2 or vs2010 along with Qt-addin

Does anyone know whether it is possible to execute spatialite functions on sqlite-databases in Qt5 or visual studio 2010 along with Qt-addin?
In particular, I would be interested in using functions on type Point.
Thanks!
PS: Functions do work within the official spatialite-gui, however I don't think there is a way to use the spatialite-gui programmatically, is there?
Here is what I tried so far: In Qt5.4.2 I connected SQLITE spatialite database and tried using function ST_X(point), ST_Y(point), ST_PointN and ST_NumPoints with no success:
SELECT ST_X(ST_PointN(Geometry, 1)) AS StartX, ST_Y(ST_PointN(Geometry, 1)) AS StartY, ST_X(ST_PointN(Geometry, ST_NumPoints(Geometry))) AS EndX, ST_Y(ST_PointN(Geometry, ST_NumPoints(Geometry))) AS EndY FROM "国道"
Then this always cannot be excuted successfully. And I also find some relate solution about java in Sqlite: Execute Spatialite Functions in Intellij but these yet cannot solve my problem:
step 1: download mod_spatialite-4.3.0a-win-x86.7z file from spatialite website and uncompress it into c:\sqlite3
PS: In Window 10, I open sqlite3 by cmd, SQL sentence that SELECT load_extension('mod_spatialite') could be excuted successfully.
step 2: In my Qt's code, add SELECT load_extension('mod_spatialite') before SQL statement contained spatialite function. Unfortunately, return value by excute QSqlQuery.exec("SELECT load_extension('mod_spatialite')") is false. Oh no, this is a depressing message.
What's more, I also find some relate solution about Qt in https://forum.qt.io/topic/69835/why-can-t-qt-be-installed-with-sqlite-that-has-load_extension-enabled. However, This need recompile Qt from source and it beyond my skill. Finally, How do I in Qt5.4.2 or visual studio 2010 along with Qt-addin? Has anybody done this before?
The load_extension() documentation says:
For security reasons, extension loaded is turned off by default.
You have to enable it with the C function sqlite3_enable_load_extension() or sqlite3_db_config(…SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION…).
And when you're calling C functions anyway, you can also use sqlite3_load_extension().
If you are not able to call the SQLite library functions directly from your Qt application, then your only choice is to recompile Qt (or the Qt plugin).
Thanks for your help very much, particularly, #Marco and #CL..
Below is my two solution:
PS:
First of all, download mod_spatialite-4.3.0a-win-x86.7z from http://www.gaia-gis.it/gaia-sins/windows-bin-x86/. Then, copy file named mod_spatialite.dll to your project's execute content(equal to your .exe program files). Next, write your Qt project and run this Qt project to test it.
My machine runtime: Window 10 + Qt5.4.2 + Sqlite3
First Solution:
As #CL. says,
If you are not able to call the SQLite library functions directly from your Qt application, then your only choice is to recompile Qt (or the Qt plugin).
So I recompile sqlite Qt plugin (file path is C:\Qt\Qt5.4.2\5.4\Src\qtbase\src\plugins\sqldrivers). Then modify two position:
add DEFINES -= SQLITE_OMIT_LOAD_EXTENSION in sqlite.pro;
add below code block in function named open of qsql_sqlite.cpp:
if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, openMode, NULL) == SQLITE_OK) {
...
char* zErrMsg = 0;
char* spatialiteDll = "mod_spatialite";
sqlite3_enable_load_extension(d->access, 1);
sqlite3_load_extension(d->access, spatialiteDll, 0, &zErrMsg);
...
}
Second Solution: Reference links: https://www.sqlite.org/quickstart.html
As #Marco says,
QSqlQuery is a c++/Qt class that you are using to get data from the sqlite database, but Qt is also c++ so you are free to use any c++ sqlite library to open you sqlite database. sqlite.org/quickstart.html
So, I get below code to execute SQL sentence along with spatialite function in Qt5.
Example Code:
#include <QCoreApplication>
#include <stdio.h>
#include <sqlite3.h>
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){
QCoreApplication a(argc, argv);
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 ){
fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
return(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
char* spatialiteDll = "mod_spatialite";
sqlite3_enable_load_extension(db, 1);
sqlite3_load_extension(db, spatialiteDll, 0, &zErrMsg);
fprintf(stderr, "sqlite3 error: %s\n", zErrMsg);
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return a.exec();
}

SDL2 Install problems: makefile.win file not recognized: file format not recognized and collect2.exe [error] id return1 exit status

After I learned the very basics of cpp i decided to push myself ahead and try SDL2 and try to make a game. I found the lazy foo's SDL tutorials. I tried to follow it but i seemed to have problems with installation. After putting in a "test" code i tried compiling it, and these messages showed up on the log:
C:\File\Location\For\My\Project\Makefile.win file not recognized: File format not recognized
C:\File\Location\For\My\Project\collect2.exe [Error] ld returned 1 exit status
I think it might be a linking error and heres my linkers:-lmingw32-lSDL2main-lSDL2
I tried deleting this Makefile.win but the same message just showed up and there isn't even a collect2.exe
I'm using the Orwell Dev-C++ using the mingw gcc 4.8.1 32bit release compiler, and heres the code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char **argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}

Program cannot open file, but the file is already there

I am writing a program that will open an image file, but strange thing happened. This is the output from cmd:
C:\Users\Karl\Pictures>testcvconsole mypic.jpg
argv[0]==testcvconsole
argv[1]==mypic.jpg
fopen is null
strerror(errno)==No such file or directory
Are there something I should consider when fopen simply failed to open my file when the file is right there along side with the executable file in the same directory?
This is on Windows 7, Visual Studios Express 2010. C++.
EDIT: code below
#include "stdafx.h"
#include <string.h>
#include <errno.h>
int goMain(int argc, char** argv);
int _tmain(int argc, _TCHAR* argv[])
{
goMain(argc, (char**)argv);
return 0;
}
int goMain( int argc, char** argv ){
if (argv[1] != NULL){
printf("argv[0]==%S\nargv[1]==%S\n", argv[0], argv[1]);
if (fopen(argv[1], "r") == NULL){
printf("fopen is null\n");
printf(strerror(errno));
}
}
return 0;
}
EDIT2:
I have tried
char *workingDir =_getcwd(NULL, 0);
printf("workingDir == %S", workingDir);
as TomK has suggested and it returned:
workingDir ==
Nothing at all. Hmm...
EDIT3:
I am getting something. I tried
argv[1] = "C:/Users/Karl/Pictures/mypic.jpg";
And fopen can open it. This statement above is inserted right before the fopen.
Make absolutely sure they are in the same directory. I'm saying this because you're using Visual Studio, for which the "same" directory isn't always so clear, because it depends on how you execute the executable through the IDE.
C:\Users\Karl\Pictures>testcvconsole mypic.jpg
Are you sure mypic.jpg is located in C:\Users\Karl\Pictures ?
Can u check whether the working directory is correct?
#include <direct.h>
char *workingDir =_getcwd(NULL, 0);
Can you run your application with admin privileges?
Usually the .exe is created in sub-directory either Debug or Release - try giving the absolute path to the image ...
I've had this problem, and it turned out that Visual Studio's runtime wasn't setting the current directory. I never figured out the problem: instead I simply used an absolute path. Without the absolute path, your program is looking in C:\. You can also try using ".\\mypic.jpg" or GetCurrentDirectory().