Import CSV data in Python-mysql "Learning notes"

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;"  > Export mysql> INSERT into Test_main-select 1, ' A ' union All, and select 2, ' B ' union All, select 3, ' C '; Query OK, 3 rows affected (0.01 sec) records:3 duplicates:0 warnings:0 mysql>mysql> Select ID, value, in To OUTFILE ' F:/test_main.txt ', "Fields TERMINATED by", ' optionally enclosed by ' and LINES TERMINATED by ' \ n ' from Test_main; Query OK, 3 rows affected (0.01 sec) Import data file more fields than target table mysql> LOAD data INFILE ' f:/test_main.txt ' into table t Est_main6, TERMINATED by ', '---optionally enclosed by ' (@dummy, value); Query OK, 3 Rows Affected (0.00 sec) Records:3 deleted:0 skipped:0 warnings:0 mysql> select * from test_main6;+-- -----+| Value |+-------+| A | | B | | C |+-------+3 rows in Set (0.00 sec) The key point here is the @dummy. For you, if the file has 10 columns, you only guide 2, 7, that is @dummy, column 2, @dummy, @dummy, @dummy, @dummy, column 7, @dummy, @dummy, @dummy </span>


The first part is how to handle the case where the number of columns in the data file is greater than the number of tables.


Suppose the data file is as follows: Book1.csv number, name, Description 1, test data 1, "test CSV file with comma" 2, test Data 2, "The test CSV file has" "double quotes" "" 3, test Data 3, "Test CSV file, there are commas and" "double quotes" "4, Test data 4, normal data mysql> CREATE TABLE test_book1 (id int, name VARCHAR, data varch AR (+)); Query OK, 0 rows affected (0.05 sec) below the lines terminated by ' \ r \ n ' is required line break symbol, for Windows line break below ignore 1 lines is the header row that ignores the first row. mysql> LOAD DATA INFILE ' f:/book1.csv ', into TABLE test_book1, fields TERMINATED by ', ' OPTION ALLY enclosed by ' "", lines terminated by ' \ r \ n ' Ignore 1 lines (ID, name, data); Query OK, 4 rows Affected (0.00 sec) records:4 deleted:0 skipped:0 warnings:0 mysql> select * from test_book1;+-- ----+-----------+--------------------------------+| ID | name |    Data |+------+-----------+--------------------------------+| 1 | Test Data 1 |    Test csv file with comma | | 2 | Test Data 2 |    "Double quotes" in the test CSV file | | 3 | Test Data 3 |    In the test CSV file, there are commas and "double quotes" | |4 | Test Data 4 | Normal data |+------+-----------+--------------------------------+4 rows in Set (0.00 sec) for you, you need to use IGN Ore 1 lines ignores the header row of the first row.

The second part, is the description, how to exclude the case of the title column.




Import CSV data in Python-mysql "Learning notes"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.