2012-04-22 71 views
1

我希望得到你的幫助,我已經搜索了很多東西,但還找不到答案。 作爲問題的標題狀態,我試圖在mySql數據庫中創建一個表,其中我不知道列的數據類型。基本上,我使用php從csv文件中創建數據庫中的新表。第一行包含列名稱。我知道可以這樣做,因爲我已經在phpMyAdmin中看到了它,我可以選擇一個csv文件並使用第一行來生成表列名稱,而且我不必指定每列的數據類型,因此我不需要指定每列的數據類型,mySql:如何使用未知數據類型創建表?

到目前爲止,這是我的PHP代碼:

$databaseName="myDatabase"; 
mysql_connect("localhost","xxxxxx","xxxxxxxx"); 
mysql_select_db($databaseName); 


for($i = 0; $i < count($newFiles); $i++){//traverse the table that contains the file names 
     $content = file($newFileLocation.$newFiles[$i]); 

     //First line: $content[0]; 
     $firstLine=$content[0];//save first line which are the column names 
     //echo $firstLine; 
     $tableName= str_replace('.txt','', $newFiles[$i]);//remove the .txt to use for table name 
     //echo $tableName."\n"; 

     echo "\n\ncreating tables\n\n"; 
     /*mysql_query("load data local infile '$newFiles[$i]' 
       into table $tableName 
        fields terminated by ',' 
        lines terminated by '\n' 
        ($firstLine) "); */ 


     mysql_close(); 
} 

回答

0

這裏是解決問題的辦法。起初,我們用文件的第一行來保存列名(以便我們知道如何創建表)。我們將它保存在一個字符串中,然後根據逗號字符將它們分開。在此之後,我們採取包含值的文件的第二行。我們將該行存儲到一個字符串,並根據逗號字符將字符串分隔爲不同的值。我們將使用它來識別每列的類型。

所以,這是代碼。

$databaseName="myDatabase"; 
mysql_connect("localhost","xxxxxx","xxxxxxxx"); 
mysql_select_db($databaseName); 


for($k = 0; $k < count($newFiles); $k++){//traverse the table that contains the file names 
      $content = file($newFileLocation.$newFiles[$k]); 

      //First line: $content[0]; 
      $firstLine=$content[0]; 
      $secondLine=$content[1]; 
      //echo $firstLine."\n"; 
      $firstLine = str_replace("\r\n",'',$firstLine); 
      $colNames=explode(',',$firstLine); 
      $fieldList = explode(',',$secondLine); 

      $dataType=""; 
      for($i = 0; $i < count($fieldList); $i++){ 
       if (is_numeric($fieldList[$i])){ 
         if (strpos($fieldList[$i],'.') == false){ 
          $fieldList[$i] = (int)$fieldList[$i]; 
         }else{ 
          $fieldList[$i] = (float)$fieldList[$i]; 
         } 
        } 

       switch(gettype($fieldList[$i])) { 
         case 'integer': 
          $typeInfo = 'int(11)'; 
          break; 
         case 'float': 
         case 'double': 
           $typeInfo = 'float'; 
           break; 

         case 'string': 
           $typeInfo = 'varchar(80)'; 
          break; 
         default: 
           $typeInfo = 'varchar(80)'; 
           break; 
         } 

       if(gettype($fieldList[$i]) != NULL) { 
        $dataType= $dataType.'`'.$colNames[$i].'` '." ".$typeInfo.' NOT NULL'; 
        if(($i)!=count($fieldList)-1){ 
         $dataType= $dataType.","; 
        } 
       } 
      } //end of for loop 


      $tableName= str_replace('.txt','', $newFiles[$k]); 
      //echo $tableName."\n"; 

      // 
       //echo "\n".$dataType."\n"; 
       $succ=mysql_query("CREATE TABLE `$tableName` ($dataType)"); 
       if($succ) 
        echo "Table ".$tableName." was created Succesfully \n"; 

       //echo $databaseName; 

       $loadsql = 'LOAD DATA LOCAL INFILE "'.$newFileLocation.$newFiles[$k].'" INTO TABLE `'.$tableName.'` FIELDS TERMINATED BY "," IGNORE 1 LINES ('.$firstLine.')'; 
       mysql_query($loadsql);  




     }//end of for loop 


    mysql_close(); 
1

有沒有「未知」的數據在MySQL(也許是BLOB類型是最相似)的類型。

如果您確實需要自動從名稱中自動定義表格字段,並且不知道將存儲哪種類型的數據,請將它們創建爲blob或文本,並在存儲值時創建一個由其實際類型作爲標題加上文本表示的值。

或者,也許你並不需要,在所有。檢查Using PHP to take the first line of a CSV file and create a MySQL Table with the data,也許你會在那裏找到答案。

+0

你好,這肯定會工作。感謝您的鏈接。它確實有幫助。我將發佈基於該鏈接的解決方案 – 2012-04-22 12:04:51

相關問題