How can I (best) convert Excel to MySQL? [duplicate] - php

Can any one explain how to import a Microsoft Excel file in to a MySQL database?
For example, my Excel table looks like this:
Country | Amount | Qty
----------------------------------
America | 93 | 0.60
Greece | 9377 | 0.80
Australia | 9375 | 0.80

There's a simple online tool that can do this called sqlizer.io.
You upload an XLSX file to it, enter a sheet name and cell range, and it will generate a CREATE TABLE statement and a bunch of INSERT statements to import all your data into a MySQL database.
(Disclaimer: I help run SQLizer)

Below is another method to import spreadsheet data into a MySQL database that doesn't rely on any extra software. Let's assume you want to import your Excel table into the sales table of a MySQL database named mydatabase.
Select the relevant cells:
Paste into Mr. Data Converter and select the output as MySQL:
Change the table name and column definitions to fit your requirements in the generated output:
CREATE TABLE sales (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Country VARCHAR(255),
Amount INT,
Qty FLOAT
);
INSERT INTO sales
(Country,Amount,Qty)
VALUES
('America',93,0.60),
('Greece',9377,0.80),
('Australia',9375,0.80);
If you're using MySQL Workbench or already logged into mysql from the command line, then you can execute the generated SQL statements from step 3 directly. Otherwise, paste the code into a text file (e.g., import.sql) and execute this command from a Unix shell:
mysql mydatabase < import.sql
Other ways to import from a SQL file can be found in this Stack Overflow answer.

Export it into some text format. The easiest will probably be a tab-delimited version, but CSV can work as well.
Use the load data capability. See http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Look half way down the page, as it will gives a good example for tab separated data:
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\'
Check your data. Sometimes quoting or escaping has problems, and you need to adjust your source, import command-- or it may just be easier to post-process via SQL.

There are actually several ways to import an excel file in to a MySQL database with varying degrees of complexity and success.
Excel2MySQL. Hands down, the easiest and fastest way to import Excel data into MySQL. It supports all verions of Excel and doesn't require Office install.
LOAD DATA INFILE: This popular option is perhaps the most technical and requires some understanding of MySQL command execution. You must manually create your table before loading and use appropriately sized VARCHAR field types. Therefore, your field data types are not optimized. LOAD DATA INFILE has trouble importing large files that exceed 'max_allowed_packet' size. Special attention is required to avoid problems importing special characters and foreign unicode characters. Here is a recent example I used to import a csv file named test.csv.
phpMyAdmin: Select your database first, then select the Import tab. phpMyAdmin will automatically create your table and size your VARCHAR fields, but it won't optimize the field types. phpMyAdmin has trouble importing large files that exceed 'max_allowed_packet' size.
MySQL for Excel: This is a free Excel Add-in from Oracle. This option is a bit tedious because it uses a wizard and the import is slow and buggy with large files, but this may be a good option for small files with VARCHAR data. Fields are not optimized.

Not sure if you have all this setup, but for me I am using PHP and MYSQL. So I use a PHP class PHPExcel. This takes a file in nearly any format, xls, xlsx, cvs,... and then lets you read and / or insert.
So what I wind up doing is loading the excel in to a phpexcel object and then loop through all the rows. Based on what I want, I write a simple SQL insert command to insert the data in the excel file into my table.
On the front end it is a little work, but its just a matter of tweaking some of the existing code examples. But when you have it dialed in making changes to the import is simple and fast.

the best and easiest way is to use "MySQL for Excel" app that is a free app from oracle. this app added a plugin to excel to export and import data to mysql. you can download that from here

When using text files to import data, I had problems with quotes and how Excel was formatting numbers. For example, my Excel configuration used the comma as decimal separator instead of the dot.
Now I use Microsoft Access 2010 to open my MySql table as linked table. There I can simply copy and paste cells from Excel to Access.
To do this, first install the MySql ODBC driver and create an ODBC connection.
Then in access, in the "External Data" tab, open "ODBC Database" dialog and link to any table using the ODBC connection.
Using MySql Workbench, you can also copy and paste your Excel data into the result grid of MySql Workbench. I gave detailed instructions in this answer.

Fastest and simpliest way is to save XLS as ODS (open document spreasheet) and import it from PhpMyAdmin

For a step by step example for importing Excel 2007 into MySQL with correct encoding (UTF-8) search for this comment:
"Posted by Mike Laird on October 13 2010 12:50am"
in the next URL:
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

