将 TFDquery 中的新记录插入另一个 sqlite 数据库(firemonkey)
insert new record from TFDquery into another sqlite db (firemonkey)
我有 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
returns 4 条记录,我想用 queryUPDATE
将它们推送到另一个数据库中。请注意,这两个数据库具有完全相同的结构和列顺序。
我不知道这是否是最好的方法,但试试这样的方法:
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();
我有 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
returns 4 条记录,我想用 queryUPDATE
将它们推送到另一个数据库中。请注意,这两个数据库具有完全相同的结构和列顺序。
我不知道这是否是最好的方法,但试试这样的方法:
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();