0

我有 2 个 SQLite 数据库。我想从一个中获取几条记录并将它们插入到另一个中。这样做的好方法是什么?我在下面显示问号,我不确定我需要什么...

TFDQuery *queryNEWDATA;
queryNEWDATA = new TFDQuery(NULL);
queryNEWDATA->Connection = Form1->FDConnection1;   
queryNEWDATA->SQL->Text = "SELECT * FROM mtgs WHERE status = 4";  
queryNEWDATA->Open();
while(!queryNEWDATA->Eof) 
{
  TFDQuery *queryUPDATE;
  queryUPDATE = new TFDQuery(NULL);
  queryUPDATE->Connection = Form1->FDConnection2; 

  queryUPDATE->SQL->Text = ????????????????

  queryUPDATE->ExecSQL();
  queryUPDATE->Close();
  queryUPDATE->DisposeOf();
}

如果我queryNEWDATA返回 4 条记录,我只想将它们推送到另一个数据库中queryUPDATE。请注意,两个数据库具有完全相同的结构和列顺序。

4

1 回答 1

1

我不知道这是否是最好的方法,但试试这样的:

TFDQuery *queryNEWDATA = new TFDQuery(NULL);
queryNEWDATA->Connection = Form1->FDConnection1;   
queryNEWDATA->SQL->Text = _D("SELECT * FROM mtgs WHERE status = 4");
queryNEWDATA->Open();

if (!queryNEWDATA->Eof) 
{
    TStringList *FieldNames = new TStringList;
    TStringList *ParamNames = new TStringList;

    int FieldCount = queryNEWDATA->Fields->Count;
    for (int i = 0; i < FieldCount; ++i)
    {
        String FieldName = queryNEWDATA->Fields->Fields[i]->FieldName;
        FieldNames->Add(FieldName);
        ParamNames->Add(_D(":P") + FieldName);
    }

    TFDQuery *queryUPDATE = new TFDQuery(NULL);
    queryUPDATE->Connection = Form1->FDConnection2;     
    queryUPDATE->SQL->Text = _D("INSERT INTO mtgs (") + FieldNames->CommaText + _D(") VALUES (") + ParamNames->CommaText + _D(")");

    FieldNames->DisposeOf();
    ParamNames->DisposeOf();

    for (int i = 0; i < FieldCount; ++i)
    {
        TField *f = queryNEWDATA->Fields->Fields[i];
        queryUPDATE->Params->ParamByName(_D("P") + f->FieldName)->DataType = f->DataType;
    }

    queryUPDATE->Prepare();

    do
    {
        for (int i = 0; i < FieldCount; ++i)
        {
            TField *f = queryNEWDATA->Fields->Fields[i];
            queryUPDATE->Params->ParamByName(_D("P") + f->FieldName)->Value = f->Value;
        }

        queryUPDATE->ExecSQL();
        queryNEWDATA->Next();
    }
    while (!queryNEWDATA->Eof);

    queryUPDATE->Close();
    queryUPDATE->DisposeOf();
}

queryNEWDATA->Close();
queryNEWDATA->DisposeOf();
于 2018-04-10T23:42:18.400 回答