Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-05

SQLQuery1: Field not Found: "enquiryreferencenumber"

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

Hi.

Following Damien's instructions I have DROPped my original table and created a new one in it's place:

CREATE table registration (
fname char(50),
sname char(50),
enquiryreferencenumber INT4 PRIMARY KEY DEFAULT nextval('reg_seq')) WITHOUT OIDS;

I have run my INSERT Query under PQConnection1 using the following arguments:

      sqlq:='INSERT INTO registration (fname,sname) ';
      sqlq:=sqlq+'VALUES
('''+FName.Text+''','''+SName.Text+''')RETURNING
enquiryreferencenumber;';
      PQConnection1.Connected:=True;
      PQConnection1.ExecuteDirect('Begin Work;');
      PQConnection1.ExecuteDirect(sqlq);
      PQConnection1.ExecuteDirect('Commit Work;');
      PQConnection1.Connected:=False;
Then I have attempted to retrieve the enquiryreferencenumber using
SQLQuery1 as follows:
      SQLQuery1.SQL.Text:='SELECT enquiryreferencenumber FROM
registration WHERE fname='''+fname.Text+''' AND
sname='''+sname.Text+'''ORDER BY enquiryreferencenumber DESC LIMIT
1;';
      SQLQuery1.Open;
      enquiryreferencenumber.Text:=SQLQuery1.FieldByName('enquiryreferencenumber').AsString;
      SQLQuery1.Close;

This gives me the following error:
SQLQuery1: Field not Found: "enquiryreferencenumber"

Can anyone help?

Is it possible that the fact that the field 'enquiryreferencenumber' is an integer and I'm accessing it AsString?

Running: "SELECT enquiryreferencenumber from registration;" in psql on the linux machine give the following output:

enquiryreferencenumber
------------------------
14
15
16
17
18

Any Ideas?

Reply by email

Damien Gerard
to Dave Coventry

Le May 28, 2008 ? 6:02 AM, Dave Coventry a ?crit :

Show quoted text 10 lines
Hi. Following Damien's instructions I have DROPped my original table and created a new one in it's place: CREATE table registration ( fname char(50), sname char(50), enquiryreferencenumber INT4 PRIMARY KEY DEFAULT nextval('reg_seq')) WITHOUT OIDS;

You can verify that the expression `WITHOUT OIDS` sticks to your needs http://www.postgresql.org/docs/8.3/static/sql-createtable.html (postgres 8.3/8.2)

Note that fname and sname could be null here.
If you don't want this behavior, append `NOT NULL` after char(50).

I don't think you want char(X) in your case. I think You would prefer varchar(50)
If I store 'A sample text' :

varchar(50) gives 'A sample text'
char(50) gives 'A sample
text '
(all unused char will be replaced by a space, it is a waste of disk space and you may have some strange behavior if you are not plenty aware of this)

Show quoted text 9 lines
I have run my INSERT Query under PQConnection1 using the following arguments: sqlq:='INSERT INTO registration (fname,sname) '; sqlq:=sqlq+'VALUES ('''+FName.Text+''','''+SName.Text+''')RETURNING enquiryreferencenumber;'; PQConnection1.Connected:=True; PQConnection1.ExecuteDirect('Begin Work;');

As said before, transactions here are useless.
Transactions would be usefull for multiple insert/update, not for a single one.

Show quoted text 11 lines
PQConnection1.ExecuteDirect(sqlq); PQConnection1.ExecuteDirect('Commit Work;'); PQConnection1.Connected:=False; Then I have attempted to retrieve the enquiryreferencenumber using SQLQuery1 as follows: SQLQuery1.SQL.Text:='SELECT enquiryreferencenumber FROM registration WHERE fname='''+fname.Text+''' AND sname='''+sname.Text+'''ORDER BY enquiryreferencenumber DESC LIMIT 1;';
You should always use bind values to avoid sql injection (essentially
sql errors).
After a quick look in examples of fpc, it should be like this :
SQLQuery1.SQL.Text := 'SELECT enquiryreferencenumber FROM registration
WHERE fname = :fname AND sname = :sname ORDER BY
enquiryreferencenumber DESC LIMIT 1;';
SQLQuery1.Params.ParamByName('fname').AsString := fname.Text;
SQLQuery1.Params.ParamByName('sname').AsString := sname.Text;

It is true for Insert, Update and Select of course.

Show quoted text 5 lines
SQLQuery1.Open; enquiryreferencenumber .Text:=SQLQuery1.FieldByName('enquiryreferencenumber').AsString;
I think
enquiryreferencenumber.Text :=
IntToStr(SQLQuery1.FieldByName('enquiryreferencenumber').AsInteger);
would be more appropriated
Show quoted text 10 lines
SQLQuery1.Close; This gives me the following error: SQLQuery1: Field not Found: "enquiryreferencenumber" Can anyone help? Is it possible that the fact that the field 'enquiryreferencenumber' is an integer and I'm accessing it AsString?

Tip: Set your search path before any queries.
Do not forget postgres structure is a bit more complicated than others DB.
Here is the structure :
<Database>
+ Schemas
+ Tables

Actually when you are logged on a postgres database, you should be attached to a database (for a standard configuration you have an user <-> a database).
But you may have several shemas, depanding your needs.

Assuming you have the following structure :
DBNameForExample / SchemaNameForExample / registration

I execute this query before all others (right after each new connection of course):
SET search_path TO SchemaNameForExample;

If you don't do this, you should have to give the full name of your tables (SELECT * FROM SchemaNameForExample.registration for example)

Damien Gerard "Intelligence is 10 million rules." -- Douglas Lenat

Reply by email

Joost van der Sluis
to Dave Coventry: Hi. Following Damien's instructions I have DROPped my original table and created a new…

Op woensdag 28-05-2008 om 06:02 uur [tijdzone +0200], schreef Dave Coventry:

Show quoted text 37 lines
Hi. Following Damien's instructions I have DROPped my original table and created a new one in it's place: CREATE table registration ( fname char(50), sname char(50), enquiryreferencenumber INT4 PRIMARY KEY DEFAULT nextval('reg_seq')) WITHOUT OIDS; I have run my INSERT Query under PQConnection1 using the following arguments: sqlq:='INSERT INTO registration (fname,sname) '; sqlq:=sqlq+'VALUES ('''+FName.Text+''','''+SName.Text+''')RETURNING enquiryreferencenumber;'; PQConnection1.Connected:=True; PQConnection1.ExecuteDirect('Begin Work;'); PQConnection1.ExecuteDirect(sqlq); PQConnection1.ExecuteDirect('Commit Work;'); PQConnection1.Connected:=False; Then I have attempted to retrieve the enquiryreferencenumber using SQLQuery1 as follows: SQLQuery1.SQL.Text:='SELECT enquiryreferencenumber FROM registration WHERE fname='''+fname.Text+''' AND sname='''+sname.Text+'''ORDER BY enquiryreferencenumber DESC LIMIT 1;'; SQLQuery1.Open; enquiryreferencenumber.Text:=SQLQuery1.FieldByName('enquiryreferencenumber').AsString; SQLQuery1.Close; This gives me the following error: SQLQuery1: Field not Found: "enquiryreferencenumber" Can anyone help?

No idea. But you could try to set the fieldname between double-quotes (") But that should only be an issue when you also did that while you created the database.

You could also try SQLQuery1.Fields[0].AsString; as you did in your first example.

And which version of fpc are you using? It could be a releaf if you use a recent 2.2.1 snapshot.

Joost.

Reply by email