Recent

Author Topic: [Solved] Which component for update?  (Read 4801 times)

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
[Solved] Which component for update?
« on: May 11, 2012, 01:19:49 pm »
Hello,

I would like to make the following update on my table:
Code: [Select]
update objekt set object='Frimærke' where object= 'Frimµrke';  Right now I have:
TSQLite3connection, SQLQuery and SQLTransaction.
Obviously the SQLQuery require a "select".
I try to use SQLscript, but I an't make it works. Probably because I can't use it!

Can you please tell me how to do so?

Best Regards
Jean-Christophe
« Last Edit: May 11, 2012, 11:32:48 pm by jeanchristophe »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Which component for update?
« Reply #1 on: May 11, 2012, 01:29:47 pm »
Obviously the SQLQuery require a "select".
That is what one would think looking at the "query" in the name.... but it isn't true.

See e.g. here:
http://wiki.lazarus.freepascal.org/Working_With_TSQLQuery#Insert_query_example

Edit: note: you can also use the Connection's ExecuteDirect method...

As an aside, I think perhaps the wiki page I linked to could do with some more updates based on the Dutch version, which is a bit more comprehensive in places..
« Last Edit: May 11, 2012, 01:32:07 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

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Which component for update?
« Reply #2 on: May 11, 2012, 01:45:42 pm »
Quote
Obviously the SQLQuery require a "select".
A query is any SQL command.
 
SQLQuery serves a dual purpose:
- It can act as a Dataset when the query is returning a result set such as SELECT, SHOW, etc. The query is executed with TSQLQuery.Open.
- It can just execute a query that doesn't return a result set such as UPDATE, DELETE, etc. The query is executed with TSQLQuery.ExecSQL. The number of affected rows is returned in SQLQuery2.RowsAffected. 

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: Which component for update?
« Reply #3 on: May 11, 2012, 01:47:50 pm »
Thank you for your interest BigChimp

When I try to write my sql line, I got this:
Code: [Select]
SQLQuery1: Cannot open a non-select statement
I don't know about the Connection's ExecuteDirect method, but I am searching...

Best Regards
Jean-Christophe

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Which component for update?
« Reply #4 on: May 11, 2012, 01:50:11 pm »
My guess is you try to execute the query by doing an "open".

That is for SELECT queries only.   Use an "ExecSQL" instead, or - indeed - an "ExecuteDirect".
1.0/2.6.0  XP SP3 & OS X 10.6.8

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: Which component for update?
« Reply #5 on: May 11, 2012, 06:19:54 pm »
Hello Arbee,

Here are the few lines of code:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  SQLQuery1.SQL.Text:= 'UPDATE objekt SET Object="Frimærke" WHERE Object="Frimµrke";';
  SQLQuery1.Active:= True;
  SQLQuery1.ExecSQL;
  SQLTransaction1.Commit;

Is it wrong?

Best Regards
Jean-Christophe

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Which component for update?
« Reply #6 on: May 11, 2012, 08:26:30 pm »
SQLQuery1.Active:= True is the same as SQLQuery1.Open.  Don't do that if you don't have a result set.

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: Which component for update?
« Reply #7 on: May 11, 2012, 11:32:27 pm »
OK thank you for your answer Ludob

I was a little bit curious because the word "object" was highlighted where I write the SQL statement, so I went to SQLitte manager from mozilla insted.
I try the samme command several times, without succes.
I exported the database and imported it into a new one (to prevent damage)
Then I changed the name of the colon from "Object" to "Objet"
Then I try the exactly samme sentence again (since this morning, I knew it  :o )
And it's works!

I don't really know, but obviously SQLite3 doesn't like the name "Object"!

Thak you everybody for your help and see you soon here!   :D

 

TinyPortal © 2005-2018