doukaojie8573 2015-03-21 21:03
浏览 35

有没有更好的方法上传记录集与JSON或其他? [关闭]

I have firebird database and need to upload one of database tables to remote mysql server on web. There are thousands records and I dont know how to upload these records. I can upload records one by one with JSON. I'm using POST method.

How can I upload all records in one time or in parts?

Update 1: This codes is working for one by one data update. But it's like such a flood attack. I want to upload all data that I've selected in one time.

Delphi 7 Side HTTP Post Method

function PostURLAsString(aURL: string; code:string): string;
var
  lHTTP: TIdHTTP;
  lStream: TStringStream;
  parameters: TStringList;

begin
  lHTTP := TIdHTTP.Create(nil);
  lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
  lStream := TStringStream.Create(Result);
  try
    Parameters := TStringList.Create;
    parameters.Add('code=' + code);
    lHTTP.Post(aURL, parameters,lStream);
    lStream.Position := 0;
    Result := lStream.ReadString(lStream.Size);
  finally
    FreeAndNil(lHTTP);
    FreeAndNil(lStream);
  end;
end;

Upload Records One by One:

procedure TForm1.Button1Click(Sender: TObject);
var
  js:TlkJSONobject;
  jb: TlkJSONbase;
  s: String;
  code:string;
begin
  IBQuery1.First;
  with IBQuery1 do
  while not Eof do
  begin
    code :=   VarToStr(FieldValues['code']);
    s := PostURLAsString('http://www.domain.com/uitems.php', code);

    js := TlkJSON.ParseText(s) as TlkJSONobject;
    jb := js.Field['items'];

    if VarToStr(jb.Child[0].Field['status'].Value) = '1' then
      ListBox1.Items.Add(code + ' is inserted')
    else
      ListBox1.Items.Add(code + ' is not inserted');

    Application.ProcessMessages;

    js.Free;
    Next;
  end;
end;

PHP Side uitems.php

<?php
  include_once dirname(__FILE__) .'/DBConnect.php';

  function update($code){

    $db = new DbConnect();

    // array for json response
    $response = array();
    $response["items"] = array();   

    $sqlstr = "INSERT INTO items (`code`) VALUES ('$code')";

    $result = mysql_query($sqlstr);



    $tmp = array();

    if ($result) {
        // successfully updated
        $tmp["status"] = 1; //inserted
    } else {
        $tmp["status"] = 0; //not inserted
    } 

    array_push($response["items"], $tmp);

    header("Content-Type: application/json; charset=utf-8", true);      
    // echoing json result
    echo json_encode($response, JSON_UNESCAPED_UNICODE);

  }

  update($_POST["code"]);

?>
  • 写回答

1条回答 默认 最新

  • doujian3401 2015-03-23 12:59
    关注

    You can send arrays of data using JSON. Just prepare that array on the Delphi side, send it over to your script and execute the query for every item of your array. You can return an array also, which includes the success messages for every array item you uploaded.

    评论

报告相同问题?

悬赏问题

  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面