Recent

Author Topic: Cannot execute INSERT with PostgresSQL 8 and TSQLQuery  (Read 5956 times)

Anonymous

  • Guest
Cannot execute INSERT with PostgresSQL 8 and TSQLQuery
« on: April 21, 2006, 02:00:04 pm »
Hello

I use PQConnection, SQLTransaction and SQLQuery.
I have no problem executing SELECT statement using Open, but I have problems using insert.

I cannot user Open for INSERT so I try like this:
       SQLQuery1.Close;
       SQLQuery1.SQL.Clear;
       SQLQuery1.SQL.Add('INSERT into table...');
       SQLQuery1.ExecSQL;  

It gives no error, but does not insert new record into database either.
What is the correct way to to this?


Cris

g-r-g

  • Newbie
  • Posts: 5
Re: Cannot execute INSERT with PostgresSQL 8 and TSQLQuery
« Reply #1 on: April 28, 2006, 01:06:55 pm »
Quote from: "Anonymous"

       SQLQuery1.Close;
       SQLQuery1.SQL.Clear;
       SQLQuery1.SQL.Add('INSERT into table...');
       SQLQuery1.ExecSQL;  

Code: [Select]
SQLTransaction1.Commit; ?

ematutu

  • Newbie
  • Posts: 5
Re: Cannot execute INSERT with PostgresSQL 8 and TSQLQuery
« Reply #2 on: January 19, 2009, 10:59:11 pm »
Hi, try with this code:

SQLQuery1.Close;
SQLQuery1.InsertSQL.Clear;
SQLQuery1.InsertSQL.Add('insert into table...');
//SQLQuery1.InsertSQL.Add('values...'); //you can divide query
//SQLQuery1.InsertSQL.Add('where...;'); // into multiple lines
SQLQuery1.Open;
SQLQuery1.Insert; //Put Query in Insert Mode
SQLQuery1.Post;
SQLQuery1.ApplyUpdates; //Post & Apply Updates
SQLQuery1.Close;

Loesje

  • Full Member
  • ***
  • Posts: 168
    • Lazarus Support website
Re: Cannot execute INSERT with PostgresSQL 8 and TSQLQuery
« Reply #3 on: January 20, 2009, 01:44:03 pm »
That could work, but for most queries you don't have to provide an InsertSQL since it's generated automatically.

And the original code should also work. Most probably he forgot to commit the transaction, which you also forgot in your example, btw... (for mysql that doesn't matter since transaction-support isn't supported for mysql)

 

TinyPortal © 2005-2018