manxing
manxing

Reputation: 3305

load data into table in Mysql

I created 4 columns for a table

cur.execute("""CREATE TABLE videoinfo (
    id INT UNSIGNED PRIMARY KEY AUTO INCREMENT, 
    date DATETIME NOT NULL, 
    src_ip CHAR(32), 
    hash CHAR(150));
""")

I have a .txt file which has three columns of data inside. I want to use LOAD DATA LOCAL INFILEcommand to insert data, but the problem is ,the table I created now has four columns, the first one is the id, so, can mysql automatically insert data from the second column or extra command is needed?

Many thanks!

Upvotes: 0

Views: 166

Answers (1)

outis
outis

Reputation: 77400

AUTO INCREMENT isn't valid syntax. If you check MySQL's documentation for the CREATE TABLE statement, you'll see the proper keyword is AUTO_INCREMENT.

Additionally, date is a keyword, so you'll need to quote it with backticks, as mentioned on the MySQL identifier documentation page. The documentation also lists all keywords, which must be quoted to use them as identifiers. To be safe, you could simply quote all identifiers.

To insert data only into some columns, you can explicitly specify columns. For LOAD DATA INFILE:

LOAD DATA INFILE 'file_name'
    INTO TABLE videoinfo
    (`date`, src_ip, hash)

For the INSERT statement:

INSERT INTO videoinfo (`date`, src_ip, hash)
  VALUES (...);

This, too, is revealed in the MySQL manual. Notice a pattern?

Upvotes: 1

Related Questions