开发者

using Qt Stored procedure in Firebird

Good Afternoon, I'm starting in the world of Qt and a Firebird database.And finish the driver installation process and perform operations onthe database insert, update and consultation. When I started to make stored procedures and run them from Qt did not work. Does not fail and always us that everything was made ​​perfect, but the database does not run. I am programming in Linux using Qt 2.0.1 and Firebird 2.1 I create a simple stored procedure test which makes it an insert into a table. It works by running the console but when trying to run fromQt does not work and gives me no errors.The SQL code is:

SET TERM ^ ;CREATE PROCEDURE AGREEGAR_UNO AS BEGIN insert into JUEGO(CODIGO,ESCRUTINIO,ESTADO,FECHA,HORAINICIO) values (next value for GNECODIGOJUEGO,'111,123,154,169,178','Hi', current_date, current_time);END^SET TERM ; ^
GRANT EXECUTE ON PROCEDURE AGREEGAR_UNO TO SYSDBA;

The following code will use to connect to firebird from Qt

bool VentanaPrueba::conectar()
{
this->db= QSqlDatabase::addDatabase("QIBASE","Data");
this->db.setDatabaseName("./BD/Data.fdb");
this->db.setPassword("password");
this->db.setUserName("SYSDBA");
if(!db.open())
{
return false;
}
else
return true;
}

And this is the code that is responsible for calling the procedure

void VentanaPrueba::procedimiento()
{
if (!this->db.isOpen()) this->conectar();
if(this->db.isOpen())
{ QSqlQuery procedimiento = QSqlQuery::QSqlQuery(this->db);
bool bandera = procedimiento.开发者_如何学Cprepare("EXECUTE PROCEDURE AGREEGAR_UNO");
QString err = procedimiento.lastError().text();
bool respuesta= procedimiento.exec();
//this->db.commit();
if(!respuesta)
{
this->db.close();
}else
{
procedimiento.finish();
this->db.commit();
this->db.close();
}


}else{
//error
}


}

Thank you very much for your help.


IBPP or InterBase Plus Plus is a client interface project which connects for Qt with Interbase database system like firebird. Using it you can easily use your firebird database. Download its source code package from http://sourceforge.net/projects/ibpp/files/ or ibpp official site, and unpack. Copy the core folder into your project folder. And add these files in your Qt project. Read complete guide at https://rongsheng007.wordpress.com/2010/08/23/make-qt-applications-work-with-firebird-database/


I can't tell if this works for firebird, but you could try the following:

procedimiento.prepare("BEGIN EXECUTE PROCEDURE AGREEGAR_UNO; END;");

This is similar to the PL/SQL passage

BEGIN
  EXECUTE PROCEDURE AGREEGAR_UNO;
END;

I am accessing Oracle databases with Qt and had to include the "BEGIN" and "END;" commands there. You don't have the "EXECUTE PROCEDURE" in oracle, but it might be required for Firebird.
Also, I am using Qt 4 so there might be a difference as well.


There is a simpler way, even though little bit weird.

Create a stored procedure in firebird with some output variable to suspend, and a VARCHAR(1024) input variable to pass the procedure call.

And call it in Qt with procedure call as a string parameter.

       SET TERM ^ ;
create PROCEDURE SP_EXECUTE (STMNT varchar(1024) )
    RETURNS (
        INRETURN integer )
    AS
    BEGIN
        execute statement stmnt;
    inReturn=1;
    suspend;
    END^
    SET TERM ; ^
    }

Then in Qt,

procedimiento.prepare("SELECT INRETURN FROM SP_EXECUTE('EXECUTE PROCEDURE AGREEGAR_UNO')");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