Hello all!
I am trying to create a database directly from my code in Qt, but until now I did not have success, Can anyone help me with some article or some code?
Thanks.
Hello all!
I am trying to create a database directly from my code in Qt, but until now I did not have success, Can anyone help me with some article or some code?
Thanks.
Can you help us with any idea what you have tried and how it "did not have success"?
How are you trying to connect from Qt to Firebird?
The Firebird project offers an ODBC driver, so maybe you can connect to the database using Qt's ODBC driver instead. Make sure you have the Qt database plugins in the right place with respect to your executable, otherwise you won't be able to instantiate the driver.
guidupas (20th February 2015)
I am using the IBase driver and trying to do that with QSqlDriver, but it does not have the statement to create database.
I need to do it directly from the Qt code, like is done with manager softwares but the connection to database returns an error if I dont select a database to connect.
"Firebird doesn't provide a way to create database using SQL. You need to either use the Services API, or external tool. As API for database creation is often not available in libraries, you can call Firebird's isql tool to do it for you..."
http://www.firebirdfaq.org/faq67/
guidupas (19th February 2015)
Thanks for the reply ChrisW67, it helped a lot. But I am trying to figure out how to execute to isql via stdin. Could you help me with this doubt?
Use QProcess to run isql from your application. You can either use isql -i to read/execute SQL commands from a file or you can use QProcess and write commands to the stdin of the running QProcess using QProcess:write().
guidupas (20th February 2015)
Thanks jthomps, I am going to try it.
Thanks to d_stranz too. I will take a look at the firebird ODBC driver too.
One comment regarding your approach. Is it reasonable to expect your users to have isql installed or are you planning on shipping isql somewhere in your bundle?
Another thing you could consider is to ship an empty firebird database (created with isql but not objects created) with your app and copy that empty database the first time you start your app. You should be able to then create any tables, indexes, or views needed by your app, etc.
Good luck.
With embedded database condition "application is running on a database server" is true of course.
Guys, when I try to connect with this code:
Qt Code:
{ bool retorno = true; db.setHostName("localhost"); db.setPassword("gui080381"); db.setUserName("sysdba"); //db.setPort(3050); if(db.open()) { retorno = true; } else { qDebug() << db.lastError(); retorno = false; } return retorno; }To copy to clipboard, switch view to plain text mode
It returns me the error:
QSqlError("-901", "Error opening database", "Can't access lock files' directory /tmp/firebird")
I find this on www an i hope, that this help You and others:
Of course You must have Firebird installed and into Your project You must addQt Code:
bool CreateDB(const QString& filePath, const QString& userName, const QString& password) { { return false; } databasePath_ = filePath; QString queryString; queryString += "CREATE DATABASE"; queryString += " \'" + filePath + "\'"; queryString += " USER \'" + userName + "\'"; queryString += " PASSWORD \'" + password + "\'"; queryString += " DEFAULT CHARACTER SET UNICODE_FSS"; ISC_STATUS_ARRAY status; isc_db_handle databaseHandle = NULL; isc_tr_handle transactionHandle = NULL; unsigned short g_nFbDialect = SQL_DIALECT_V6; if (isc_dsql_execute_immediate(status, &databaseHandle, &transactionHandle, 0, queryString.toStdString().c_str (), g_nFbDialect, NULL)) { long SQLCODE=isc_sqlcode(status); return false; } isc_commit_transaction( status, &transactionHandle ); if (databaseHandle != NULL) { ISC_STATUS_ARRAY status; isc_detach_database(status, &databaseHandle); } return true; }To copy to clipboard, switch view to plain text mode
include ibase.h
and
library fbclient
Regards.
Last edited by ravelsk; 22nd August 2016 at 17:14.
Bookmarks