如何将文本文件导入到主键为自增的表中

发布于 2024-08-26 02:20:04 字数 325 浏览 4 评论 0原文

我的文本文件中有一些批量数据,需要将其导入 MySQL 表中。该表由两个字段组成..

  1. ID(自动递增的整数)
  2. 名称(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 ..

  1. ID (integer with auto-increment)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

柏林苍穹下 2024-09-02 02:20:04

这是迄今为止最简单的方法:

  1. 确保您的文件没有包含列名称的标题行。如果有,请将其删除。
  2. 在 phpMyAdmin 中,像往常一样:进入表的“导入”选项卡并选择您的文件。选择 CSV 作为格式。然后——这是重要的部分——

格式特定选项:

...在列名称: 填写数据所属列的名称,在您的例子中为“名称”。

这将导入名称并自动递增 id 列。你完成了!

使用 phpMyAdmin 4.2.7.1 测试良好。

Here is the simplest method to date:

  1. Make sure your file does NOT have a header line with the column names. If it does, remove it.
  2. In phpMyAdmin, as usual: go in the Import tab for your table and select your file. Select CSV as the format. Then -- and this is the important part -- in the

Format-Specific Options:

...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.

梦萦几度 2024-09-02 02:20:04

我使用的另一种方法不需要重新排序表的字段(假设自动增量字段是第一列),如下所示:

1) Open/import the text file in Excel (or a similar program).

2) Insert a column before the first column. 

3) Set the first cell in this new column with a zero or some other placeholder.

4) Close the file (keeping it in its original text/tab/csv/etc. format).

5) Open the file in a text editor.

6) Delete the placeholder value you entered into the first cell.

7) Close and save the file.

现在您将拥有一个文件,其中包含原始文件的每一行,前面有一个空列,该文件将是通过 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:

1) Open/import the text file in Excel (or a similar program).

2) Insert a column before the first column. 

3) Set the first cell in this new column with a zero or some other placeholder.

4) Close the file (keeping it in its original text/tab/csv/etc. format).

5) Open the file in a text editor.

6) Delete the placeholder value you entered into the first cell.

7) Close and save the file.

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.

不…忘初心 2024-09-02 02:20:04

使用 LOADTABLE INFILE 导入时不正确,只需创建自动增量列作为最后一列/字段...作为其解析,如果您的表定义有 30 列,但文本文件只有 1 列(或更少) ,它将首先按直接顺序导入前导列,因此请确保字段之间的分隔符...是正确的(对于将来的导入)。再次,将自动增量放在您知道要导入的列数之后。

create table YourMySQLTable
(  FullName varchar(30) not null ,
   SomeOtherFlds varchar(20) not null,
   IDKey int not null AUTO_INCREMENT,
   Primary KEY (IDKey)
);

请注意,IDKey 在表的最后一个字段中自动递增...无论您的 INPUT 流文本文件的列数可能少于最终表实际容纳的列数。

然后,通过以下方式导入数据...

LOAD DATA
    INFILE `C:\SomePath\WhereTextFileIs\ActualFile.txt`
    INTO TABLE YourMySQLTable
    COLUMNS TERMINATED BY `","`  
    LINES TERMINATED BY `\r\n` ;

上面的示例基于逗号分隔的列表,每个字段周围都有引号,例如
“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.

create table YourMySQLTable
(  FullName varchar(30) not null ,
   SomeOtherFlds varchar(20) not null,
   IDKey int not null AUTO_INCREMENT,
   Primary KEY (IDKey)
);

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...

LOAD DATA
    INFILE `C:\SomePath\WhereTextFileIs\ActualFile.txt`
    INTO TABLE YourMySQLTable
    COLUMNS TERMINATED BY `","`  
    LINES TERMINATED BY `\r\n` ;

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.

一个人的夜不怕黑 2024-09-02 02:20:04

我刚刚尝试过:

  1. 在 phpMyAdmin 表中 - 匹配 csv 中的字段数量。
  2. 将csv 数据导入表中
  3. 转到 [结构] 选项卡并添加一个新字段 [在表的开头](我假设您需要 id 字段)
  4. 将 [name] 属性填写为“id”,
  5. [ length] 到“5”
  6. [Index] 到“Primary”
  7. 勾选 A_I(自动增量)
  8. 点击 [Go] 按钮
  9. 该表应该已更新为所有数据前面的 id 字段并自动增量。

至少这样你就不必担心匹配字段等问题。

I just tried this:

  1. In phpMyAdmin table- match the amount of fields you have in your csv.
  2. Perform the import of csv data into your table
  3. Go to the [Structure] tab and add a new field [At beginning of table] (I assume you want the id field there)
  4. Fill in the [name] attribute as "id",
  5. [length] to "5"
  6. [Index] to "Primary"
  7. Tick the A_I (Auto Increment)
  8. Hit [Go] button
  9. The table should have updated with the id field at the front of all your data with auto-incrementing.

At least this way you don't have to worry about matching fields, etc.

笑忘罢 2024-09-02 02:20:04

我只是使用 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...

2024-09-02 02:20:04

我通过简单地在特定于格式的选项下添加不带列 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.

魂归处 2024-09-02 02:20:04

如果表列不匹配,我通常会在真实数据所在的位置添加带有空数据的“虚假”字段,因此,如果我的表需要“id”、“name”、“surname”、“address” "、"电子邮件" 并且我有 "id"、"name"、"surname",我将 CSV 文件更改为包含 "id"、"name"、"姓氏”、“地址”、“电子邮件”,但将我没有数据的字段留空。

这会生成一个如下所示的 CSV 文件:

1,John,Doe,,
2,Jane,Doe,,

我发现它比其他方法更简单。

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:

1,John,Doe,,
2,Jane,Doe,,

I find it simpler than the other methods.

合久必婚 2024-09-02 02:20:04

只是想在我以编程方式将 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)

人心善变 2024-09-02 02:20:04

如果使用 phpMyAdmin,如果您选择 CSV 作为格式,则可以定义列名称。

If using phpMyAdmin you can define your column names if you choose CSV for Format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文