1

I am trying to insert some text into a sqlite database.

I am using FireDac connection and FireDac Query(FDQuery1) to connect to the sqLite database. Here is code.

FDQuery1.SQL.Text := 'select * from Invoice where Name = :Name';
FDQuery1.ParamByName('Name').AsString := '123';
FDQuery1.Open;
LinkListControlToField1.BindLink.FillList

I seems there is a new record inserted in the database but all fields are null. What could be the problem ?

Now i am using

NEW_NAME:='dfddf';

  SQL :='INSERT INTO INVOICE (Name) VALUES (:NEW_NAME)';
  fdquery1.Close;
  fdquery1.SQL.Text:= SQL;
  FdQuery1.Open();
  FDQuery1.Insert;
  //Fdquery1.ParamByName('New_Name').AsString := NEW_NAME;
  //fdquery1.SQL.Text:='INSERT INTO INVOICE (Name) VALUES (:NEW_NAME)';
  fdquery1.FieldByName('Name').AsString := quotedstr(NEW_NAME);
  //fdquery1.ExecSQL();
  fdquery1.Post;

I am getting eerror message. FireDac, Phys,Sqlite - 308 Can not open/define command, wiich does not return result sets. Hint use Execute? ExecSql metnod for non Select commands.

As you can see from the commented code I am trying the ExecSql but same error.

3
  • 3
    'Select' statements ... select data from the database. They don't insert data. Commented Sep 23, 2013 at 4:18
  • Take a look here, for the different ways: stackoverflow.com/questions/18822687/… Commented Sep 23, 2013 at 5:32
  • The code should look like this: 1. INSERT INTO .... 2. Post - to commit the transaction. Commented Sep 23, 2013 at 13:22

2 Answers 2

3

While SELECT sql statements cannot insert data into a table, records can be inserted/appended through TDataset descendents that are connected to a table via a SELECT sql statement.

For example:

FDQuery1.SQL.Text := 'select * from Invoice';
FDQuery1.Open;

NEW_NAME:='dfddf';
FDQuery1.Append;  // or FDQuery1.Insert;
FDQuery1.FieldByName('Name').AsString := NEW_NAME;
// set other column values as needed
FDQuery1.Post;

If you prefer to use an INSERT:

FDQuery1.SQL.Text := 'INSERT INTO INVOICE (Name) VALUES (:NEW_NAME)';

NEW_NAME := 'dfddf';
FDQuery1.ParamByName('NEW_NAME').AsString := NEW_NAME;
// you will have to define parameters for each column
FDQuery1.ExecSQL;
Sign up to request clarification or add additional context in comments.

Comments

0

Replace FDQuery1.Open to FDQuery1.ExecSQL;

But you statment "Select *..." dont Insert any record in database...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.