Recent

Author Topic: Extracting the contents of a record's field.  (Read 11482 times)

cov

  • Full Member
  • ***
  • Posts: 246
Extracting the contents of a record's field.
« on: April 11, 2013, 06:52:49 pm »
Sorry for dominating the forum!

How do I get values from a table?

If I have a DBGrid displaying the result of 'SELECT * FROM FIELDS;', all the entries in the FIELDS table.

If the User double-clicks on a row, I can get the ID of the record selected by id:=DBGrid1.DataSource.DataSet.Fields[0].Value; <- the ID field is the first column declared.

How do I get the Path of the document from the PATHS table whose record ID corresponds to the record ID obtained from the FIELDS table above?

Do I need to drop another TQuery onto the form? I obviously don't want to change the contents of the DBGrid, which surely would happen if I use the SQLQuery1 component again?

Presumably I would use 'SELECT PATH FROM PATHS WHERE ID='+strtoint(id)+';';

But how do I access the document path?

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Extracting the contents of a record's field.
« Reply #1 on: April 11, 2013, 10:05:24 pm »
If the PATH field is displayed by the DBGrid then clicking should give you the column index which should correspond to the field index.

Click on column 3 then the value should be at DBGrid1.DataSource.DataSet.Fields[2].Value

As for the 'WHERE ID= ...' suggest you look into parameter queries.
http://wiki.freepascal.org/SqlDBHowto#How_to_use_parameters_in_a_query.3F

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #2 on: April 11, 2013, 10:24:06 pm »
Hi, GN, thanks for the response.

No, The PATHS table is different from the FIELDS table which is displayed in the DBGrid and which is constructed from the query 'SELECT * FROM FIELDS;'.

I was aware of the technique of parametising the variables in a query, but thought it simpler, for the purposes of this enquiry, to present it as a concatenated string.

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Extracting the contents of a record's field.
« Reply #3 on: April 12, 2013, 01:06:18 am »
In that case a single query can join the two tables together.

SELECT fields.*, paths.*
FROM fields INNER JOIN fields.key=paths.id

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #4 on: April 12, 2013, 08:11:00 am »
I don't actually want the DBGrid to display the Document path.

I just want to read it so that I can access the document.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Extracting the contents of a record's field.
« Reply #5 on: April 12, 2013, 08:20:56 am »
I have trouble understanding your remarks...

Quote
I don't actually want the DBGrid to display the Document path.
Then don't display the document path i.e. don't include it in the query on which the databound control (e.g. grid)  is based!??!

Quote
I just want to read it so that I can access the document.
I'd second Goodname's suggestion about getting the path, but it may help if you show your actual data structure, or in other words, how do *you* think you'll get from the FIELDS table to the PATHs table?

Also, please read
http://wiki.lazarus.freepascal.org/SQLdb_Tutorial3
if you haven't already. It shows you how to read data from queries etc without displaying them.
« Last Edit: April 12, 2013, 08:23:34 am by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #6 on: April 12, 2013, 02:18:10 pm »
I have read all three Tutorials, numerous times.

I understand how SQL works and have previously written (admittedly not very complex) SQL queries for Postgres and MySQL.

I understand that the TSQLQuery has TIBConnection as it's Database component, and both TSQLQuery and TIBConnection are connected to the TSQLTransaction. The TDBGrid has the TDatasource as it's Datasource component, which, in turn, has it's Dataset component linked to TSQLQuery.

So, when the TSQLQuery runs an SQL query, the result is displayed in the TDBGrid.

In this case, the Query is 'SELECT * FROM FIELDS;' the result of which is correctly displayed in the TDBGrid.

I want to take the ID field from the Record selected from the FIELDS table and use it to get the document path from the PATH field in the PATHS table for the document, whose ID matches that ID.

I hope this is clearer.

As far as I can tell this is not addressed in tutorial one, nor in tutorials two or three, although I'm happy to be proven wrong.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Extracting the contents of a record's field.
« Reply #7 on: April 12, 2013, 02:24:11 pm »
Yep, thanks, that's certainly clearer.

No, the exact scenario is not described in the tuts.

However, once you have the ID field value, you can use the scenario in tutorial3 to get the path you are looking for by running SQL and putting the result in a variable.

As for how to get the ID field: you might be able to do it via the datasource (it keeps step with the db bound controls), or via the dbgrid (Goodname's suggestion could be adapted... it uses the datasource anyway...).

If not, the dbgrid docs should come in handy.

Edit: updated datasource info
« Last Edit: April 12, 2013, 02:31:15 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #8 on: April 12, 2013, 03:33:07 pm »
Ok, so if I use a different TSQLQuery, say SQLQuery2 on the form and connect it to IBConnection1 and run a query 'SELECT PATH WHERE ID=7654', how can I access the result of that query?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Extracting the contents of a record's field.
« Reply #9 on: April 12, 2013, 03:36:58 pm »
http://wiki.lazarus.freepascal.org/SQLdb_Tutorial3#Getting_database_data_into_normal_controls
Quote
It is also possible to programmatically retrieve database content and fill any kind of control (or variable) with that content.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #10 on: April 12, 2013, 04:43:12 pm »
Brilliant!

Just what I'm after! Thanks!

So I would need to instantiate a second TSQLQuery? I thought I would. That's fine!

Thanks again.

As ever, your help is much appreciated.

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #11 on: April 14, 2013, 10:11:33 am »
Just to add that a second TSQLQuery component does not stop the DBGrid from losing the results of the first SQLQuery1 query.

A second IBConnection component seems to be required.

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #12 on: April 14, 2013, 10:21:01 am »
Hmmm.

That doesn't work.

Using the second TSQLQuery2 when it's connected to IBConnection1 works, but it resets the DBGrid.

Connecting it to IBConnection2 doesn't work.

Code: [Select]
IBConnection2: PrepareStatement:
-invalid transaction handle (expecting explicit transaction start)

cov

  • Full Member
  • ***
  • Posts: 246
Re: Extracting the contents of a record's field.
« Reply #13 on: April 14, 2013, 10:26:18 am »
Ok.

I removed the second TIBConnection and pointed the SQLQuery2 back to IBConnection1.

I then just opened SQLQuery1 again and my DBGrid repopulated itself.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Extracting the contents of a record's field.
« Reply #14 on: April 14, 2013, 10:54:49 am »
Not a second connection a second transaction is required. Using a different transaction for different sql queries you can control when those queries get closed or not. The idea seems simple enough but the transaction isolation level makes things complicated. Try reading the Firebird/interbase documents on transaction isolation. Personally I use modified version of MDO (http://sourceforge.net/projects/mdo/?source=directory) that allows me to have different read and write transactions making easy to post updates with out closing the read transaction.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018