QSqlDatabase not opening - qt

I am unable to understand what I am doing wrong here. I am using Qt 5.7.1 and the code is as follows:
QString filePath = QCoreApplication::applicationDirPath();
QString dbPath = QDir(filePath).absoluteFilePath("../../../Database");
m_db = QSqlDatabase::addDatabase("QSQLITE", "user_connection");
m_db.setDatabaseName(dbPath + "/Sensor_Objects.db");
qDebug() << filePath << " & " << dbPath;
if (!m_db.open())
qDebug() << "Database Error: " + m_db.lastError().text();
else
{
qDebug() << "Database: connection ok";
createDatabase("Sensor_Objects");
m_db.close();
}
qDebug() is printing the paths to the directories correctly and yet m_db.open() fails with the error "Database Error: out of memory Error opening database".

Probably the point is dbpath,try with
m_db.setDatabaseName(dbPath.toLatin1() + "/Sensor_Objects.db");

Related

SQLITE c++ in Visual Studio

#include <iostream>
#include <SQLiteCpp/SQLiteCpp.h>
#include <SQLiteCpp/VariadicBind.h>
int main() {
std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl;
std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl;
try {
SQLite::Database db("my.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
}
}
On debugging it shows the error saying "Unable to start the program 'C:\SQLITE\SQLITE\x64\Debug\SQLite.exe' - The system cannot find the file specified"
I am not sure what to do in this case.
I am trying to create a sql database and create a table on it and make a relation table out of the tables that I will be creating. But I am not able to create a database due to this issue and cannot move forward with my task

QSQLITE driver not loaded

I'm creating a light APP with QT that add entries into my SQLITE base from my website.
But I've this problem :
("QSQLITE")
QSqlError(-1, "", "")
DATABASE OPENED
QSqlQuery::prepare: database not open
QSqlError(-1, "Driver not loaded", "Driver not loaded")
I'm on a Bananapi (Debian 7) with QT installed and upgrade from Debian-APT.
This is my code :
// Load QSLite driver
qDebug() << bdd.drivers(); //Said : ("QSQLITE")
bdd = QSqlDatabase::addDatabase("QSQLITE");
qDebug() << bdd.lastError();
// Open the database
bdd.setDatabaseName("/var/www/config/database.sqlite");
// Connect to the database
if( ! bdd.open())
{
qDebug() << "ERROR " << EXIT_FAILURE_CANT_OPEN_DATABASE << " : Can't open database";
qDebug() << bdd.lastError();
exit(EXIT_FAILURE_CANT_OPEN_DATABASE);
}
else
qDebug() << "DATABASE OPENED";
query.prepare(....); //Said : QSqlQuery::prepare: database not open.
Any idea ?

QNetworkAccessManager request fail (CA signed certificate)

I've got a CA signed certificate which I use for a webservices. In Chrome and Firefox everything work fine.
Now I want to write a QT-client for the webservice. But all what I get is "connection closed" after 30 seconds. If I request "https://gmail.com" or "https://www.dfn.de/" I get a proper result.
Here is my code.
void Request::send() {
QUrl url("my url");
qDebug() << "URL: " << url;
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
QObject::connect(manager, &QNetworkAccessManager::authenticationRequired, this, &Request::provideAuthenication);
QObject::connect(manager, &QNetworkAccessManager::finished, this , &Request::replyFinished);
QObject::connect(manager, &QNetworkAccessManager::sslErrors, this , &Request::sslErrors);
qDebug() << "fire request";
manager->get(request);
}
void Request::provideAuthenication(QNetworkReply *, QAuthenticator *ator) {
qDebug() << "provideAuthenication";
ator->setUser("***");
ator->setPassword("***");
}
void Request::replyFinished(QNetworkReply *reply) {
if (reply->error() != QNetworkReply::NoError)
qDebug() << "Network Error: " << reply->errorString();
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );
QVariant statusPhrase = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute );
qDebug() << "Result: " << statusCode.toInt() << " " << statusPhrase.toString();
qDebug() << "Data: " << reply->readAll();
}
void Request::sslErrors(QNetworkReply *, const QList<QSslError> &errors) {
foreach (const QSslError &error, errors) {
qDebug() << "SSL Error: " << error.errorString();
}
}
And that is the output. No sslError! No HTTP Error!
URL: QUrl( "my url" )
Network Error: "Connection closed"
Result: 0
Data: ""
So why hangs QT or the server? Did I miss something?!
It was a misconfiguration of openjdk7 (glassfish).
The hint of Aldaviva works on serverfault for me. https://serverfault.com/questions/389197/ssl-routinesssl23-writessl-handshake-failure
To bad that QT do not throw an ssl-error.

