trying to connect to pgsql in qt driver not loaded - postgresql

I have windows 10, qt version is 4.14.1 postgres version is 13.3 (most recent as of 7/23/21. When I run the following code below in qt it gives me the following error.
"Driver not loaded Driver not loaded"
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
I have tried to place dll files into areas like the person in the following youtube video https://www.youtube.com/watch?v=qeErME39zvw as this worked when I was trying to set up MySQL on qt
QSqlDatabase db2 = QSqlDatabase::addDatabase("QPSQL");
db2.setHostName("127.0.0.1");
db2.setUserName("test");
db2.setPassword("test");
db2.setDatabaseName("test");
if (db2.open())
{
qDebug() << "\n\n\n database connected successfully\n\n\n";
}
else
{
qDebug() << "\n\n\n database connection go poop \n\n" << db2.lastError().text() << " \n ";
qDebug() << QSqlDatabase::drivers();
}
qDebug() << QSqlDatabase::drivers();
db2.close();
return 0;

Related

Why do I get "Failed to open macho file..." when running program under VSCODE but not under normal shell?

I have a C++ program (compiled using CLANG++) that executes CLANG++ to compile another program. When I run this program using OSX shell it runs fine. When I run it under VSCODE debug it runs but following message is displayed: "023-01-12 15:01:02.577820-0800 clang++[10766:1004764] Failed to open macho file at /Library/Developer/CommandLineTools/usr/bin/clang++ for reading: Too many levels of symbolic links". I'm assuming that this message is coming from CLANG++ but I couldn't swear to that. Note, too, that the return code from clang++ is 0.
The routine that executes the compiler is (Note that I also have a routine that runs the compiled program but there isn't any problem with that):
void compileit(){
string codeLoc = "/usr/bin/clang++";
vector<char *> argv;
argv.push_back((char *)codeLoc.c_str());
argv.push_back((char *)"-g");
argv.push_back((char *)"-std=c++14");
argv.push_back((char *)"-otest.a");
argv.push_back((char *)"test.cpp");
argv.push_back((char *)NULL);
pid_t pid;
if ((pid = fork()) < 0) {
cout << "Fork failed" << endl;
exit(999);
}else if (pid > 0) {
cout << "Launching compiler" << endl;
int wstatus;
waitpid(-1, &wstatus, 0);
if (wstatus == 0){
cout << "CPP compiled successfully" << endl;
return;
}else{
cout << "Compile of CPP file failed: " << wstatus << endl;
}
}else{
int status = execv(codeLoc.c_str(),&argv[0]);
cout << "Couldn't launch the compiler" << endl;
exit(998);
}
}
type here
The program being compiled (by the code above) is:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
cout << "\nhello world" << endl;
return 0;
}
type here
The launch.json file is:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": ["-b1"],
"cwd": "${workspaceFolder}",
"preLaunchTask": "C/C++: clang++ build active file",
}
]
}
And, the output displayed by VSCODE's terminal when I run the program (including the output when it is run) is:
Launching compiler
2023-01-12 15:01:02.577820-0800 clang++[10766:1004764] Failed to open macho file at /Library/Developer/CommandLineTools/usr/bin/clang++ for reading: Too many levels of symbolic links
CPP compiled successfully
From main: Launching test.a
test.a completed successfully
From forked main: Launching test.a
carl#McTreehouse test %
hello world
type here
A caveat: I'm new to Stack Overflow so If I've not provided enough or the correct information please let me know and I'll provide updates. I'm also more of a hobbyist c++ programmer than a professional (my background is Tandem/HP Nonstop TAL/Cobol).
My original code, which was related to a larger project, exhibited this behavior so I tried to reduce the specifics to the code described above. I had hoped that something I was doing in the larger context was causing the problem but after getting the specific code to its minimal state the problem persisted.
I spent some time looking at the VSCODE properties but couldn't spot anything that seemed like a problem. I also went looking for symlinks in the /Library/Developer directory but didn't spot anything (but could have easily missed it).
What I'm concerned about it this behavior showing up randomly when running the larger project from the shell or as a CGI.

connecting to questDB with libpqxx

