我的目的是这样的:在程序运行中,动态的把表tablei(比如叫x1.dbf)复制一份,改名为x1(k).dbf(也可以是随便一个不同于x1.dbf的名字),保存在文件夹abc下,现在写了一些代码,但是运行报错,大家帮我看看:
procedure TForm6.savetable(tablei:ttable;k:integer);
var
  fname,yname,fname_first,fname_last,str:string;
  i:integer;
begin
  yname:=ExtractFileName(tablei.tablename);//返回绝对文件名x1.dbf
  i:=pos('.',yname);
  fname_first:=copy(yname,1,i-1);
  fname_last:=ExtractFileExt(yname);    //扩展名
  fname:=fname_first+'('+inttostr(k)+')'+fname_last;//改成x1(k).dbf  str:='select * into '+fname+'from '+yname;  
  Form6.Query1.Close;
  Form6.Query1.SQL.Clear;
  Form6.Query1.SQL.add(str);
  Form6.Query1.ExecSQL;
end;我没有在sql语句中用过变量(比如上面的fname和yname),现在尝试这样用不知道行不行。现在的问题是,一运行到这里就报错:
invalid use of keyword
line number:1我想问的是:
1,这是什么原因,x1表里并没有设关键字啊?该怎么修改程序呢
2,在str中使用变量fname等,能不能这样用呢?
3,应该不是2的问题吧?因为我改成str:='select * into x12 from x1';还是同样的错误。

解决方案 »

  1.   

    Borland 的 LocalSQL 是本支持 select into 语句的,你要实现备份数据文件的目的,完全可以用API文件操作函数 CopyFile 来实现啊!用法是:CopyFile('源文件名','新文件名',False);需要注意的是:源文件名、新文件名都必须是PChar类型的。CopyFileThe CopyFile function copies an existing file to a new file. BOOL CopyFile(    LPCTSTR lpExistingFileName, // pointer to name of an existing file 
        LPCTSTR lpNewFileName, // pointer to filename to copy to 
        BOOL bFailIfExists  // flag for operation if file exists 
       );
     ParameterslpExistingFileNamePoints to a null-terminated string that specifies the name of an existing file. lpNewFileNamePoints to a null-terminated string that specifies the name of the new file. bFailIfExistsSpecifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.  Return ValuesIf the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError. ResSecurity attributes for the existing file are not copied to the new file. 
    File attributes (FILE_ATTRIBUTE_*) for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. For further information on file attributes, see CreateFile.
      

  2.   

    将String类型转换为PChar可以这样:var
      aSRC,aDest:String;
    begin
      CopyFile(PChar(aSRC),PChar(aDest),False);
    end;//即:使用强制类型转换。
    大量电脑书籍下载:
    http://www.netyi.net/in.asp?id=ForMoreU