Lazarus

Programming => Databases => Topic started by: CapelliC on July 21, 2020, 04:07:22 pm

Title: [solved] help with ODBC visual components
Post by: CapelliC on July 21, 2020, 04:07:22 pm
I was trying the simplest possible setup for an ODBC editing GUI: in my form


Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     DataSource1: TDataSource;
  3.     DBGrid1: TDBGrid;
  4.     ODBCConnection1: TODBCConnection;
  5.     SQLQuery1: TSQLQuery;
  6.     SQLTransaction1: TSQLTransaction;
  7.  

In SQLQuery1 I've set the updateSQL statement, and the grid happily let me browse the dataset and edit the column I need to change, but when I call

SQLQuery1.ApplyChanges

I get an exception because the connection is busy with the results actually needed to populate the grid.

Have tried to circumvent the problem in several ways, but without luck so far.
Maybe I'm missing the key problem...
Title: Re: help with ODBC visual components
Post by: rvk on July 21, 2020, 04:12:30 pm
I get an exception because the connection is busy with the results actually needed to populate the grid.
What exception exactly?
You can always click the message and press CTRL+C to copy the message to the clipboard and paste it here.

How does your SQL look like. Normally the INSERT, UPDATE and DELETE SQLs are created automatically.
What is your TSQLQuery.UpdateMode set to?
Title: Re: help with ODBC visual components
Post by: CapelliC on July 21, 2020, 04:34:07 pm
Thanks for the quick reply. The exception message

Code: [Select]
[Debugger Exception Notification]

Project test_update_odbc raised exception class 'EUpdateError' with message:
An error occurred while applying the updates in a record: Could not execute statement. ODBC error details: LastReturnCode: SQL_ERROR; Record 1: SqlState: HY000; NativeError: 0; Message: [Microsoft][ODBC Driver 17 for SQL Server]La connessione � occupata dai risultati di un altro comando;

 At address 10020B434


[Ignore this exception type]

[Break] [Continue]

On SQLQuery1 I've set the properties SQL:
Code: MySQL  [Select][+][-]
  1.  D.nome_tipoazione AS Azione,
  2.  A.tipo AS IdTipo,
  3.  D.esecutore AS Esecutore,
  4.  A.N AS Tot_Azioni
  5. FROM my_table D,
  6.  (SELECT count(*) AS N, tipo FROM my_table_2 GROUP BY tipo) A
  7. WHERE D.numero_tipoazione=A.tipo
  8. ORDER BY A.tipo
  9.  
and updateSQL:
Code: MySQL  [Select][+][-]
  1. UPDATE my_table
  2.  SET esecutore=:Esecutore
  3.  WHERE numero_tipoazione=:IdTipo
  4.  

As I said, I've tried various visual properties as well as some programmatic ones, acting on SQLTransaction1. In the end, the only relevant snippet seems to be the

SQLQuery1.ApplyUpdates;

that I call - for instance - from a button click handler, but generates the exception.

The updateMode seems irrelevant, I've tried all 3 allowed values without luck.

Also, the database is SqlServer, not MySql. I've used that tag only to highlight the SQL code.
Title: Re: help with ODBC visual components
Post by: rvk on July 21, 2020, 04:42:07 pm
I'm not sure about the exception message.

But did you set the IdTipo parameter?

Anyway... it might be best to set the UPDATESQL to something more generic. Like

Code: SQL  [Select][+][-]
  1. UPDATE my_table
  2.  SET esecutore=:Esecutore
  3.  WHERE numero_tipoazione=:old_numero_tipoazione
The :OLD_ fieldnames are automatically generated from the record.

https://www.freepascal.org/docs-html/fcl/sqldb/tsqlquery.updatesql.html

If that doesn't work you could try
Code: SQL  [Select][+][-]
  1. UPDATE my_table
  2.  SET esecutore=:Esecutore
  3.  WHERE numero_tipoazione=:old_IdTipo