Im having an issue connecting to QuestDB with libpqxx, i can establish a connection using the postgresql client as per the instructions here:
https://questdb.io/docs/guidePSQL
however, when i go to connect to QuestDB, using my simple program, that is more-or-less a slightly modified version of the standard "get me started" program:
https://github.com/jtv/libpqxx
#include <iostream>
#include <pqxx/pqxx>
int main(){
try
{
pqxx::connection C(
"user=admin "
"hostaddr=127.0.0.1 "
"password=quest "
"dbname=qdb"
"port=8812 ");
std::cout << "Connected to " << C.dbname() << std::endl;
pqxx::work W{C};
pqxx::result R{W.exec("SELECT name FROM employee")};
std::cout << "Found " << R.size() << "employees:\n";
for (auto row: R)
std::cout << row[0].c_str() << '\n';
std::cout << "Doubling all employees' salaries...\n";
W.exec0("UPDATE employee SET salary = salary*2");
std::cout << "Making changes definite: ";
W.commit();
std::cout << "OK.\n";
}
catch (std::exception const &e)
{
std::cerr << e.what() << '\n';
return 1;
}
return 0;
}
.. i get an error:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
what also complicates things possibly, is i cannot find anywhere pg_hba.conf ,are these even still a thing in version 10 of postgresql? i have /usr/lib/postgresql/10 but no config files.. and ive also searched the machine.. nothing found. any help would be much appreciated.
thankyou
The official documentation for libpqxx states that:
The connection string consists of attribute=value pairs separated by spaces, e.g. "user=john password=1x2y3z4". reference
Your connection string is:
pqxx::connection C(
"user=admin "
"hostaddr=127.0.0.1 "
"password=quest "
"dbname=qdb"
"port=8812 ");
You are missing a space after qdb, so the correct connection string is:
pqxx::connection C(
"user=admin "
"hostaddr=127.0.0.1 "
"password=quest "
"dbname=qdb "
"port=8812 ");
I just tried it and it works fine for me.
On a second hand, the following SQL statement:
W.exec0("UPDATE employee SET salary = salary*2");
Will not work, UPDATE is not supported yet by QuestDB. You can find more details about SQL support on the official documentation, here.

Sometimes, the asio::tcp::socket object gets closed automatically before calling shutdown/close

Once in 5 times, I get this "Bad file descriptor"error when I am attempting to shutdown or close the asio::ip::tcp::socket object. The following is the function to close the acceptor and socket.
void close_server()
{
acceptor_instance.close(error_code);
if (error_code)
{
std::cerr << "close_server(): acceptor::close()" << "Error: " << error_code.message() << std::endl;
}
if(socket_instance.is_open())
{
socket_instance.shutdown(asio::ip::tcp::socket::shutdown_both, error_code);
if (error_code)
{
std::cerr << "close_server(): socket::close()" << "Error: " << error_code.message() << std::endl;
}
socket_instance.close(error_code);
if (error_code)
{
std::cerr << "close_server(): socket::close()" << "Error: " << error_code.message() << std::endl;
}
}
else
{
std::cerr << "close_server(): socket_instance is already CLOSED" << std::endl;
}
}
Sometimes the socket_intance.is_open() fails. Attempting to call the socket.shutdown() or socket.close() without checking if the socket is open, gives "Bad file descriptor" error, that means the socket was already closed.
How to identify the cause?
How to know when it was closed?

How fix strange syntax problem when using execute function?

I met an error when trying to use a function called execute to use one database. The information shows me that's about syntax, but I don't think so.
The following is my critical code and important information about the error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'u' at line 1
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include<string>
#include "mysql_connection.h"
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql;
int main()
{
try {
sql::mysql::MySQL_Driver driver;
cout<<" driver "<<endl;
sql::Connection *con = NULL;
cout<<" con"<<endl;
sql::Statement *stmt;
cout<<" stmt"<<endl;
sql::ResultSet *res;
cout<<" res"<<endl;
/* Create a connection */
//driver = sql::mysql::MySQL_Driver::MySQL_Driver();
SQLString ip_port("localhost:3306");
cout<<"some debug string"<<endl;
SQLString user("debian-sys-maint");
SQLString password("fTlykRye1LwttC8f");
con = driver.connect(ip_port, user, password);
assert(con!=NULL);
cout<<" root"<<endl;
/* Connect to the MySQL test database */
SQLString schema("account");
//con->setSchema(schema);
cout<<" table"<<endl;
stmt = con->createStatement();
cout<<" table*2"<<endl;
assert(stmt!=NULL);
SQLString db("use account");
stmt->execute(db);
res = stmt->executeQuery("SELECT * from user");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("name") << endl;
cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
cout << res->getString("password") << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "<<endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
cout<<"\n";
}
cout << endl;
return EXIT_SUCCESS;
}
I was confused that why the information was not about the sentence "use account". Instead, it was about "u". Thanks very much.
USER is a reserved word/keyword in MySQL.
You should delimit your field names with backticks to avoid problems:
SELECT * FROM `user`

d aGetting error while connecting to Postgres via dev c++

I am trying to get connected to Postgres database via Dev-c++ (Windows application and not console) for executing queries, but I am continuously getting errors.
I went through the following link:
http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
and added the below code to that of mine:
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[])
{
try{
connection C("dbname=testdb user=postgres password=cohondob \
hostaddr=127.0.0.1 port=5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
C.disconnect ();
}catch (const std::exception &e){
cerr << e.what() << std::endl;
return 1;
}
}
But got error stating:
'No such file or directory
#include
Compilation terminated'
Please, Can anyone help me get through this?
is there any other possible way to get connected?