Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL] eksport szkieletu bazy danych
Kreton
post 28.06.2006, 17:26:55
Post #1





Grupa: Zarejestrowani
Postów: 345
Pomógł: 3
Dołączył: 20.02.2005

Ostrzeżenie: (0%)
-----


Witam, ostatnio zastanawialem sie nad pewną rzeczą. Mam np 3 tabele i chcialbym nie tyle wyeksportowac dane które sie w nich znajdją co strukture tych ze tabeli do pliku.

Teraz troszke inaczej smile.gif. Mamy w pliku costam.sql szereg komend ktore buduja nam strokture tabel, bazy. A tutaj sytuacja odwrotna. To na podstawie budowy bazy danych tworzony jest taki plik. Chodzi mi o to zebym nie musiał ręcznie tego pisać. Bo mam skrypt ale wczesniej potworzylem dla potrzeb skryptu te tabele ręcznie, . A pisanie ręcznie (dla osoby która bedzie ten skrypt wykorzystywała gdzie indziej ) szeregu poleceń SQL było by czasochłonne i niezbyt wolne od błędów.
Go to the top of the page
+Quote Post
siemakuba
post 28.06.2006, 18:25:33
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 112
Pomógł: 20
Dołączył: 10.04.2005

Ostrzeżenie: (0%)
-----


moim zdaniem - phpMyAdmin :)
1. możesz poprostu zrobić z jego poziomu eksport struktury bazy i dołączyć jako plik *.sql do skyptów
2. możesz zajrzeć jak to jest tam zrobione :)

jeżeli chcesz robić ręcznie - sprawdź część manuala MySQL dotyczącą pobierania informacji o bazie - przydatne będą zapytania takie jak SHOW, DESCRIBE.

pozdr.
Go to the top of the page
+Quote Post
bim2
post 28.06.2006, 18:34:08
Post #3





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