Title: Re: help with ODBC visual components
Post by: CapelliC on July 21, 2020, 05:20:07 pm
Have tried to simplify, just started another project and connected only the small table, so no joins or columns aliases, but the result doesn't change. I always get the exception because the connection is busy...
Title: Re: help with ODBC visual components
Post by: rvk on July 21, 2020, 05:37:12 pm
Have tried to simplify, just started another project and connected only the small table, so no joins or columns aliases, but the result doesn't change. I always get the exception because the connection is busy...
Does it work with a simple query and without setting UpdateSql?
(And setting UpdateMode to upWhereAll)

UpdateSql will be generated automatically for simple queries.
Title: Re: help with ODBC visual components
Post by: CapelliC on July 21, 2020, 05:48:44 pm
Does it work with a simple query and without setting UpdateSql?
(And setting UpdateMode to upWhereAll)
No, always the same exception about the connection being busy...
Title: Re: help with ODBC visual components
Post by: rvk on July 21, 2020, 05:52:23 pm
Can you post a reproducible example.
Did you handle/open transaction correctly?
Is insert the first statement after open?

Quote
"This error occurs when there are pending results on a statement handle
that is then used to execute another query. This causes a problem when
the ODBC data source is a SQL Server (Microsoft or Sybase) because,
owing to the architectural design, there can be only one active
statement per connection on an SQL Server."

Therefore the SQL Server ODBC driver (SQLSRVR.DLL) cannot allow multiple
active HSTMTs on a single connection handle or HDBC. An active statement
is defined as a statement that has pending results; that is, the whole
result set has not been read from the server. "
https://it.comp.java.narkive.com/mcG1LkR8/odbc-sql-server-driver-la-connessione-e-occupata-dai-risultati-di-un-altro-hstmt
Title: Re: help with ODBC visual components
Post by: CapelliC on July 21, 2020, 06:23:54 pm
A reproducible example requires of course SqlServer...
don't know if there is some public testbed instance out there.
Of course, our servers work with ODBC as well as other connections, as usual.
The root problem is - imho - that TSQLQuery *requires* a TSQLTransaction to fetch data.
Thanks for your help.

Title: Re: help with ODBC visual components
Post by: rvk on July 21, 2020, 06:54:45 pm
Yes, but someone here on the forum might have such server to test.
Or we might spot an error in your code.

Does your code work with a sqlite database?

The root problem is - imho - that TSQLQuery *requires* a TSQLTransaction to fetch data.
Does your code not have a transaction?

Even SQL server itself only works with transactions. You can't do anything without it.
Title: Re: help with ODBC visual components
Post by: CapelliC on July 22, 2020, 08:59:52 am
Does your code not have a transaction?
Even SQL server itself only works with transactions. You can't do anything without it.

Yes, it has a TSQLTransaction connected to a TODBCConnection, as I have shown in the first post,
and I've also tried various hacks to get around the problem, but failed...
Without it, there is no chance to get data from a TSQLQuery.

About SqlLite, I don't know if it will work... Does ODBC would be involved ? Otherwise I don't see the point...

So far, I've experienced bad feelings wrt SqlServer support, the only 'reliable' way I've found so far has been to *rewrite*
the thin client layer throu ADO (ComObj etc...). Now debugging memory leakage etc...
Title: Re: help with ODBC visual components
Post by: rvk on July 22, 2020, 09:29:01 am
So far, I've experienced bad feelings wrt SqlServer support, the only 'reliable' way I've found so far has been to *rewrite* the thin client layer throu ADO (ComObj etc...).
Why can't you use TMSSQLConnection in combination with the SQL server client?
Title: Re: help with ODBC visual components
Post by: CapelliC on July 22, 2020, 09:54:45 am
FreeTDS (that I used years ago on Unix to connect to SqlServer) requires a toolchain I cannot install on my servers. Moreover, dblib is not more available... and on production servers to download and install unknown components is something I don't can - and will - to do.
Title: Re: help with ODBC visual components
Post by: rvk on July 22, 2020, 10:07:06 am
Then the only thing is for someone to figure out why you are getting that error message with a simple SQL on ODBC with your code.
Title: Re: help with ODBC visual components
Post by: rvk on July 22, 2020, 01:46:35 pm
BTW, have you tried setting the TSQLQuery.PacketRecords := -1; ?
(as according to the readme)
Title: Re: help with ODBC visual components
Post by: CapelliC on July 22, 2020, 02:55:27 pm
BTW, have you tried setting the TSQLQuery.PacketRecords := -1; ?
(as according to the readme)

