Excel CSV to Mysql Database - mysql

i am planning to get some Data Entry done for the records of Restaurant info which i later want to upload to MySQL table.
Is there some kinda rules i need to follow because i read some place that we have to mention some kinda delimiter like , ; etc. not sure how to do that in Excel.
Secondly can i use Google Forms which stores question into Google sheets for doing the same as filling the form will be a lot faster and typing it out in excel :D

You can save the file (in Excel and also in Google spreadsheet) as a CSV. You can load the CSV into mysql using the LOAD DATA INFILE syntax. For example:
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table
FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Excel is the enemy of database programmers. If you simply ask someone to fill in an Excel spreadsheet expect 1) data in the wrong column for some rows, 2) text in fields that must be numbers (eg "bet 10 and 20", 3) Incomplete date fields (eg "10/2010"), text that is too long to fit your column definition and other problems.
However, if you have clean data you can Save As CSV in Excel and that will produce a file that any tool that can read CSV will be able to parse and load.

Related

Need to load excel data into sql, then I need to use sql data for forecasting

I am interning at a company and they want to load their excel data (with formulas) into SQL. There are 2 types of excel sheets. The first ones contain their initial budgets, while the second ones contain the total expenses by each department. After loading the data into SQL, they want to use the first sheets (containing their budgets) to forecast, and compare them to the second sheets (containing the real expenses).
I was thinking about saving the data as csv (not sure if the formulas will let me do it). Then pulling everything into MYSQL using Workbench. After that, I am not sure on how to forecast with the data from the first sheet for comparison.
I am not sure if I should use MySQL and Workbench due to licensing issues, I though about using PostgreSQL and pgAdmin
ny ideas will be greatly appreciated, thanks
Perhaps this can help:-
LOAD DATA INFILE 'c:/tmp/file_name.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ',' /* if applicable */
ENCLOSED BY '"' /* if applicable */
LINES TERMINATED BY '\n'
IGNORE 1 ROWS; /* IF you want to exclude header row */
I'm doing that task really often too. I found a lot of great conversion applications who help you to convert your Excel sheets to SQL or MySQL Databases and nearly any Database-Management-Application has such a tool too. So you could search for that in your management-studio.
Otherwise I recommend you to read this tutorial by MySQL itself, where they show how to convert a Excelsheet to a MySQL Database:
https://dev.mysql.com/doc/mysql-for-excel/en/mysql-for-excel-export.html
Other converters:
https://www.rebasedata.com/convert-excel-to-mysql-online;
https://sqlizer.io/#/;
Then you have to simply import them into your database by using the standard Database import tool.

Is there anyway to import a csv file that contains multiple worksheets using LOAD DATA INFILE

The load data infile only imports the first sheet.
"LOAD DATA INFILE 'C:\Users\\user\\Desktop\\example.csv'
INTO TABLE exampletable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n'
IGNORE 1 LINES (Id, Name)";
Is it possible to upload the 2nd sheet simultaneously with the first sheet?
When you export a worksheet in an Excel workbook to CSV, you usually do so one sheet at a time. So, to do what you want you might have to open the first CSV file and append the second one at the end. This assumes that both sheets would have the same number and types of columns. You also might have to remove the header row from the second CSV file before combining it.
It should be possible to write a VBA script to iterate all sheets and generate a single CSV output. But that would be a bit of work, and perhaps not worth it for you to do.

How to get MySQL to load data from a TEXT column that contains CSV data as multiple columns?

We want our users to be able to upload CSV files into our software and have the files be put into a temporary table so they can be analyzed and populated with additional data (upload process id, user name, etc).
Currently our application allows users to upload files into the system, but the files end up as TEXT values in a MySQL table (technically BLOB, but for our purposes, I will call it TEXT, as the only type of files I am concerned with are CSVs).
After a user uploads the CSV and it becomes a TEXT value, I want to take the TEXT value and interpret it as a CSV import, with multiple columns, to populate another table within MySQL without using a file output.
A simple insert-select into won't work as the TEXT is parsed as one big chunk (as it should be) instead of multiple columns.
insert into db2.my_table (select VALUE from db1.FILE_ATTACHMENT where id = 123456)
Most examples I have found export data from the DB as a file, then import it back in, i.e. something like:
SELECT VALUE INTO OUTFILE '/tmp/test.csv'
followed by something like:
LOAD DATA INFILE '/tmp/test.csv' INTO TABLE db2.my_table;
But I would like to do the entire process within MySQL if possible, without using the above "SELECT INTO OUTFILE/LOAD DATA INFILE" method.
Is there a way to have MySQL treat the TEXT value as multiple columns instead of one big block? Or am I stuck exporting to a file and then re-importing?
Thanks!
There is flaw in your data load approach.
Instead of keep each row in a single column, keep each value in respective column.
For example, suppose csv file contains n column. Create a table with n columns.
LOAD DATA INFILE '/tmp/test.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS; // for ignoring header if present in csv file.

How do I add/append data from an excel sheet to a table?

The Excel sheet is huge; contains around 40,000 rows. Both the table and the excel sheet have an equal number of columns.
I am using Ubuntu Linux, Libre Office and MySQL workbench. All I could find when I searched on the web was to import using MySQL Server Studio(I don't think it is available for Ubuntu), and queries containing "Microsoft.Jet-OPENROWSET" query. I am new to performing tasks other than the basic operations in excel/SQL.
And also, what format should the excel be in? Should it contain the column names explicitly?
Is there a simple way to perform this operation?
Thank You in Advance.
You can use the following:
LOAD DATA LOCAL INFILE '/path/to/your/file' INTO TABLE tablename
FIELDS TERMINATED BY ',' lines terminated by '\n';

How to export from excel to MySql with dynamic fields inside

I have a problem,
I want to export from excel to MySql database table, but the destination table field is dynamic.
Let's say:
table A for storing field_information (e.g. field_name, field_type)
table B for storing field_answers (e.g. field_info_id, value)
example of excel spreadsheet file (which I convert into cvs format):
name,school,news;
"test","test_school","test_news";
I know I can export from excel to MySql (with static field) using following syntax:
LOAD DATA LOCAL INFILE '\\path' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'
but, what if dynamic field?
How could I programmatically know which row of the spreadsheet should go to which database table?
Anyone can help?Thanks.
How will you programmatically know which row of the spreadsheet should go to which database table? How about some example rows of the spreadsheet?
Assuming I'm understanding your question correctly,
I'd sort the spreadsheet and then do two imports, to the two db tables.