gpt4 book ai didi

mySql:如何创建具有未知数据类型的表?

转载 作者:行者123 更新时间:2023-11-29 00:46:41 38 4
gpt4 key购买 nike

我希望得到你的帮助,我已经搜索了很多但还没有找到答案。正如问题的标题所述,我试图在 mySql 数据库中创建一个表,我不知道其中列的数据类型。基本上,我使用 php 从 csv 文件在我的数据库中创建新表。第一行包含列名。我知道这是可以做到的,因为我在 phpMyAdmin 中看到过它,我可以在其中选择一个 csv 文件并使用第一行生成表列名称,而且我不必指定每一列的数据类型,

到目前为止,这是我的 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();
}

最佳答案

MySQL 中没有“未知”数据类型(可能 blob 类型与它最相似)。

如果您真的需要仅根据名称自动定义表字段,并且不知道将在其中存储哪种类型的数据,请将它们创建为 blob 或文本,并在其中存储值时创建一个由其组成的文本表示实际类型作为标题加上值的文本表示。

或者您可能根本不需要它。检查Using PHP to take the first line of a CSV file and create a MySQL Table with the data也许您会在那里找到回应。

关于mySql:如何创建具有未知数据类型的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10266749/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com