Wow, that worked. Thanks so much.

Sorry for not being able to find the readme you're referring. The documentation is a bit sparse :)
There are a lot of objects/interfaces to examine, meanwhile I had to solve other problems in my code not related to the usage of RAD components, I was a bit lost.

Generally Lazarus/FPC are working well, but when something start not working and I cannot overcome the problem... well, here I come, after a bit of sweat and pain...

Thank you again.
Title: Re: help with ODBC visual components
Post by: rvk on July 22, 2020, 03:19:17 pm
Sorry for not being able to find the readme you're referring. The documentation is a bit sparse :)
I don't blame you  :D
It's an obscure little line in the fpc\packages\fcl-db\src\sqldb\mssql\readme.txt

Quote
Known problems:
===============
- CHAR/VARCHAR data truncated to column length when encoding to UTF-8 (use NCHAR/NVARCHAR instead or CAST char/varchar to nchar/nvarchar)
- Multiple result sets (for example when SP returns more than 1 result set only 1st is processed)
- Output parameters of stored procedure are not returned. See FreeTDS FAQ: "I'm not getting my output parameters returned, but I seem to be doing everything right!"
- DB-Library error 10038 "Results Pending" - set TSQLQuery.PacketRecords=-1 to fetch all pendings rows
- BLOB data (IMAGE/TEXT columns) larger than 16MB are truncated to 16MB - (set TMSSQLConnection.Params: 'TEXTSIZE=2147483647' or execute 'SET TEXTSIZE 2147483647')
  (create temporary stored procedures for prepared statements)

It's also on the page for TMSSQLConnection (https://www.freepascal.org/docs-html/current/fcl/mssqlconn/tmssqlconnection.html).

Although you didn't use TMSSQLConnection directly, the error message "Results Pending" did seem a bit like the message you got "The connection is occupied by the results of another command" and the quote I gave earlier about "pending results on a statement handle". So it's seems a SQLServer thing that pending results need to be handled first.

Normally the number of records returned by the server is set by the PacketRecords property. The default value is 10; if you make it -1 then all records will be loaded at once. And in that case there are no pending results on the handle anymore.

Glad it's solved.
Title: Re: [solved] help with ODBC visual components
Post by: EgonHugeist on July 22, 2020, 06:35:16 pm
JFYI: Zeos 7.3 (upcomming 8.0) https://sourceforge.net/p/zeoslib/code-0/HEAD/tree/branches/testing-7.3/ (https://sourceforge.net/p/zeoslib/code-0/HEAD/tree/branches/testing-7.3/)
has a ODBC_W/ODBC_A/OleDB(Windows only) protocol, deeply tested and optimized for SQL-Server.
Title: Re: [solved] help with ODBC visual components
Post by: CapelliC on July 23, 2020, 08:41:18 am
JFYI: Zeos 7.3 (upcomming 8.0) https://sourceforge.net/p/zeoslib/code-0/HEAD/tree/branches/testing-7.3/ (https://sourceforge.net/p/zeoslib/code-0/HEAD/tree/branches/testing-7.3/)
has a ODBC_W/ODBC_A/OleDB(Windows only) protocol, deeply tested and optimized for SQL-Server.

