Insert data into database [Delphi] Insert data into database [Delphi] database database

Insert data into database [Delphi]


You can use a TADODataset (or TADOQuery).

The way I do it sometimes is with by setting the CommandText to return 0 records from the table, i.e. SELECT TOP 0 * FROM [table], then use .Append and .Post

But personally, I prefer writing the SQL, such as with a TADOCommand


Use the TADOQuery object if you don't need to display the table's data.

Basically:

  • Use TADOQuery.SQL.Text to set the SQL command
  • Use TADOQuery.ExecSQL method to fire the SQL command


You can also use the TADOCommand component, and have it execute the specific SQL command. If you find yourself performing the same command over and over again (like inserts into a table) then consider using parameters rather than directly changing the SQL for every call. Parameters are easy to use, just place a :PARAMNAME in your sql, then use the parameters object on the ado component your using to set the value. For example:

Assuming the CommandText of the TAdoCommand component contains "INSERT INTO TABLENAME (FIELD1) VALUES (:FIELDVALUE1)"

AdoCommand1.Parameters.ParamByName('FIELDVALUE1').Value := 'TEST'AdoCommand1.Execute;

When the above sql is executed, then the string "TEST" would be written to FIELD1.