DB2 Table Data Migration DB2 command DB2 download DB2 database Getting Started teaching

Source: Internet
Author: User
Tags db2
The system uses a remote database. Location in USA, connection speed is particularly slow. DBA does not give guidance to the database
I had to write a database table migration method. But only one sheet at a time is slowly guiding. Write with PHP


   //Original database$databaseOri=' original database name ';$hostnameOri=' IP address ';$portOri=' Port number ';$userOri=' User name ';$passwordOri=' Password ';$csOri="database= $databaseOri; Hostname= $hostnameOri; Port= $portOri; Protocol=tcpip; Uid= $userOri; Pwd= $passwordOri; ";$dbOri= Db2_connect ($csOri,$userOri,$passwordOri);//target database$databaseDes=' name of the database to transfer to ';$hostnameDes=' IP address ';$portDes=' Port number ';$userDes=' User name ';$passwordDes=' Password ';$csDes="database= $databaseDes; Hostname= $hostnameDes; Port= $portDes; Protocol=tcpip; Uid= $userDes; Pwd= $passwordDes; ";$dbDes= Db2_connect ($csDes,$userDes,$passwordDes);//methods for executing SQL functionfetcher($db, $query,$par = array()) {$stmt= Db2_prepare ($db,$query);$res=Array();if($stmt) {//print_r ($stmt);$ex= Db2_execute ($stmt,$par);if($ex) {Try{ while($row= Db2_fetch_assoc ($stmt) {Array_push ($res,$row); }             }Catch(Exception$e){}          }Else{Print_r ($query); }        }return$res; }//How to insert a database functioninsertintodes($db, $query,$par = array ()){$stmt= Db2_prepare ($db,$query);$res=Array();if($stmt) {$ex= Db2_execute ($stmt,$par);if(!$ex) {Print_r ($query); }        }return$res; } functiontransferdb($tableName,$dbOri,$dbDes)  {//Spell out all the field names of the table you want to transfer$tableCloums= Fetcher ($dbOri,"Select NAME from Sysibm.syscolumns where Tbname= '".$tableName."'",Array()        );$tableParams="";$insertParamsLength= Count ($tableCloums); for($temp=0;$temp<$insertParamsLength;$temp++){$tmpName=$tableCloums[$temp]["NAME"];$tableParams=$tableParams.$tmpName;//print_r ($tableCloums [$temp] ["NAME"]);if($temp<$insertParamsLength-1){$tableParams=$tableParams.","; }        }//Find the primary key of the table$resultKeyArray= Fetcher ($dbOri,"Select NAME from Sysibm.syscolumns where Tbname= '".$tableName."' and keyseq = ' 1 '",Array()        );if($resultKeyArray[0]["NAME"]){$keyCloum=$resultKeyArray[0]["NAME"]; }Else{$keyCloum="ID"; }//Get all the rows of the table, because if the table is too big millions of rows, the full import into memory will be as memory overflow$count= Fetcher ($dbOri,"Select COUNT (*) from public.".$tableName,Array()        );$dataCount=$count[0][1];//Determine how many times to execute$executeloops= Floor ($dataCount/10000+1);//$executeloops = 1;//Cyclic execution for($i=0;$i<$executeloops;$i++){$pageIndex=$i;$beginIndex=$i*10000;$endIndex= ($i+1) *10000;//Through paged query, each time from the original table to get 10,000 data$getDataFromOriTab="SELECT".$tableParams."From (select Row_number () over (ORDER by".$keyCloum."DESC") As ROWNUM, ".$tableParams."from public.".$tableName.") A Where ROWNUM >".$beginIndex."and ROWNUM <=".$endIndex;//fwrite ($myfile, $getDataFromOriTab);$tmpData= Fetcher ($dbOri,$getDataFromOriTab,Array());//Stitching the value of a field in the INSERT statement$valueStr="";foreach($tmpData as$index=$row){$valueStr="";foreach($row as$cloumName=$value) {if(Empty($value)){$valueStr=$valueStr."'',"; }Elseif(Is_null ($value)) {$valueStr=$valueStr."null,"; }Else{$valueStr=$valueStr."$value,"; }                }$valueStr= substr ($valueStr,0, -1);$INSERTSQL="INSERT into public.".$tableName." (".$tableParams.") VALUES (".$valueStr.");"; Insertintodes ($dbDes,$INSERTSQL,Array()); }        }    }//parameter is the table name of the table to be guidedTransferdb ("Table name",$dbOri,$dbDes);?>

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the DB2 table data migration, including the DB2, migration aspects, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.