Import data from CSV
We will show you how to import data from CSV, or XML files, to your database using DMXzone Data Importer. We have a database table, where we store our users data. Currenly, we have only one record in it. We like to import our users data in it, from a CSV file.
On our page, we've added a form containing a file upload field, and a submit button. We will use this file upload form in order to upload CSV files, and import their content into the database (1). First, let's create a server action file (2). And add a name for it. Then, select globals (3). Link the page containing your form and import form fields (4). Right click steps, when you are done importing the form fields (5).
And add a database connection.
And add a database connection (1). Select your database connection, or create a new one, if you haven't done this already (2). Right click your database connection step (3).
And add file upload (1). Select your upload field (2). Expand globals. And you will see your upload field, imported under post variables. Select it. And click the select button (3).
Then, select an upload path.
Now, right click the file upload step (1). Open the data management menu (2) And select - import CSV file (3).
Then select a path, for the file to be imported. Click the dynamic data button, in order to do this (1). Select the path binding, returned by the file upload step (2). And click select (3).
Check the contains header checkbox, if your CSV file contains a header row containing column names (1). If your CSV file does not have a header row with column names then you will have to add the column names manually. Click the add new button, in order to add a column. Note, that you must add all of your csv file columns here (2). And then add a name, for the first column of your csv file. Do the same for the rest of the columns (3).
Here are all of our CSV columns. You can add edit or remove columns if needed (1). Also, you can change the default delimiter (2). Right click the import step (3). Open the core actions menu (4). And add a repeat (5).
Select an expression for the repeat (1). You should select the import step, as an expression (2). click the select button (3).
And right click steps, inside the repeat (1). Then add database insert (2).
And setup insert options (1). Select the database table, where you want to insert the data and add it (2). Then, add the table columns which you will need to populate. We add all of them (3). Select the first one (4). And click the dynamic data, for its value in order to select a column from the csv file (5).
Select the corresponding column, returned by the repeat step (1). And click the select button (2)
Do the same, for the rest of the database columns (1). And click the OK button, when you are done (2).
Save the action file (1). And create a new server action executor for it (2). Add a name for it (3). Select to run it on form submit (4). then select your server action, from the dropdown (5). Bind the executor to your form (6). And set the method to post (7) Now, we will add notification, when the data has been successfully imported. This is not mandatory, but it is nice to have. Open the options tab (8). Then, open behaviors (9).
And select the on success event (1). Under available behaviors, select control html5 responsive notify 2 and add it (2).
Click the ok button when you are done (1).
click ok, in order to attach this behavior to your server action. And click ok, when you are done. Now, let's preview the results. Browse to your csv file select it and click the submit button. As soon as the import is finished you will see the notification. Now, let's open our database table and refresh its content. Here is our newly imported data! That's how easy it is to import data, from CSV, or XML files.
Comments
Be the first to write a comment
You must me logged in to write a comment.