dongqiuge5435 2017-09-29 20:41
浏览 42

Postgresql从PHP脚本中复制批量数据 - 理想情况下使用ADODB

I have custom software which includes a function to copy data between tables on postgresql servers. It does this 1 row at a time, which is fine when the servers are close together, but as I've started deploying servers where the latency is > 300ms this does not work well at all.

I believe the solution is to use the "COPY" statement, but I am having difficulty implementing it. I am using the ADODB php library

When I attempt a copy from a file I get the error "must be superuser to COPY to or from a file". The problem is that I don't know how to copy "from STDIN" where stdin is not actually piped to the PHP script. Is there any way to provide the stdin input as part of the sql command using ADODB, or is there an equivalent command which will allow me to do a batch insert without waiting for each individual insert ?

  • 写回答

2条回答 默认 最新

  • dongzhugao9929 2017-09-30 02:06
    关注

    I solved the problem by using an insert command which had all the inserts in a single statement using "union all" - ie

    $sql="INSERT INTO tablename (name,payload)
        select 'hello', 'world' union all
        select 'this', 'is a test' union all
        select 'and it', 'works'";
    $q=$conn->Execute($sql);
    

    One limitation of this solution was that strings need to be enquoted while integers for example, must not be. I thus needed to write some additional code to make sure some fields were enquoted but not others.

    To find out which columns needed to be enquoted I used

    $coltypes=$todb->GetAll("select column_name,data_type 
                            from information_schema.columns 
                            where table_name='".$totable."'");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据