如何将文本文件导入到主键为自增的表中
我的文本文件中有一些批量数据,需要将其导入 MySQL 表中。该表由两个字段组成..
- ID(自动递增的整数)
- 名称(varchar)
文本文件是一大名称集合,每行一个名称...
(示例)
John Doe
Alex Smith
Bob Denver
我知道如何通过 phpMyAdmin 导入文本文件但是,据我了解,我需要导入与目标表具有相同字段数的数据。有没有一种方法可以将文本文件中的数据导入到一个字段中,并使 ID 字段自动递增?
预先感谢您的帮助。
I have some bulk data in a text file that I need to import into a MySQL table. The table consists of two fields ..
- ID (integer with auto-increment)
- Name (varchar)
The text file is a large collection of names with one name per line ...
(example)
John Doe
Alex Smith
Bob Denver
I know how to import a text file via phpMyAdmin however, as far as I understand, I need to import data that has the same number of fields as the target table. Is there a way to import the data from my text file into one field and have the ID field auto-increment automatically?
Thank you in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是迄今为止最简单的方法:
CSV
作为格式。然后——这是重要的部分——...在
列名称:
填写数据所属列的名称,在您的例子中为“名称”。这将导入名称并自动递增
id
列。你完成了!使用 phpMyAdmin 4.2.7.1 测试良好。
Here is the simplest method to date:
CSV
as the format. Then -- and this is the important part -- in the...in the
Column names:
fill in the name of the column the data is for, in your case "Name".This will import the names and auto-increment the
id
column. You're done!Tested fine with phpMyAdmin 4.2.7.1.
我使用的另一种方法不需要重新排序表的字段(假设自动增量字段是第一列),如下所示:
现在您将拥有一个文件,其中包含原始文件的每一行,前面有一个空列,该文件将是通过 phpMyAdmin 导入时转换为下一个相关的自动增量值。
Another method I use that does not require reordering a table's fields (assuming the auto-increment field is the first column) is as follows:
Now you will have a file containing each row of your original file preceded by an empty column, which will be converted into the next relevant auto-increment value upon import via phpMyAdmin.
使用 LOADTABLE INFILE 导入时不正确,只需创建自动增量列作为最后一列/字段...作为其解析,如果您的表定义有 30 列,但文本文件只有 1 列(或更少) ,它将首先按直接顺序导入前导列,因此请确保字段之间的分隔符...是正确的(对于将来的导入)。再次,将自动增量放在您知道要导入的列数之后。
请注意,IDKey 在表的最后一个字段中自动递增...无论您的 INPUT 流文本文件的列数可能少于最终表实际容纳的列数。
然后,通过以下方式导入数据...
上面的示例基于逗号分隔的列表,每个字段周围都有引号,例如
“myfield1”,“另一个字段”,“最后一个字段”。此外,终止符是典型文本文件每行分隔的 cr/lf
在将全名作为单列的文本文件示例中,所有数据都将加载到“YourMySQLTable”的 FullName 列中。由于IDKey位于列表的末尾,因此它仍然会从1-?并且与入站文本中的列没有任何冲突。
Not correct on import with the LOADTABLE INFILE, just create the auto-increment column as the LAST column/field... As its parsing, if your table is defined with 30 columns, but the text file only has 1 (or anything less), it will import the leading columns first, in direct sequence, so ensure your delimited with... is correct between fields (for any future imports). Again, put the auto-increment AFTER the number of columns you know are being imported.
Notice the IDKey is auto-increment in the last field of the table... regardless of your INPUT stream text file which may have less columns than your final table will actually hold.
Then, import the data via...
Above example is based on comma seperated list with quotes around each field such as
"myfield1","anotherField","LastField". Also, the terminated is the cr/lf that typical text files are delimited per row
In the sample of your text file having the full name as the single column, all the data would get loaded into the "YourMySQLTable" into the FullName column. Since the IDKey is at the END of the list, it will still be auto-increment assigned values from 1-? and not have any conflict with the columns from the inbound text.
我刚刚尝试过:
至少这样你就不必担心匹配字段等问题。
I just tried this:
At least this way you don't have to worry about matching fields, etc.
我只是使用 TAB 作为文本文件中的第一个字段,然后像往常一样导入它。我收到有关 ID 字段的警告,但该字段按预期递增......
I just used a TAB as the first field in my text file, then imported it as usual. I got a warning about the ID field but the field incremented as expected...
我通过简单地在特定于格式的选项下添加不带列 ID 的列名解决了这个问题。因为Column ID是自动递增的。就我而言,无需更改 CSV 文件中的任何内容即可正常工作。我的 CSV 文件中只有数据,没有列标题。
I´ve solved that problem by simply add the column_names under Format-Specific Options without the Column ID. Because the Column ID ist Auto increment. In my case it works fine without changing anything in the CSV File. My CSV File has only Data inside no Column Headers.
如果表列不匹配,我通常会在真实数据所在的位置添加带有空数据的“虚假”字段,因此,如果我的表需要“id”、“name”、“surname”、“address” "、"电子邮件" 并且我有
"id"、"name"、"surname"
,我将 CSV 文件更改为包含"id"、"name"、"姓氏”、“地址”、“电子邮件”
,但将我没有数据的字段留空。这会生成一个如下所示的 CSV 文件:
我发现它比其他方法更简单。
If the table columns do not match, I usually add "bogus" fields with empty data where the real data would've been, so, if my table needs
"id", "name", "surname", "address", "email"
and I have"id", "name", "surname"
, I change my CSV file to have"id", "name", "surname", "address", "email"
but leave the fields that I do not have data for blank.This results in a CSV file looking like this:
I find it simpler than the other methods.
只是想在我以编程方式将 CSV 文件导入到主键作为自动增量的表时使用的命令之上添加 Fabien 的答案。我只是指定要向哪些列插入值,并且仅将这些值包含在 CSV 文件中。这样我就不必在空白字段上胡闹了。
<代码>
加载数据本地 INFILE 'files/thefile.csv'
进入表
importedStuff
由 ',' 终止的字段由 '"' 括起 由 '\n' 终止的行
(创建日期、字段 1、字段 2、字段 5、字段 10、字段 12)
Just wanted to add on to Fabien's answer above the command I use when programmatically importing a CSV file into a table with primary key as auto-increment. I merely specify which columns I'm inserting values to and include those values only in the CSV file. That way I don't have to monkey around with blank fields.
LOAD DATA LOCAL INFILE 'files/thefile.csv'
INTO TABLE
importedStuff
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
(dateCreated,field1,field2,field5,field10,field12)
如果使用 phpMyAdmin,如果您选择 CSV 作为格式,则可以定义列名称。
If using phpMyAdmin you can define your column names if you choose CSV for Format.