Over a million developers have joined DZone.

Populate PostgreSQL Tables

·
This script in PHP populate with demo data tables Postgresql.
It insert serious registers automatically in a table.
Remove the primary key before run.




Inserir Registros de Teste

Cadastrar Tabela do PostgreSQL com Massa de Testes

Remova chave primáia da tabela, antes de executar

Observe que nem todos os tipos de dados foram contemplados, alguns ficarão como string

Host
Banco
Usuário
Senha
Tabela
Registros
'.pg_last_error($conexao)); } $str="SELECT * FROM $tabela"; $consulta= pg_query($conexao,$str); $nc=pg_num_fields($consulta); $nr=pg_num_rows($consulta); $n='';//numericos (int, tinyint, smallint, bigint, etc) $r=''; //reais (float e double) $s="'";//strings $d=date('Y-m-d'); //datas $dt=date('Y-m-d H:i:s');//datatimes $o=''; //outros $inscod .="INSERT INTO $tabela ("; for ($j = 0; $j < $nc; $j++) { $campo = pg_field_name($consulta, $j); if ($j < $nc-1) $inscod .= "$campo,"; else $inscod .= "$campo"; } $inscod .= ")"; $inscod .= " VALUES ("; for ($j = 0; $j < $nc; $j++) { $tam = pg_field_size($consulta, $j); if($tam == -1) $tam=20; //Caso queira limitar os campos ao máximo de 20 posiçes if ($j < $nc-1){ switch (pg_field_type($consulta, $j)){ case 'int4': $n=str_pad($n,$tam,'12345679890'); $inscod .= "$n,"; break; case 'float4': $r=str_pad($r,$tam,'1234567890'); $inscod .= "$r,"; break; case 'bpchar': case 'varchar': $s=str_pad($s,$tam,"abcdefghijklmnopqrstuvxyz"); $inscod .= "$s',"; break; case 'date': $inscod .= "'$d',"; break; case 'timestamp': $inscod .= "'$dt',"; break; case 'text': $inscod .= "'$t'"; break; default: $o=str_pad($o,$tam,"abcdefghijklmnopqrstuvxyz"); $inscod .= "'$o',"; break; } }else{ switch (pg_field_type($consulta, $j)){ case 'int4': $n=str_pad($n,$tam,'1234567890'); $inscod .= "$n"; break; case 'float4': $r=str_pad($r,$tam,'1234567890'); $inscod .= "$r"; break; case 'bpchar': case 'varchar': $s=str_pad($s,$tam,"abcdefghijklmnopqrstuvxyz"); $inscod .= "$s'"; break; case 'date': $inscod .= "'$d'"; break; case 'timestamp': $inscod .= "'$dt'"; break; case 'text': $inscod .= "'$t'"; break; default: $o=str_pad($o,$tam,"abcdefghijklmnopqrstuvxyz"); $inscod .= "'$o'"; break; } } } $inscod .=");"; for($r=1;$r<=$registros;$r++){ //echo $inscod; if(!pg_query($conexao,$inscod)) die ("Erro na inclusã
".pg_last_error($conexao)); } } ?>
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}