Вы находитесь на странице: 1из 11

How to import EXCEL into mySQL

Posted on Thursday, 20. January 2011 by PeterS This is a workaround for (mostly) non-programmers, found in a forum for os Commerce but I thought, it might be helpful here, too.

Original article is at: http://www.aspfree.com/c/a/Database/Converting-Your-Excel-Worksheetinto-a-Working-MySQL-Database/ You can only export one worksheet at a time to your MySQL database. Your worksheet should be free from images, colors, wrapping text, and fancy fonts. The first row is considered your Heading row starting in A1. Steps to import a CSV file created in Excel into a MySQL table for a MySQL database stored on a server (not XAMPP localhost): 1. Create your Excel file using the EXACT field names as in your MySQL table. If you are importing into a table with many field names, you may want to EXPORT the table first, open in Excel and save it as your template. If you are adding new data, make sure you delete any data that might have exported with your field names. If your table has a Primary Key, it should be the first field listed. If you have it set to auto_increments, leave it blank in your Excel file. 2. Save it as a CSV file (SUGGESTION: you may want to save the Workbook the same name as your table in the same format. Ex: products_attributes).

Go to File -> save as CSV (Comma delimited). If there is a warning that says Do you want to keep the workbook in this format? Click YES and close the workbook. Click NO if there is a warning that asks Do you want to save the changes made to yourworkbook.csv?

3. Then go to http://csv2sql.evandavey.com/. This will convert csv data into MySQL insert statements.

Table Name: Type the EXACT table name you are importing to. CSV File: Browse to the location of the CSV file you have saved in the above steps. Mode: Insert Statements o If you are updating existing data, choose Updating Statement. Primary Key Field (Update Only): leave blank. After all are set, click GO Copy the created statement(s). You can leave off the following: Generated by http://csv2sql.com online CSV to SQL converter

4. 5. 6. 7.

Go to phpMyAdmin >> select your database Click on the SQL tab at the top Paste your statement(s) in the Run SQL query/queries box Click GO

Yes you can, save it as a Comma Separated Value(.csv) file. Open up PHPmyAdmin and I believe you have to have a corresponding table that has the same amount of fields that you have in your spreadsheet. Get into the database then into the matching table and click on import. Open up the .csv file and choose the CSV format. Here you have to match the way excel formatted the file, how the fields are terminated, enclosed in quotes, etc. It might take several attempts to do so I suggest doing it a test database first. Come to think of it you might be able to just create a table a dummy field, and choose "replace table data with file" - what I *think* happens here is it will take the first line of your .csv file and make it the names of the fields. Again test it first. It was only a couple of weeks ago that I did this and I forgot how exactly I did it. Having a senior moment;)

Lesson 18: Create databases and tables


In the previous lesson we looked at how to create a connection to a database server. Next step is to create databases and tables. We'll look at two ways to create databases and tables. First, how it is done in PHP, and then how it's made with the more user-friendly tool PhpMyAdmin, which is standard on most web hosts and in XAMPP. If you have a hosted website with PHP and MySQL, a database has probably been created for you already and you can just skip this part of the lesson and start creating tables. Again, you should consult your host's support pages for more information.

Create a database and tables with PHP


The function mysql_query are used to send a query to a MySQL database. The queries are written in the language Structured Query Language (SQL). SQL is the most widely used language for database queries - not only for MySQL databases - and is very logical and easy to learn. In this lesson and the next, you will learn the most important SQL queries. When creating a database, the SQL query syntax: CREATE DATABASE is used with the following

CREATE DATABASE database name

Logical and easy, right!? Let's try to put it into a PHP script:
mysql_connect("mysql.myhost.com", "user", "sesame") or die(mysql_error()); mysql_query("CREATE DATABASE mydatabase") or die(mysql_error()); mysql_close();

First, we connect to the MySQL server. Next, we create a database named "mydatabase". And finally, we close the connection to the MySQL server again. So far so good... but things become a little bit more complicated when we want create tables in PHP. When creating tables, we use the SQL query syntax:
CREATE TABLE ( column1_name column2_name column3_name ... ) table name DATA_TYPE, DATA_TYPE, DATA_TYPE,

CREATE TABLE with the following

