Domanda

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.

È stato utile?

Soluzione

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;

Altri suggerimenti

Replace FDQuery1.Open to FDQuery1.ExecSQL;

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top