Thanks, will try ASAP.
I've installed ZEOS a little ago (ver. 7.2.6.1), but stumbled into the same problems (FreeTDS or dblib required for connection) as with TMSSQLConnection.
Maybe will try ver. 8 as it come out, right now I can complete with my simple minded ADODB (ComObj based) shallow interface.
Title: Re: [solved] help with ODBC visual components
Post by: EgonHugeist on July 23, 2020, 08:58:57 am
right now I can complete with my simple minded ADODB (ComObj based) shallow interface.
That's why i'm writing, 7.2 already has a ADO protocol for FPC. So don't invest to much time. There are loads of incompatibilities with FPC and the OleVariants, ADO is using for data-transport(all of them are hooked by Zeos to get it running). Such as decimal values etc. Delphi has no such problems. The more the ADO bridge seem's less maintained by MS and field types like datetime2/time2 are returned as strings(OleVariant missue), whereas ODBC and OleDB can handle them gracefully. The more we've Pointed out using ADO is such an incredible performance loss in comparision to OleDB (4x faster).. JFYI, Michael
Title: Re: [solved] help with ODBC visual components
Post by: PascalDragon on July 23, 2020, 09:27:35 am
There are loads of incompatibilities with FPC and the OleVariants, ADO is using for data-transport(all of them are hooked by Zeos to get it running). Such as decimal values etc. Delphi has no such problems.

If there are incompatibilities in FPC that don't exist in Delphi, then please report them, so that they can be fixed.
Title: Re: [solved] help with ODBC visual components
Post by: CapelliC on July 23, 2020, 11:09:56 am
7.2 already has a ADO protocol for FPC. So don't invest to much time.
Yes, ADO support was the main reason I installed and attempted to use, but what protocol (from the listbox of TZConnection) I should use ?

mssql does require either FreeTDS or ntwdblib at transport layer, so I'm unable to connect.
ado seems to require ODBC - it's rather confusing, what to put into catalog, database, hostname, assuming the following information from a DSN file, that allows connection when put into FileDSN of a TODBCConnection

Code: Text  [Select][+][-]
  1. [ODBC]
  2. DRIVER=ODBC Driver 17 for SQL Server
  3. DATABASE=XXX_DB
  4. WSID=XXX_WS
  5. Trusted_Connection=Yes
  6. SERVER=XXX.YYY.ZZZ.UU
  7. Description=XXX_DESC
  8.  
Title: Re: [solved] help with ODBC visual components
Post by: EgonHugeist on July 23, 2020, 12:38:25 pm
There are loads of incompatibilities with FPC and the OleVariants, ADO is using for data-transport(all of them are hooked by Zeos to get it running). Such as decimal values etc. Delphi has no such problems.

If there are incompatibilities in FPC that don't exist in Delphi, then please report them, so that they can be fixed.
Well, since most of my and others helpfull reports are rejected as won't fix and it's forbitten to ask questions, i gave up doing that by now. The more we need backward compatibility for older FPC's so it's worth it cirumvent such problems by our self in some cases.  Of course it would be nice to omit "old" code in several years, honestly it would, so i agree with your POV.

@CapelliC
i'm using such a DataBase-String fo my tests:
ADO:
Quote
Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=zeoslib;Data Source=K95VM-Egon\SQLEXPRESS2012;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=EGONDEVLAPTOPW7;Use Encryption for Data=False;Tag with column collation when possible=False
OleDB:
Quote
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=zeoslib;Data Source=(localdb)\ProjectsV13;MarsConn=Yes;Trusted_Connection=Yes
ODBC:
Quote
DRIVER={SQL Server Native Client 11.0};Server=(localdb)\ProjectsV13;DataBase=zeoslib;Trusted_Connection=Yes;MARS_Connection=yes

Take attantion to the "MarsConn=Yes"(ODBC) parameter and "MARS_Connection=yes"(OleDB) if you ever test 7.3
Title: Re: [solved] help with ODBC visual components
Post by: PascalDragon on July 29, 2020, 01:34:58 pm
There are loads of incompatibilities with FPC and the OleVariants, ADO is using for data-transport(all of them are hooked by Zeos to get it running). Such as decimal values etc. Delphi has no such problems.

If there are incompatibilities in FPC that don't exist in Delphi, then please report them, so that they can be fixed.
Well, since most of my and others helpfull reports are rejected as won't fix and it's forbitten to ask questions, i gave up doing that by now. The more we need backward compatibility for older FPC's so it's worth it cirumvent such problems by our self in some cases.  Of course it would be nice to omit "old" code in several years, honestly it would, so i agree with your POV.

Do you have examples for such rejected reports?

Also discussion on the bugtracker is not desired. For discussion the mailing lists or the forum should be used.
TinyPortal © 2005-2018