我希望得到你的幫助,我已經搜索了很多東西,但還找不到答案。 作爲問題的標題狀態,我試圖在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();
}
你好,這肯定會工作。感謝您的鏈接。它確實有幫助。我將發佈基於該鏈接的解決方案 – 2012-04-22 12:04:51