作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望得到你的帮助,我已经搜索了很多但还没有找到答案。正如问题的标题所述,我试图在 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/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Combination of List> 我有多个列表,可以是 2 个或 3 个,最多 10 个列表,有多个
我是一名优秀的程序员,十分优秀!