table_name and column_name are of course the name of the table and the columns, respectively. DATA_TYPE are used to specify the data type to be inserted into the column. The most commonly used data types are:
INT For numbers without decimals DECIMAL For numbers with decimals CHAR Short text up to 255 characters TEXT

For plain text up to 65,535 characters LONGTEXT For long passages of text up to 4,294,967,295 characters Date For dates in the format YYYY-MM-DD Time For time in the format HH:MM:SS DATETIME For date and time in the format YYYY-MM-DD HH:MM:SS

All in all, logical and relatively easy. Let's try to put it into an example:
mysql_connect("mysql.myhost.com", "user", "sesame") or die(mysql_error()); mysql_select_db("people") or die(mysql_error()); mysql_query("CREATE TABLE MyTable ( id INT AUTO_INCREMENT, FirstName CHAR, LastName CHAR, Phone INT, BirthDate DATE PRIMARY KEY(id) )") Or die(mysql_error()); mysql_close ();

In the example, we start by connecting to the MySQL server. Next we use the function mysql_select_db to select the database "people". Then we create the table "persons" with 5 columns. Note that at the "id" column, we first use INT to specify that the column contains numbers and then add AUTO_INCREMENT to automatically increase the number and ensure a unique ID for each row. At the end, we use PRIMARY KEY to set the "id" column as the primary key. The primary key uniquely identifies each record (/row) in the table, which becomes very useful later when we update the database.

Create database and tables with phpMyAdmin


It can be useful to be able to create databases and tables directly in PHP. But often, it will be easier to use phpMyAdmin (or any other MySQL administration tool), which is standard on most web hosts and XAMPP. The screendumps below shows how to create a database and tables in phpMyAdmin. Start by logging onto phpMyAdmin. Often, the address will be the same as your MySQL server (eg. "http://mysql.myhost.com") and with the same username and password. In XAMPP, the address is http://localhost/phpmyadmin/. When you are logged on, simply type a name for the database and press the button "Create":

At some hosts, it's possible the have already created a database, and you may not have the rights to create more. If that is the case, you obviously just use the assigned database. To create a table, click on the tab "Databases" and choose a database by clicking on it:

Then there will be a box titled "Create new table in database", where you type the name of the table and the number of columns and press the button "Go":

Then you can name the columns and set the data type, etc., as in the SQL example above.

Notice, that here we also set "id" as (A_I).

PRIMARY KEY and uses

AUTO_INCREMENT

Now you have created your own database and table. In the next lessons, we look at how to insert, retrieve and delete data in a database

MySQL set auto increment in phpMyAdmin How to set MySQL auto increment in phpMyAdmin

A common practice in database design is to set primary keys (PKs) with auto increment enabled. This way, you don't have to worry about specifying a new unique primary key value each time you insert a new record in such table. While phpMyAdmin is a very powerful and easy to use MySQL database management tool, "where could I set auto increment in phpMyAdmin" is still a frequent question. And here is the solution. In the latest phpMyAdmin versions there is a new A_I Checkbox. Mark this option as enabled when creating or editing your primary key table column and that numeric field will automatically increment its value each time a new record is inserted.

You can check that the auto increment property was successfully setup in the EXTRA column of the table column properties (after selecting a table, inside the structure tab). If the auto_increment text appears here, the configuration was successful. In previous phpMyAdmin versions, auto_increment was an additional option inside the dropdown menu of the EXTRA category (the last column in the field creation menu). To access the "edit table field menu" you can click the pencil icon in the desired table field row, inside the Structure tab. Anyway, you can always run an SQL command to update the auto increment status of the desired column by selecting the SQL tab and writing an SQL query like this one:
ALTER TABLE `table_name` CHANGE `pk_column_name` `pk_column_name` INT(key_length) NOT NULL AUTO_INCREMENT

Just replace "table_name" by the name of the current table being edited, "pk_column_name" by the column name of your primary key column and "key_length" by your integer primary key length (the default int length is 11). You should also make sure that the auto incrementing field is in fact the primary key of the current table. You can reset the table primary key by clicking on the table key icon of the desired field row.

