Interbase & Firebird : Wrapper Problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

@thanos

I just read this thread again and noticed you had exactly the same problem with the wrapper from Progi1984, even though he said he could connect with it.

fbclient.dll is dependent on some of the msvc*.dll run-time libraries, try running the IBPhoenix Firebird installer again and select 'minimum Client Install' as the installation type (you'll have to stop your server before the installer will run).

I understand these libraries are included in more recent versions of Windows and also I believe they are updated by Windows updates, but if you're running an older version and you've got updates switched off that may be the problem.
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:Are you on x64 thanos?

My curiosity got the better of me so I installed the PB 4.30 beta and the code still works correctly for me.
No i am not running on x64.
But, today i have run the code into 2 other computers with XP Home Sp3 and XP Prof Sp3. For this purpose i installed the Purebasic 4.20.
Then code worked perfectly on both of them!
Unfortunately, something is going wrong with my system.
I will uninstall and install again the FireBird, odbc drivers etc.
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

thanos wrote:today i have run the code into 2 other computers with XP Home Sp3 and XP Prof Sp3. For this purpose i installed the Purebasic 4.20.
Then code worked perfectly on both of them!
Phew!
thanos wrote:Unfortunately, something is going wrong with my system
I think it's this:
the.weavster wrote:fbclient.dll is dependent on some of the msvc*.dll run-time libraries, try running the IBPhoenix Firebird installer again and select 'minimum Client Install' as the installation type (you'll have to stop your server before the installer will run).
Fingers crossed :)
Weave
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:I think it's this:
the.weavster wrote:fbclient.dll is dependent on some of the msvc*.dll run-time libraries, try running the IBPhoenix Firebird installer again and select 'minimum Client Install' as the installation type (you'll have to stop your server before the installer will run).
It did not work :cry:
Tomorrow i will reinstall all the odbc drivers and database servers and apps (SQLite, MSDE, mySQL, FireBird etc.)
Thank you for your interest!
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

I googled '+firebird +335544375' and found that error code means 'unavailable database'
http://agritecsoft.com/kb/en/por/errors.htm

Hopefully a full reinstall will do the trick.
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

I think I've finally realised the problem.

When you reinstalled the client libraries did you remember to copy fbclient.dll to the directory you were running the code from? If you used the default installation path you'll find it in C:\Program Files\Firebird\Firebird_2_1\bin\

With Firebird v2.0 a new function, fb_interpret(), was introduced for interpreting the Firebird error messages. The original function, isc_interprete(), was deprecated as it was susceptible to buffer overruns (it has left in the library for backwards compatibility though).

I didn't start learning the interbase api until after v2.0 so this fact hadn't stuck in my memory, I just suddenly remembered seeing it at some point when reading release notes.

My code is calling the new version of the error function but I suspect that function is not in the fbclient.dll you are using and that's why you're getting the invalid memory access error.

When I post more code I will add a change that checks for the new version of the function and if it's not there to fall back on the old one.
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:I think I've finally realised the problem.

When you reinstalled the client libraries did you remember to copy fbclient.dll to the directory you were running the code from? If you used the default installation path you'll find it in C:\Program Files\Firebird\Firebird_2_1\bin\
Yes. When i reinstalled the new library i copied the fbclient.dll into my application's folder.
But, i took the same bad results.
Something is going wrong with my system.
I started to uninstall and install again all the database servers and programs.
I'm waiting for your new code.
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

Try this:

Code: Select all

#FB_Client = 1
Global sLibrary.s
Global EndOfLine.s
Global lErrorFunction
Global fb_LastErrorMessage.s

;data type constants
#SQL_ARRAY = 540
#SQL_BLOB = 520
#SQL_DATE = 510
#SQL_DOUBLE = 480
#SQL_D_FLOAT = 530
#SQL_FLOAT = 482
#SQL_INT64 = 580
#SQL_LONG = 496
#SQL_QUAD = 550
#SQL_SHORT = 500
#SQL_TEXT = 452
#SQL_TIMESTAMP = 510
#SQL_TYPE_DATE = 570
#SQL_TYPE_TIME = 560
#SQL_VARYING = 448

;some other required constants
;database parameter buffer constants
#isc_dpb_version1 = 1
#isc_dpb_user_name = 28
#isc_dpb_password = 29
#isc_dpb_sql_dialect = 63
;transaction parameter buffer constants
#isc_tpb_concurrency = 2
#isc_tpb_version3 = 3
#isc_tpb_wait = 6
#isc_tpb_write = 9

#DSQL_CLOSE = 1
#DSQL_DROP = 2
#SQLDA_VERSION1 = 1
#SQL_DIALECT_V5 = 1
#SQL_DIALECT_V6 = 3

Structure ISC_STATUS ;used for retrieveing error messages
  vector.i[20]
EndStructure

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    sLibrary.s = "fbclient.dll"
    EndOfLine = Chr(13) + Chr(10)
  CompilerCase #PB_OS_Linux
    sLibrary.s = "libfbclient.so"
    EndOfLine = Chr(10) ;need to check this out
  CompilerCase #PB_OS_MacOS
    sLibrary.s = "Firebird.framework"
    EndOfLine = Chr(10) ;need to check this out
CompilerEndSelect

Prototype.i isc_attach_database(*StatusVector,dbNameLength.w,*dbName,*dbHandle,DPBSize.w,*DPB)
Global fb_attach.isc_attach_database

Prototype.i isc_detach_database(*StatusVector,*dbHandle)
Global fb_detach.isc_detach_database

PrototypeC.i isc_start_transaction(*StatusVector,*TransactionHandle,Count.w,*dbHandle,TPBLength.w,*TPB)
Global fb_start.isc_start_transaction

Prototype.i isc_rollback_transaction(*StatusVector,*TransactionHandle)
Global fb_rollback.isc_rollback_transaction

Prototype.i isc_commit_transaction(*StatusVector,*TransactionHandle)
Global fb_commit.isc_commit_transaction

Prototype.i isc_dsql_alloc_statement2(*StatusVector,*dbHandle,*QueryHandle)
Global fb_allocate.isc_dsql_alloc_statement2

Prototype.i isc_dsql_prepare(*StatusVector,*TransactionHandle,*QueryHandle,SQLlength.w,*SQLstring,Dialect.w,*XSQLDA)
Global fb_prepare.isc_dsql_prepare

Prototype.i isc_dsql_describe(*StatusVector,*QueryHandle,da_version.w,*XSQLDA)
Global fb_describe.isc_dsql_describe

Prototype.i fb_interpret_old(*Buffer,*PtrToStatusVector)
Global fb_error_old.fb_interpret_old

Prototype.i fb_interpret(*Buffer,SizeOfBuffer.i,*PtrToStatusVector)
Global fb_error.fb_interpret

Prototype.i isc_sqlcode(*StatusVector)
Global fb_sqlcode.isc_sqlcode

Prototype.i isc_sql_interpret(SQLCode.w,*Buffer,SizeOfBuffer.w)
Global fb_sql_error.isc_sql_interpret

Prototype.i isc_dsql_fetch(*StatusVector,*QueryHandle,da_version.w,*XSQLDA)
Global fb_fetch.isc_dsql_fetch

Prototype.i isc_dsql_free_statement(*StatusVector,*QueryHandle,free_option.w)
Global fb_free.isc_dsql_free_statement

Prototype.i isc_dsql_execute_immediate(*StatusVector,*dbHandle,*TransactionHandle,SQL_length.w,*SQLstring,Dialect.w,*XSQLDA)
Global fb_execute_immediate.isc_dsql_execute_immediate

Prototype.i isc_dsql_execute(*StatusVector,*TransactionHandle,*QueryHandle,da_version.w,*XSQLDA)
Global fb_execute.isc_dsql_execute

If OpenLibrary(#FB_Client,sLibrary)
  fb_attach = GetFunction(#FB_Client,"isc_attach_database")
  fb_detach = GetFunction(#FB_Client,"isc_detach_database")
  fb_start = GetFunction(#FB_Client,"isc_start_transaction")
  fb_rollback = GetFunction(#FB_Client,"isc_rollback_transaction")
  fb_commit = GetFunction(#FB_Client,"isc_commit_transaction")
  fb_allocate = GetFunction(#FB_Client,"isc_dsql_alloc_statement2")
  fb_sqlcode = GetFunction(#FB_Client,"isc_sqlcode")
  fb_sql_error = GetFunction(#FB_Client,"isc_sql_interprete")
  fb_fetch = GetFunction(#FB_Client,"isc_dsql_fetch")
  fb_free = GetFunction(#FB_Client,"isc_dsql_free_statement")
  fb_execute_immediate = GetFunction(#FB_Client,"isc_dsql_execute_immediate")
  fb_execute = GetFunction(#FB_Client,"isc_dsql_execute")
  fb_prepare = GetFunction(#FB_Client,"isc_dsql_prepare")
  fb_describe = GetFunction(#FB_Client,"isc_dsql_describe")  
  If GetFunction(#FB_Client,"fb_interpret") = 0 ;the new error function isn't in the library
    lErrorFunction = 0 ;use the old error function
    fb_error_old = GetFunction(#FB_Client,"isc_interprete")    
  Else
    lErrorFunction = 1 ;use the new error function
    fb_error = GetFunction(#FB_Client,"fb_interpret")
  EndIf  
Else
  MessageRequester("Error","Library not found",0)
  End
EndIf

Procedure fb_InterpretDatabaseError(*sv)
  fb_LastErrorMessage = ""
  lSQLCode.w = fb_sqlcode(*sv)
  If lSQLCode <> 0
    fb_LastErrorMessage = "SQL Code: " + Str(lSQLCode)
    *Mem = AllocateMemory(1000)
    fb_sql_error(lSQLCode,*Mem,1000)

    fb_LastErrorMessage = fb_LastErrorMessage + EndOfLine + PeekS(*Mem,1000,#PB_UTF8)
    FreeMemory(*Mem)
  EndIf
  If fb_LastErrorMessage <> "":fb_LastErrorMessage = fb_LastErrorMessage + EndOfLine + EndOfLine:EndIf
  lresult = 1
  If lErrorFunction = 0 ;the old error function
    While lresult <> 0
      *Mem = AllocateMemory(1000)
      lresult = fb_error_old(*Mem,@*sv)
      fb_LastErrorMessage = fb_LastErrorMessage + PeekS(*Mem,1000,#PB_UTF8)
      FreeMemory(*Mem)
    Wend  
  Else ;the new error function
    While lresult <> 0
      *Mem = AllocateMemory(1000)
      lresult = fb_error(*Mem,1000,@*sv)
      fb_LastErrorMessage = fb_LastErrorMessage + PeekS(*Mem,1000,#PB_UTF8)
      FreeMemory(*Mem)
    Wend
  EndIf
EndProcedure

Procedure.i fb_OpenDatabase(DatabaseAlias.s,Username.s,Password.s)
  lAlias.w = StringByteLength(DatabaseAlias,#PB_UTF8)
  *dbAlias = AllocateMemory(lAlias)
  PokeS(*dbAlias,DatabaseAlias,lAlias,#PB_UTF8)
  sParam.s = Chr(#isc_dpb_version1)
  lUN.w = StringByteLength(Username,#PB_UTF8)
  If lUN > 0:sParam = sParam + Chr(#isc_dpb_user_name) + Chr(lUN) + Username:EndIf
  lPWD.w = StringByteLength(Password,#PB_UTF8)
  If lPWD > 0:sParam = sParam + Chr(#isc_dpb_password) + Chr(lPWD) + Password:EndIf
  lUN = StringByteLength(sParam,#PB_UTF8)
  *dbParam = AllocateMemory(lUN)
  PokeS(*dbParam,sParam,lUN,#PB_UTF8)
  lHandle = #Null
  lresult = fb_attach(@sv.ISC_STATUS,lAlias,*dbAlias,@lHandle,lUN,*dbParam)
  FreeMemory(*dbAlias)
  FreeMemory(*dbParam)
  If lresult = 0
    ProcedureReturn lHandle
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn -1
  EndIf
EndProcedure

Procedure.i fb_CloseDatabase(dbHandle)
  lresult = fb_detach(@sv.ISC_STATUS,@dbHandle)
  If lresult = 0
    ProcedureReturn lresult
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn -1
  EndIf
EndProcedure

Procedure.i fb_StartTransaction(dbHandle)
  *TPB = AllocateMemory(4)
  PokeB(*TPB,#isc_tpb_version3)
  PokeB(*TPB + 1,#isc_tpb_write)
  PokeB(*TPB + 2,#isc_tpb_concurrency)
  PokeB(*TPB + 3,#isc_tpb_wait)
  TransactionID = #Null
  lResult = fb_start(@sv.ISC_STATUS,@TransactionID,1,@dbHandle,4,*TPB)
  FreeMemory(*TPB)
  If lResult = 0
    ProcedureReturn TransactionID
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn -1   
  EndIf
EndProcedure

Procedure.i fb_CommitTransaction(TransactionID)
  lresult = fb_commit(@sv.ISC_STATUS,@TransactionID)
  If lresult = 0
    ProcedureReturn lresult
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn -1
  EndIf
EndProcedure

Procedure.i fb_RollbackTransaction(TransactionID)
  lresult = fb_rollback(@sv.ISC_STATUS,@TransactionID)
  If lresult = 0
    ProcedureReturn lresult
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn -1
  EndIf
EndProcedure

Procedure.i fb_SQLExecute(dbHandle,TransactionID,SQLString.s)
  lSQL = StringByteLength(SQLString,#PB_UTF8)
  *SQL = AllocateMemory(lSQL)
  PokeS(*SQL,SQLString,lSQL,#PB_UTF8)
  lResult = fb_execute_immediate(@sv.ISC_STATUS,@dbHandle,@TransactionID,lSQL,*SQL,#SQL_DIALECT_V6,#Null)
  FreeMemory(*SQL)
  If lResult = 0
    ProcedureReturn #True
  Else
    fb_InterpretDatabaseError(@sv)
    ProcedureReturn #False
  EndIf
EndProcedure

;start the test
;Employees is an alias I've set up for the Employees example database that installs with Firebird

db = fb_OpenDatabase("Employees","sysdba","masterkey")
If db = -1
  MessageRequester("Error",fb_LastErrormessage,0)
  End
Else
  MessageRequester("Cool #1!","Connected - handle: " + Str(db),0)
  lTransID = fb_StartTransaction(db)
  If lTransID = -1
    MessageRequester("Error",fb_LastErrormessage,0)
  Else
    MessageRequester("Cool #2!","Started a transaction - handle: " + Str(lTransID),0)
    If fb_SQLExecute(db,lTransID,"UPDATE COUNTRY SET CURRENCY = 'Euros' WHERE COUNTRY = 'France';")
      fb_CommitTransaction(lTransID)
      MessageRequester("Cool #3!","Record updated",0)
    Else
      fb_RollbackTransaction(lTransID)
      MessageRequester("Error",fb_LastErrormessage,0)
    EndIf
  EndIf
  fb_CloseDatabase(dbHandle)
EndIf

CloseLibrary(#FB_Client)
If fb_interpret() isn't present in the library it should load isc_interprete() instead.
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:Try this:
:D
We have improvment!
The program not crashed anymore. Thank you!
But, returns the following error:
SQL Code: -907
Unsuccessful execution caused by an unavailable resource.

unavailable database

Please mention that i can read and edit those databases with FlameRobin and VB program.
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

If your login code is this:

Code: Select all

db.l = fb_OpenDatabase("DB_FIREBIRD.FDB", "sysdba", "masterkey")
I would suggest the problem is simply that the server doesn't know where to find your database (there is no path stated).

It's really much easier to set up an alias in aliases.conf, then clients can simply refer to the database by that alias.
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:If your login code is this:

Code: Select all

db.l = fb_OpenDatabase("DB_FIREBIRD.FDB", "sysdba", "masterkey")
I would suggest the problem is simply that the server doesn't know where to find your database (there is no path stated).

It's really much easier to set up an alias in aliases.conf, then clients can simply refer to the database by that alias.
I tried three methods to connect:
1.

Code: Select all

fb_OpenDatabase("Evolution.fdb", "sysdba", "masterkey")
The Evolution.fdb were in the same folder with application.

2.

Code: Select all

fb_OpenDatabase("F:\My-Applications\Manager\Evolution.fdb", "sysdba", "masterkey")
Full path for the Evolution.fdb

3.

Code: Select all

fb_OpenDatabase("Evolution", "sysdba", "masterkey")
The Evolution had declared in the aliases.conf.

Contents of aliases.conf:

#
# List of known database aliases
# ------------------------------
#
# Examples:
#
# dummy = c:\data\dummy.fdb
#

Evolution = F:\My-Applications\Manager\Evolution.fdb


I took the same error -904 with the 3 methods
Regards.
Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

What is the installation path of the server? The database files have to be on a hard disk that is physically attached to your server machine, shares or mapped drives will not work.
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:What is the installation path of the server? The database files have to be on a hard disk that is physically attached to your server machine, shares or mapped drives will not work.
Server path:
C:\Program Files\Firebird\Firebird_2_1\bin
Database file:
F:\My-Applications\Manager
which is a partition of the hard disk.
No map.
Regards

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Post by thanos »

the.weavster wrote:What is the installation path of the server? The database files have to be on a hard disk that is physically attached to your server machine, shares or mapped drives will not work.
Server path:
C:\Program Files\Firebird\Firebird_2_1\bin
Database file:
F:\My-Applications\Manager
which is a partition of the hard disk.
No map.
Please mention that i can connect to the database via my VB application using the same odbc driver.
Regards

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

thanos wrote:Please mention that i can connect to the database via my VB application
Maybe it's connecting by network loopback, try using a TCP/IP connection string rather than a local connection string.

e.g. instead of:

Code: Select all

fb_OpenDatabase("F:\My-Applications\Manager\Evolution.fdb", "sysdba", "masterkey")
use:

Code: Select all

fb_OpenDatabase("localhost:F:\My-Applications\Manager\Evolution.fdb", "sysdba", "masterkey")
thanos wrote:using the same odbc driver
Note, you don't need the ODBC driver installed to connect when directly using fbclient.dll
Post Reply