You could use DocChow, a very intuitive GIU for importing Excel into MySQL, and it's free on most common platforms (including Linux).
More especially if you are concerned about date, datetime datatypes, DocChow easily handles datatypes. If you are working with multiple Excel spreadsheets that you want to import into one MySQL table DocChow does the dirty work.

Step 1 Create Your CSV file
Step 2 log in to your mysql server
mysql -uroot -pyourpassword
Step 3
load your csv file
load data local infile '//home/my-sys/my-excel.csv' into table my_tables fields terminated by ',' enclosed by '"' (Country, Amount,Qty);

Another useful tool, and as a MySQL front-end replacement, is Toad for MySQL. Sadly, no longer supported by Quest, but a brilliant IDE for MySQL, with IMPORT and EXPORT wizards, catering for most file types.

If you are using Toad for MySQL steps to import a file is as follows:
create a table in MySQL with the same columns that of the file to be imported.
now the table is created, goto > Tools > Import > Import Wizard
now in the import wizard dialogue box, click Next.
click Add File, browse and select the file to be imported.
choose the correct dilimination.("," seperated for .csv file)
click Next, check if the mapping is done properly.
click Next, select the "A single existing table" radio button also select the table that to be mapped from the dropdown menu of Tables.
Click next and finish the process.

If you don't like plugins, VBA and external tools, I have an excel file that using formulas only allows you to create INSERT/UPDATES. You only have to put the data on the cells:
As an extra, there's another tab in the file to CREATE TABLES:
The file can be found on the following link:
EXCEL FILE

I've had good results with the Tools / Import CSV File feature in HeidiSQL, with CSV files directly exported from Excel 2019 with "Save As..."
It uses LOAD DATA INFILE internally but with a GUI interface and also analyzes the CSV file before passing it to LOAD DATA INFILE so it can, for example, create the table using the first row as column names and guessing the column data type (<New table> option as shown in the picture)

Related

How to import Wordpress database to pure PHP MySQL website?

I am currently in a project which develops (actually re-designs) a news company website. Their old website had been developed using Wordpress (Latest version). And, now we are developing brand new custom design website using PHP and MySQL. But, they have over 10,000 news (posts) on old wordpress database. All I need to know is, how do we import all those post data to our main database? Our database and table structure is way different from wordpress one. Are there any SQL commands to import just only single or couple of columns data and paste them to another database table? Your help is highly appreciated.
Pull them down into a CSV, update the fields as required, import them into MySQL...
A CSV is a file of Comma separated values: https://en.wikipedia.org/wiki/Comma-separated_values
Plugin to get Posts as CSV: https://wordpress.org/plugins/simple-csv-xls-exporter/
Edit the field names (in a text editor or something that can edit CSVs and save them correctly), remove data you don't require etc etc. then import into your database.
Here's a handy link to import CSV into MySQL: How to import CSV file to MySQL table
Does that make sense?
If you know how to use MySQL , you make queries that can fetch the data and like that you can fetch all the data from MySQL import it in a way that you need in your new website and export it there. if You don't understand.
Step 1 :
Know that is required this is very basic step, you should know what you need and make a list of it.
Step 2 :
Make MySQL queries and fetch the data according to your database structure, use excel if your not so good in MySQL , organized as data as requirement
Step 3:
Now you will have all the data, you can either convert that to csv or sql format and insert in to your database easily.

Export Mysql data to a customized XLS file using php

First of all, I do know the process of exporting MySQL data to excel file using php. But I have some specific requirement and I want to know if it can be done or not. I have already googled, but didn't get any specific information or method regarding the same.
This is my xls template :
Overview:
Column C is fixed and others are scrollable.
There are basically couple of organizations.
Data starting from COL-C8 to COL-L8 is exactly stored on a MySQL Table of an organization (e.g. Organization1).
Data of COL-C1 to COL-C5 & D1 to D5 is on another MySQL Table of Organization1.
So for every organization there are 2 different MySQL tables.
Requirement :
The goal is to export data from MySQL and add it to the excel file according to the image above.
Problem :
Adding data from MySQL table to excel is pretty easy if the requirement is to just replicate same on the excel sheet. But I have no idea how to do this customization.
Like pulling data from two different tables and adding them to different columns in an excel sheet.
Please let me know if it is possible. And if yes, then kindly direct me to some references on how to achieve this.
Thank You!
Try PHPExcel. This library allows you to read and write from/to .xls files.

import client data from csv into mysql

