Question

I have a small txt(~130 kb) I'm trying to import but give me error: "Error creating table: A table must have at least 1 column"

if(mysql_num_rows(mysql_query("SHOW TABLES LIKE 'test'")) == 1){
   $sql = "LOAD DATA INFILE 'file.txt' INTO TABLE test IGNORE 1 LINES";

    if (mysqli_query($con,$sql)){
        echo "Import successfull.";
    }
    else{
        echo "Error importing: " . mysqli_error($con);
    }
}
else{ 
    $sql = "CREATE TABLE test"; 

    if (mysqli_query($con,$sql)){
        echo "Table created successfully";
    }
    else{
        echo "Error creating table: " . mysqli_error($con);
    }
}

The first line of the txt are column names. Can I create columns based on that name? Or do I have to create them manually? Thank you!

Was it helpful?

Solution

You cannot use Load Data infile to dynamically create columns for you. You have to create the columns beforehand. Think of it this way, creating the columns with the names given in the first line will not suffice, because we also need the column type associated (e.g. int, date, varchar etc.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top