Ostrzeżenie: (0%)
-----


  1. <?
  2.  
  3.  
  4. $db_host='localhost';
  5. $db_name='nazwa-bazy';
  6. $db_pass='haslo';
  7. $db_user='user';
  8. $site_name='nazwa strony';
  9. $aadres;='adres strony';
  10.  
  11.  
  12.  
  13.  
  14. $crlf="n";
  15.  
  16. $strNoTablesFound = "Brak tabel w bazie danych.";
  17. $strHost = "Host";
  18. $strDatabase = "Baza danych ";
  19. $strTableStructure = "Struktura tabeli dla";
  20. $strDumpingData = "Zawartosc tabeli";
  21. $strError = "blad";
  22. $strSQLQuery = "SQL";
  23. $strMySQLSaid = "MySQL : ";
  24. $strBack = "Wstecz";
  25. $strFileName = "Kopia bazy";
  26. $strName = "Baza danych";
  27. $strDone = "Z dnia";
  28. $strat = "";
  29. $strby = "by";
  30. $date_jour = date ("m-d-Y");
  31.  
  32.  
  33. header("Content-disposition: filename=$strFileName $db_name $date_jour.sql");
  34. header("Content-type: application/octetstream");
  35. header("Pragma: no-cache");
  36. header("Expires: 0");
  37.  
  38. // doing some DOS-CRLF magic...
  39. $client = getenv("HTTP_USER_AGENT");
  40. if(ereg('[^(]*((.*))[^)]*',$client,$regs)) 
  41. {
  42. $os = $regs[1];
  43. // this looks better under WinX
  44. if (eregi("Win",$os)) 
  45. $crlf="rn";
  46. }
  47.  
  48.  
  49. function my_handler($sql_insert)
  50. {
  51. global $crlf;
  52. echo "$sql_insert;$crlf";
  53. }
  54.  
  55. // Get the content of $table as a series of INSERT statements.
  56. // After every row, a custom callback function $handler gets called.
  57. // $handler must accept one parameter ($sql_insert);
  58. function get_table_content($db, $table, $handler)
  59. {
  60. $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die();
  61. $i = 0;
  62. while($row = mysql_fetch_row($result))
  63. {
  64. // set_time_limit(60); // HaRa
  65. $table_list = "(";
  66.  
  67. for($j=0; $j<mysql_num_fields($result);$j++)
  68. $table_list .= mysql_field_name($result,$j).", ";
  69.  
  70. $table_list = substr($table_list,0,-2);
  71. $table_list .= ")";
  72.  
  73. if(isset($GLOBALS["showcolumns"]))
  74. $schema_insert = "INSERT INTO $table $table_list VALUES (";
  75. else
  76. $schema_insert = "INSERT INTO $table VALUES (";
  77.  
  78. for($j=0; $j<mysql_num_fields($result);$j++)
  79. {
  80. if(!isset($row[$j]))
  81. $schema_insert .= " NULL,";
  82. elseif($row[$j] != "")
  83. $schema_insert .= " '".addslashes($row[$j])."',";
  84. else
  85. $schema_insert .= " '',";
  86. }
  87. $schema_insert = ereg_replace(",$", "", $schema_insert);
  88. $schema_insert .= ")";
  89. $handler(trim($schema_insert));
  90. $i++;
  91. }
  92. return (true);
  93. }
  94.  
  95. // Return $table's CREATE definition
  96. // Returns a string containing the CREATE statement on success
  97. function get_table_def($db, $table, $crlf)
  98. {
  99. $schema_create = "";
  100. //$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
  101. $schema_create .= "CREATE TABLE $table ($crlf";
  102.  
  103. $result = mysql_db_query($db, "SHOW FIELDS FROM $table") or mysql_die();
  104. while($row = mysql_fetch_array($result))
  105. {
  106. $schema_create .= "  $row[Field] $row[Type]";
  107.  
  108. if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
  109. $schema_create .= " DEFAULT '$row[Default]'";
  110. if($row["Null"] != "YES")
  111. $schema_create .= " NOT NULL";
  112. if($row["Extra"] != "")
  113. $schema_create .= " $row[Extra]";
  114. $schema_create .= ",$crlf";
  115. }
  116. $schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
  117. $result = mysql_db_query($db, "SHOW KEYS FROM $table") or mysql_die();
  118. while($row = mysql_fetch_array($result))
  119. {
  120. $kname=$row['Key_name'];
  121. if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
  122. $kname="UNIQUE|$kname";
  123.  if(!isset($index[$kname]))
  124.  $index[$kname] = array();
  125.  $index[$kname][] = $row['Column_name'];
  126. }
  127.  
  128. while(list($x, $columns) = @each($index))
  129. {
  130.  $schema_create .= ",$crlf";
  131.  if($x == "PRIMARY")
  132.  $schema_create .= "  PRIMARY KEY (" . implode($columns, ", ") . ")";
  133.  elseif (substr($x,0,6) == "UNIQUE")
  134. $schema_create .= "  UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
  135.  else
  136. $schema_create .= "  KEY $x (" . implode($columns, ", ") . ")";
  137. }
  138.  
  139. $schema_create .= "$crlf)";
  140. return (stripslashes($schema_create));
  141. }
  142.  
  143. function mysql_die($error = "")
  144. {
  145. echo "<b> $strError </b><p>";
  146. if(isset($sql_query) && !empty($sql_query))
  147. {
  148. echo "$strSQLQuery: <pre>$sql_query</pre><p>";
  149. }
  150. if(empty($error))
  151. echo $strMySQLSaid.mysql_error();
  152. else
  153. echo $strMySQLSaid.$error;
  154. echo "<br><a href="java script:history.go(-1)">$strBack</a>";
  155. }
  156.  
  157. global $db_host, $db_name, $db_pass, $db_user, $site_name, $aadres;
  158.  
  159. $tables = mysql_list_tables($db_name);
  160.  
  161. $num_tables = @mysql_numrows($tables);
  162. if($num_tables == 0)
  163. {
  164. echo $strNoTablesFound;
  165. }
  166. else
  167. {
  168. $i = 0;
  169. $heure_jour = date ("H:i");
  170. print "# ========================================================$crlf";
  171. print "#$crlf";
  172. print "# $strName : $db_name$crlf";
  173. print "# $strDone $date_jour $strat $heure_jour$crlf";
  174. print "# $site_name ($aadres)$crlf";
  175. print "#$crlf";
  176. print "# ========================================================$crlf";
  177. print "$crlf";
  178.  
  179. while($i < $num_tables)
  180. { 
  181. $table = mysql_tablename($tables, $i);
  182.  
  183. print $crlf;
  184. print "# --------------------------------------------------------$crlf";
  185. print "#$crlf";
  186. print "# $strTableStructure '$table'$crlf";
  187. print "#$crlf";
  188. print $crlf;
  189.  
  190. echo get_table_def($db_name, $table, $crlf).";$crlf$crlf";
  191.  
  192. print "#$crlf";
  193. print "# $strDumpingData '$table'$crlf";
  194. print "#$crlf";
  195. print $crlf;
  196.  
  197. get_table_content($db_name, $table, "my_handler");
  198.  
  199. $i++;
  200. }
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. ?>
taki mały skrypcik haha.gif (jest z phpnuke przerobiony)


--------------------
Go to the top of the page
+Quote Post
dr_bonzo
post 28.06.2006, 18:40:00
Post #4





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


mysqldump i przejrzyj jego opcje -- jest tam generowanie tylko struktury,

lub

  1. SHOW CREATE TABLE nazwaTabeli


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Kreton
post 2.07.2006, 19:20:45
Post #5





Grupa: Zarejestrowani
Postów: 345
Pomógł: 3
Dołączył: 20.02.2005

Ostrzeżenie: (0%)
-----


Wielkie dzięki. Zobacze jak to działa smile.gif

Jest też inny sposób. Przy ekporcie danych w ustawieniach eksoportu wyłączamy checkboxa DANE. W taki sposób PHP_My_Admin generuje do pliku szkielet. To tyle. Temat można zamknąć biggrin.gif Dzieki wszystkim za pomoc biggrin.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 1.05.2025 - 05:09