Sqlite3 network oddity

I have a simple QT program to read an sqlite3 database. Annoyingly, my program failed when the actual database is on the network as a mapped drive (Windows 7) but works when the database is on the local drive. The code I'm using is below and just for experimental purposes.
QFileInfo fileInfo(m_lastDatabaseFile);
QString host = fileInfo.canonicalPath();
QString filename = fileInfo.fileName();
m_dataBase = QSqlDatabase::addDatabase("QSQLITE");
m_dataBase.setHostName(host);
m_dataBase.setDatabaseName(filename);
qDebug() << " valid: " << m_dataBase.isValid();
if (m_dataBase.open() )
{
QStringList tableList = m_dataBase.tables(QSql::Tables);
qDebug() << "Database open.";
QSqlQuery query = m_dataBase.exec("SELECT * FROM PallolBoards");
qDebug() << "Found: " << query.size();
if (query.size() <= 0)
{
QSqlError err = query.lastError();
qDebug() << "Last error: " << err.text();
}
while (query.next() )
{
QString title = query.value(0).toString();
qDebug() << "Query: " << title;
}
}
else
{
QMessageBox::critical(this,"Invalid database",m_dataBase.lastError().text());
}
Any clues anyone? So H: will fail but D: succeeds.
At least in windows vista there was a security setting controlling if an application was allowed to access network shares or not.
There is also a dependency between Internet security settings(accessible through e.g. internet explorer) and access to network shares.

File copy operation does not work in a separate thread?

I am trying to run code of copying files in other thread so that it may not freeze the GUI of the application.
I found that it does not seem to work in a separate thread.
Why is it not working ?
void CopyOperation::run()
{
CopyFilesToFolder(list,sFolder);
}
bool CopyOperation::CopyFilesToFolder(const QStringList &oFileList,const QString
&sTargetFolder)
{
if(sTargetFolder.isEmpty())
{
status = false;
return false;
}
QDir dir(sTargetFolder);
if(!dir.exists()) dir.mkdir(sTargetFolder);
QString sOldDirPath = dir.currentPath();
//if(!dir.setCurrent(sTargetFolder)) return false;
QFile file;
status = true;
foreach(QString sFileName,oFileList)
{
file.setFileName(sFileName);
QFileInfo info(sFileName);
QString newfile = sTargetFolder + "/" + info.fileName();
qDebug() << "\n name = " << newfile;
if(!QFile::copy(sFileName,newfile))
//if(!file.copy(newfile))
{
qDebug() << "\n File copy failed .. " + file.fileName() + " Error : " + file.errorString();
status = false;
break;
}
}
qDebug() << "\n Result .. " << file.errorString() << "code " << file.error();
//dir.setCurrent(sOldDirPath);
return status;
}
Since you didn't post code, I just can try to guess what is the problem. Qt has a static function:
bool copy ( const QString & fileName, const QString & newName )
There is also a copy which is not static:
bool copy ( const QString & newName )
Both of them will fail if file defined by newName already exists, ie. existing file will not be overwritten. Also, maybe path doesn't exists. Without some portion of code is difficult to guess what is the problem.

Resources