I am trying to import write a script that imports a csv file and parses it into mysql, then imports it in the db, I came across this article,but it seems to be written for ms sql, does anyone know of a tutorial for doing this in mysql, or better still a libary or script that can do it ?.
Thanks :-)
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
Using the LOAD DATA INFILE SQL statement
Example :
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
If you are looking for script / library. I want to refer below link. here you can find :
PHP script to import csv data into mysql
This is a simple script that will allow you to import csv data into your database. This comes handy because you can simply edit the appropriate fields, upload it along with the csv file and call it from the web and it will do the rest.
It allows you to specify the delimiter in this csv file, whether it is a coma, a tab etc. It also allows you to chose the line separator, allows you to save the output to a file (known as a data sql dump).
It also permits you to include an empty field at the beginning of each row, which is usually an auto increment integer primary key.
This script is useful mainly if you don’t have phpmyadmin, or you don’t want the hassle of logging in and prefer a few clicks solution, or you simply are a command prompt guy.
Just make sure the table is already created before trying to dump the data.
Kindly post your comments if you got any bug report.

how to converted the excel file into sql file

hi i want to converted my excel file into sql file can any one know what should i do for converting? i dont know how to converted.
thanks
Use sql convertor from below link
http://www.sqlconverter.com/
OR
You need to convert the .xls file to .csv(comma seperated file) and then use the loader to load the data if you want to load the .xls data to database.
Try Navicat products.
I used Navicat for MySQL for example and it provides great flexibility in Importing Excel sheets in Mysql tables.
From there you can run any type of SQL query
Here is web application tool that allows fast Excel to MySQL conversion.
Excel data does not have a Schema by default and this tool allows you to define it interactively with the help of a wizard.
Input
An Excel (xls / xlsx) data file, whose format is given in the documentation they provide.
Output
A .sql export file (which can be imported into a MySQL database) is available for download after the conversion is done. (And it works pretty fast)
The other option is to export to CSV first. Excel can export to CSV and then MySQL can import CSV via LOAD command or PHPMyAdmin. The schema should be defined before. Its a little longer process.

How to convert excel file into mysql database? [duplicate]

This question already has answers here:
How to import an excel file in to a MySQL database
(15 answers)
Closed 9 years ago.
my boss wants me to create a mysql database for an existing excel file. I was wondering if there are any ways to do convert or import excel file? I have searched google but didn't find anything useful. I appreciate for any reply....Thanks.
Save as CSV
Use a COPY sql statement
Profit
If you have a web server up and running with PHP support, I highly recommend phpMyAdmin.
Configure it to connect to MySQL
Create the Database and table
Click the import tab and you can import a CSV.
If this is a simple one-time import this probably isn't worth the effort. If, on the other hand, you will ever have to work with MySQL again, you will never regret the time to install and configure phpMyAdmin.
PHPMyAdmin can import CSV files, as well as Excel files (though I've never tried it)
First you need to create your datebase, and add a table. There must be as many fields in that table as there are columns in your Excel document (yes, I know you know)
Then select that database and table in phpmyadmin and use the "Import" tab.
I wrote a tool that will let you do sql queries against a csv file. The output is saved as a csv as well. Maybe you will find it useful.
http://whitlock.ath.cx/EasyCSV/
From Excel, export the sheet as a text file. In MySQL, use LOAD DATA INFILE to import the text file.
easiest way to do it would be this:
insert into Table (col 1,col 2,col 3...col n) values (val1,...valn);
basically:
do 2 for loops in your excel:
dim i,j
dim sqlString,sqlBase
sqlString=""
sqlBase="insert into yourTable (col1....coln) values ("
for i=1 to myRowVariable
sqlString=""
for j=1 to myColVariable
if j=myColVariable then
sqlString=sqlString & Cells(i,j).value & ");"
else if j=1 then
sqlString=sqlBase & sqlString & ","
else
sqlString=sqlString & Cells(i,j).value & ","
end if
Next j
Next i
'write sqlString into a txt or something
this will do what you need in a bootstrap but fast and very intuitive way.
You can use an ODBC driver to "mount" an Excel file as database and then make SQL queries to it. All you need then, is a simple migration tool, to copy the tables to another databases system.
I believe there's even an mysqldump-like tool for ODBC driven databases.
A low tech solution would be to use the concatenation function in excel to convert the data into a series of insert statements and then copy and paste them into mysql query analyzer or whatever client you are using.

Categories