PDA

View Full Version : Create firebird database programmatically with Qt



guidupas
28th January 2015, 19:39
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.

ChrisW67
28th January 2015, 21:30
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?

d_stranz
29th January 2015, 02:32
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
19th February 2015, 15:09
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.

ChrisW67
19th February 2015, 21:27
"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, 22:07
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?

jefftee
20th February 2015, 00:06
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, 17:27
Thanks jthomps, I am going to try it.

Thanks to d_stranz too. I will take a look at the firebird ODBC driver too.

jefftee
20th February 2015, 20:00
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.

Lesiok
21st February 2015, 10:05
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.
Possible only when the application is running on a database server. And what if the database server is another computer and You are connecting to them with TCP/IP?

jefftee
22nd February 2015, 05:52
Possible only when the application is running on a database server.

Lesiok, wouldn't this approach work also for an embedded Firebird database?

Lesiok
22nd February 2015, 13:05
With embedded database condition "application is running on a database server" is true of course.

guidupas
23rd February 2015, 19:26
Guys, when I try to connect with this code:



bool DB::conectarServidorFirebird(QSqlDatabase db)
{
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;
}


It returns me the error:

QSqlError("-901", "Error opening database", "Can't access lock files' directory /tmp/firebird")

jefftee
23rd February 2015, 22:22
It returns me the error:

QSqlError("-901", "Error opening database", "Can't access lock files' directory /tmp/firebird")
What are the permissions on your /tmp/ and /tmp/firebird directories? The user you are running with will require write access to the /tmp and /tmp/firebird directories.

ravelsk
22nd August 2016, 18:08
I find this on www an i hope, that this help You and others:


bool CreateDB(const QString& filePath, const QString& userName, const QString& password)
{
if (QFile::exists(filePath))
{
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;
}

Of course You must have Firebird installed and into Your project You must add
include ibase.h
and
library fbclient

Regards.