Take also into account that only one auto incrementing field may be specified in each MySQL table. Nevertheless, auto incrementing only makes sense when used with numeric primary keys, and only a primary key field may be specified for each MySQL table as well. Finally, if you want to change the current Auto Increment Value (i.e.: to make your auto increment field start from a specific numeric value) just select the phpMyAdmin Operations tab, type in the AUTO_INCREMENT field the new starting value of your auto incrementing field, and you are ready to go.

A Simple Practice of Using XAMPP for PHP-MySQL


December 11, 2010 by dito In this post, I will share about how to make a simple access from PHP page to MySQL database using XAMPP. There are three main steps for beginners: XAMPP installation, creating database using phpMyAdmin, and accessing database using PHP. XAMPP version that I use is 1.7.3, so it is perhaps different from current version. XAMPP Installation

Install XAMPP. After the installation finishes, the command prompt will appear and ask some options. I usually choose the default answers. Start XAMPP Control Panel. Make sure that Apache and MySql statuses are running. If they arent, its possible that there is another application using port 80, such as IIS or Skype. If you dont want to change the using of port 80 for the application, you can change the Apache port in C:\xampp\apache\conf\httpd.conf (for example, replacing Listen 80 with Listen 85 in the file will change Apache port to 85). Consequently, you have to include the port number in browser address, for example: http://locahost:85/. Click the two Admin buttons at Apache and MySql the make sure that you can see these two pages on your browser.

The working directory of web pages that will be created is located in C:\xampp\htdocs. First picture above shows that http://localhost/xampp/splash.php located in C:\xampp\htdocs\xampp\splash.php.

Creating Database using phpMyAdmin

Create new database. Fill the name of your database in the text box as shown below (for example: database school), and then click Create.

Create new table. Fill the name of your new table in the text box as shown below (for example: table students which has three fields), and then click Go.

On the next page, fill the three fields in the text boxes, and then click Save below the table.

This time, the text boxes that should be filled are Field (field name of the table), Type (field type), Length/Values (string size of the field), Null (the field may be null), Index: PRIMARY (setting primary key), and AUTO_INCREMENT (the field will be automatically filled by server using auto-increment number). The field can be edited by clicking the menu in column Action or below the table in the picture below.

To add new content to the table, click Insert tab, fill the fields, then click Go.

The result can be viewed by clicking Browse tab. You can create other tables by entering the database page which can be accessed at navigation menu located at the left of the web page. If you want, you can create relation design. Enter the database page. Click Create Relation button, then select referenced key and foreign key.

You can get the exported SQL file. Click Export tab in database page, change the configuration needed, then click Go. SQL file can be imported. Click Import tab in database page.

Accessing Database using PHP

Download file index.php (http://cid38a55214b54a920a.office.live.com/self.aspx/.Public/Blog%20-%20XAMPP/index.php) and add.php (http://cid-38a55214b54a920a.office.live.com/self.aspx/.Public/Blog%20%20XAMPP/add.php). Check the comments in the files if you want to get the explanation. Copy the files to a new folder in htdocs folder, for example: C:\xampp\htdocs\test. Open browser and go to the folder address, for example: http://localhost/test/.

Voil! You can show tables and add row of a table on a web page. Hopefully this is useful for beginners.

Foreign Key in phpMyAdmin (Xampp)


Posted by imdad on July 24, 2008 The following steps are required for you to be able to add foreign keys using phpadmin (in xampp).
1. Select any table and go to operations tab, if InnoDB is listed under storage engine drop down menu then jump to step 5. 2. So InnoDB is disabled in mysql engine, edit the \xampp\mysql\bi\my.cnf file and remove the hashes that are required to enable InnoDB (its self explanatory). 3. Add a line in the above file, default_storage_engine=InnoDB (This makes InnoDB the default storage engine). 4. Restart mysql from the xampp control panel or from MS services. 5. Make sure, both PARENT and CHILD tables are of storage type InnoDB. 6. REFERENCED key (in parent) should be primary and REFERENCING key (in child) should be index. 7. In the CHILD tables structure view, click on the link relations view (it lies above add fields). 8. In the row corresponding to the REFERENCING key, select the REFERENCED key from the second column drop box. 9. Click go .. youll see that the required query is executed.

For storage type information go here.

Вам также может понравиться