Adding a New Record

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ashlar64
Posts: 75
Joined: Thu 04 May 2006 18:56

Adding a New Record

Post by ashlar64 » Thu 04 May 2006 19:10

Hello,

I am evaluating these components with Builder 6.0 and I am trying to add a new record onto a table.

The table Site_ID has these fields.

ID which is a int, the primary flag and is set to auto increment
Name varchar(20)
Notes text
Num int


I was trying to use some of the methods in TMyTable like
Table1->Insert();
Table1->FieldVaues["Name"] = Edit1->Text;
Table1->FieldVaues["Notes"] = Edit2->Text;
Table1->FieldVaues["Num"] = 5;
Table1->Post();

But I seem to get errors when I try it this way.
Am I missing some steps? Or do I need to do it with SQL statements in a TMyQuery component?

Also how do I ensure that ID will be given the next number? I was under the impression MySQL does that automatically?


Thanks in Advance


---Dave

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Thu 04 May 2006 21:30

You should at least post the error message you're getting. You'll know the last inserted ID with mytable.LastInsertID.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 05 May 2006 09:26

> Am I missing some steps?
It looks like correct operators sequence. Please describe the problem more detailed.

> Or do I need to do it with SQL statements in a TMyQuery component?
You can use TMyQuery but this isn't necessary. You should keep in mind that TMyQuery component provides more functionality than TMyTable.

> Also how do I ensure that ID will be given the next number?
As GEswin said, you can use LastInsertID property. Furthermore, after Post you can also check field value for AUTO_INCREMENT field.

> I was under the impression MySQL does that automatically?
MySQL server automatically generates values for fields defined as AUTO_INCREMENT, but the server also allows to insert values into AUTO_INCREMENT fields manually.

ashlar64
Posts: 75
Joined: Thu 04 May 2006 18:56

Post by ashlar64 » Fri 12 May 2006 12:42

Actually my code was correct....the mysql server was going a little nutty and just needed to be reset.

Post Reply