Heim >Backend-Entwicklung >PHP-Tutorial >DB2表数据迁移 db2命令 db2下载 db2数据库入门教

DB2表数据迁移 db2命令 db2下载 db2数据库入门教

WBOY
WBOYOriginal
2016-07-29 08:52:041297Durchsuche

系统用到一个远程数据库。地点在USA,连接速度特别慢。DBA又不给导数据库
只好自己写一个数据库表迁移的方法。但是只能一次一张表慢慢的导。使用PHP编写

<code><span><?php </span><span>//原数据库</span><span>$databaseOri</span> = <span>'原数据库名称'</span>;
    <span>$hostnameOri</span> = <span>'IP地址'</span>;
    <span>$portOri</span> = <span>'端口号'</span>;
    <span>$userOri</span> = <span>'用户名'</span>;
    <span>$passwordOri</span> = <span>'密码'</span>;
    <span>$csOri</span>=<span>"DATABASE=$databaseOri;HOSTNAME=$hostnameOri;PORT=$portOri;PROTOCOL=TCPIP;UID=$userOri;PWD=$passwordOri;"</span>;
    <span>$dbOri</span> = db2_connect (<span>$csOri</span>, <span>$userOri</span>, <span>$passwordOri</span>);
    <span>//目标数据库</span><span>$databaseDes</span> = <span>'要转移到的数据库的名称'</span>;
    <span>$hostnameDes</span> = <span>'IP地址'</span>;
    <span>$portDes</span> = <span>'端口号'</span>;
    <span>$userDes</span> = <span>'用户名'</span>;
    <span>$passwordDes</span> = <span>'密码'</span>;
    <span>$csDes</span>=<span>"DATABASE=$databaseDes;HOSTNAME=$hostnameDes;PORT=$portDes;PROTOCOL=TCPIP;UID=$userDes;PWD=$passwordDes;"</span>;
    <span>$dbDes</span> = db2_connect (<span>$csDes</span>, <span>$userDes</span>, <span>$passwordDes</span>);

    <span>//执行sql的方法</span><span><span>function</span><span>fetcher</span><span>(<span>$db</span>, <span>$query</span>,<span>$par</span> = array<span>()</span>)</span>{</span><span>$stmt</span> = db2_prepare(<span>$db</span>, <span>$query</span>);
        <span>$res</span>=<span>array</span>();
        <span>if</span>(<span>$stmt</span>) {
          <span>//print_r($stmt);</span><span>$ex</span> = db2_execute(<span>$stmt</span>,<span>$par</span>);
          <span>if</span>(<span>$ex</span>) {
            <span>try</span>{
                <span>while</span>(<span>$row</span> = db2_fetch_assoc(<span>$stmt</span>)) {
                    array_push(<span>$res</span>, <span>$row</span>);
                } 
            }<span>catch</span>(<span>Exception</span><span>$e</span>){}
          }<span>else</span>{
            print_r(<span>$query</span>);
          }
        }
        <span>return</span><span>$res</span>;
    }

    <span>//插入数据库的方法</span><span><span>function</span><span>insertIntoDes</span><span>(<span>$db</span>, <span>$query</span>,<span>$par</span> = array<span>()</span>)</span>{</span><span>$stmt</span> = db2_prepare(<span>$db</span>, <span>$query</span>);
        <span>$res</span>=<span>array</span>();
        <span>if</span>(<span>$stmt</span>) {
          <span>$ex</span> = db2_execute(<span>$stmt</span>,<span>$par</span>);
          <span>if</span>(!<span>$ex</span>){
            print_r(<span>$query</span>);
          }
        }
        <span>return</span><span>$res</span>;
    }

    <span><span>function</span><span>transferDB</span><span>(<span>$tableName</span>,<span>$dbOri</span>,<span>$dbDes</span>)</span> {</span><span>//拼出要转移的table的所有字段名</span><span>$tableCloums</span> = fetcher(
            <span>$dbOri</span>,
            <span>"select NAME from sysibm.syscolumns where tbname='"</span>.<span>$tableName</span>.<span>"'"</span>,
            <span>array</span>()
        );
        <span>$tableParams</span> = <span>""</span>;
        <span>$insertParamsLength</span> = count(<span>$tableCloums</span>);
        <span>for</span>(<span>$temp</span>=<span>0</span>;<span>$temp</span>$insertParamsLength</span>;<span>$temp</span>++){
            <span>$tmpName</span> = <span>$tableCloums</span>[<span>$temp</span>][<span>"NAME"</span>];
            <span>$tableParams</span> = <span>$tableParams</span>.<span>$tmpName</span>;
            <span>//print_r($tableCloums[$temp]["NAME"]);</span><span>if</span>(<span>$temp</span> $insertParamsLength - <span>1</span> ){
                <span>$tableParams</span> = <span>$tableParams</span>.<span>","</span>;
            }
        }
        <span>//找出table的主键</span><span>$resultKeyArray</span> = fetcher(
            <span>$dbOri</span>,
            <span>"select NAME from sysibm.syscolumns where tbname='"</span>.<span>$tableName</span>.<span>"' and keyseq = '1'"</span>,
            <span>array</span>()
        );
        <span>if</span>(<span>$resultKeyArray</span>[<span>0</span>][<span>"NAME"</span>]){
            <span>$keyCloum</span> = <span>$resultKeyArray</span>[<span>0</span>][<span>"NAME"</span>];
        }<span>else</span>{
            <span>$keyCloum</span> = <span>"ID"</span>;
        }


        <span>//获取表的所有行数,因为如果表太大有几百万行的话,一下全导入到内存中会照成内存溢出</span><span>$count</span> = fetcher(
            <span>$dbOri</span>,
            <span>"select COUNT(*) from public."</span>.<span>$tableName</span>,
            <span>array</span>()
        );
        <span>$dataCount</span> = <span>$count</span>[<span>0</span>][<span>1</span>];
        <span>//确定要执行几次</span><span>$executeloops</span> = floor(<span>$dataCount</span> / <span>10000</span> + <span>1</span>);
        <span>//$executeloops = 1;</span><span>//循环执行</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$executeloops;<span>$i</span>++){
            <span>$pageIndex</span> = <span>$i</span>;
            <span>$beginIndex</span> = <span>$i</span>*<span>10000</span>;
            <span>$endIndex</span> = (<span>$i</span>+<span>1</span>) * <span>10000</span>;
            <span>//通过分页查询,每次从原表中获取1万条数据</span><span>$getDataFromOriTab</span> = 
            <span>"select "</span>.<span>$tableParams</span>.<span>" from (
                select ROW_NUMBER() OVER(ORDER BY "</span>.<span>$keyCloum</span>.<span>" DESC) AS ROWNUM, "</span>.<span>$tableParams</span>.<span>" from public."</span>.<span>$tableName</span>.<span>"  
            ) a where ROWNUM > "</span>.<span>$beginIndex</span>.<span>" and ROWNUM .<span>$endIndex</span> ;
            <span>//fwrite($myfile, $getDataFromOriTab);</span><span>$tmpData</span> = fetcher(<span>$dbOri</span>,<span>$getDataFromOriTab</span>,<span>array</span>());
            <span>//拼接出insert语句中字段的value的值</span><span>$valueStr</span> = <span>""</span>;
            <span>foreach</span>(<span>$tmpData</span><span>as</span><span>$index</span> => <span>$row</span>){
                <span>$valueStr</span> = <span>""</span>;
                <span>foreach</span> (<span>$row</span><span>as</span><span>$cloumName</span> => <span>$value</span>) {
                    <span>if</span>(<span>empty</span>(<span>$value</span>)){
                        <span>$valueStr</span> = <span>$valueStr</span> . <span>"'',"</span>;
                    }<span>else</span><span>if</span> (is_null(<span>$value</span>)) {
                        <span>$valueStr</span> = <span>$valueStr</span> . <span>"null,"</span>;
                    }<span>else</span>{
                        <span>$valueStr</span> = <span>$valueStr</span> . <span>"$value,"</span>;
                    }
                }
                <span>$valueStr</span> = substr(<span>$valueStr</span>, <span>0</span>, -<span>1</span>);
                <span>$insertSql</span> = <span>"INSERT INTO public."</span>.<span>$tableName</span>.<span>" ("</span>.<span>$tableParams</span>.<span>") VALUES ("</span>.<span>$valueStr</span>.<span>");"</span>;
                insertIntoDes(<span>$dbDes</span>,<span>$insertSql</span>,<span>array</span>()); 
            }
        }

    }
    <span>//参数为要导的表的表名</span>
    transferDB(<span>"表名"</span>,<span>$dbOri</span>,<span>$dbDes</span>);
<span>?></span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了DB2表数据迁移,包括了db2,迁移方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn