Recent

Author Topic: (solved) basic question-Tzquery  (Read 7142 times)

aliboy

  • New Member
  • *
  • Posts: 23
(solved) basic question-Tzquery
« on: November 24, 2012, 02:11:32 pm »
hi dear

i read this tutorial and  have a basic question about writing query.

http://wiki.freepascal.org/Zeos_tutorial


Quote
Drop a ZConnection.
Set your User, Password, Host, Port and Protocol (and any other params if needed).
Set Connected to True.
Drop a ZQuery (do not mistake with ZReadOnlyQuery).
Set the Connection to your active ZConnection.
Set the Sql property to something like SELECT * FROM MyTable
Set Active to True.
Drop a DataSource from the [Data Access] tab.
Set the DataSet to your active ZQuery.
Drop a DBGrid from the [Data Controls] tab.
Set the Datasource to your DataSource.
If all is ok you should now be able to see the records from your table

 (notice: I used  ZConnection1,ZQuery1,Datasource1,DBGrid1  and can make connection to database )

and i added this code to FormCreate  (onCreate event) to show data in DBgrid.

Code: [Select]
ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from tlb');
  ZQuery1.ExecSQL;

my problem occurs when goning to ENABLE ZQuery but facing with this error:
Quote
SQL Query is empty

I would like to get data from database with write query (not change  SQL property of ZQuery1).
is there need other object such as ZUpdateSQL1?


thanks a lot.



« Last Edit: November 28, 2012, 10:35:15 pm by aliboy »

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: basic question-Tzquery
« Reply #1 on: November 24, 2012, 03:37:34 pm »
Shouldn't it be ZQuery1.Open rather than ZQuery1.ExecSQL?
Lazarus 3.0/FPC 3.2.2

aliboy

  • New Member
  • *
  • Posts: 23
Re: basic question-Tzquery
« Reply #2 on: November 24, 2012, 04:38:46 pm »
Shouldn't it be ZQuery1.Open rather than ZQuery1.ExecSQL?

when i used ZQuery1.ExecSQL project loaded with no error and Dbgrid had no data,

Code: [Select]
ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from tlb');
  ZQuery1.ExecSQL

 and when used ZQuery1.Open  had a memory error.

Code: [Select]
ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from tlb');
  ZQuery1.open

i think connecting to database has a tip that i search for it.
actually i have 2 problems 1-SQL Query is empty   2-i would like to write query to get data on   FormCreate


EgonHugeist

  • Jr. Member
  • **
  • Posts: 78
Re: basic question-Tzquery
« Reply #3 on: November 24, 2012, 06:42:42 pm »
You are posting everywhere around and nobody knows if your threads are solved or not.  %)

TZQuery.Open; opens a resultset and fetches the data from the database.

TZQuery.ExecSQL; simply executes a statement but no resultset is retrived. Only an updatecount is validated.

What is a memory-error? Give all peoples more details to help you getting in. Also read the tutorials.

aliboy

  • New Member
  • *
  • Posts: 23
Re: basic question-Tzquery
« Reply #4 on: November 24, 2012, 11:10:29 pm »
Ok.thanks.

I install Lazarus 1.0.2 .

I create a form (form1) and drag these objects to it and for each

component change  some properties:
1-Zconnection1

clientcodepage=utf8
conncted:ture
database:amahi
libararylocation:G:\programming\Lazrus_P\DataBase\Example2\libmysql.dll
port:3306
user:root
password:
protocol:mysql-5

(amahi is my database name at mysql) (my username has no password so it is blank)

2-ZQuery1

connection:ZConnection1

(in default   Active:false i didn't change it )

3-datasource1

dataset:ZQuery1

4-dbgrid1

Datasource:Datasource1

5-form1
onCreate:

 ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from tlb');
  ZQuery1.ExecSQL;

with this changes, the project ran but dbgrid couldn't show data.
so for ZQuery1  change Active to true and Lazarus says:
Sql query is empty
.
for avoiding this error must add a sql command to SQL property of ZQuery1.(for example: select * from tlb) ==>and dbgrid shows data

I would like to get data from database with write query (not change  SQL property of ZQuery1).
OK?

thank you.



« Last Edit: November 24, 2012, 11:17:15 pm by aliboy »

aliboy

  • New Member
  • *
  • Posts: 23
Re: basic question-Tzquery
« Reply #5 on: November 28, 2012, 10:32:47 pm »
it solved:

onCreate form must add this code:

 
Code: [Select]
ZQuery1.SQL.Clear;
  ZQuery1.SQL.Add('select * from s');
  ZQuery1.Open;
  ZQuery1.Active:=true;
« Last Edit: November 29, 2012, 04:11:46 pm by aliboy »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: (solved) basic question-Tzquery
« Reply #6 on: November 29, 2012, 11:33:40 am »
Glad you got it solved and thanks for posting.

If you think you can update the tutorial so others can benefit, please be my guest. Don't worry too much about the English or formatting as others can always fix that - it is much more difficult to write content than to correct some spelling errors.

A tiny hint: rather than using the Insert Quote button around your code on your posts, try the Insert Code button next to it (that looks like #)
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

 

TinyPortal © 2005-2018