Skip to content
Advertisement

Import csv file to MariaDB without knowing the exact number of columns [closed]

i need to import a csv file to a mysql database, the problem is: The csv file cointains too many days horizontally, that means that i need to create fields in my DB according to the fields that exist in the excel file.

I found this script and was trying to adpat it to my case but i keep getting the following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘;Turno;10/06/2022;11/06/2022;12/06/2022;

I will put an image so that you have a clue off what im doingenter image description here

JavaScript
JavaScript

Advertisement

Answer

Assume you have a well formatted csv. Mainly first line should indicate each column content and also assume unique tags.

JavaScript

DB design can be:

JavaScript

Data can be added as:

JavaScript

One of main advantage is that storage is independent of file-contents (irrelevant how many columns). Also it’s not required each time a file is parsed to create a new table.

Notice: Db design is the key !

Imagine 100 distinct files as column numbers so need to create 100 additional tables ?!
Or maybe one table with more then 100 columns for who knows what a files can contain ?!
(hard to read and inefficient as storage -> affect query performance. Many columns can be null)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement