Importing excel data in mysql database [duplicate]

2020-02-29 03:31发布

I have a excel file with about 5000 rows to be insersted at one of my mysql databse table, can anyone give quick and dirty solution ? Thanks

标签: mysql excel
6条回答
对你真心纯属浪费
2楼-- · 2020-02-29 03:46

Quick and dirty:

Put a calculated column in Excel to generate a "insert" statement. Then COPY+PASTE all sentences into MySQL command interpreter.

Your cell should have a formula like this:

=CONCATENATE("insert into your table (col1, col2) values (", A1, ", ", B1, ");")

Then copy the formula on all the rows and you'll have your script.

Other quick & dirty:

Open your Excel file with ACCESS, then use "Export to ODBC" function to put your data in MySQL. It's a little bit more complicated since you will have to setup your ODBC driver and Connection but if you plan to do this regularly, it might be a better choice.

查看更多
聊天终结者
4楼-- · 2020-02-29 04:01

You can export the Excel file to a CSV file, then import it to the MySQL database using phpMyAdmin.

查看更多
家丑人穷心不美
5楼-- · 2020-02-29 04:04

Unless you've some additional tools lying around, there is no quick way.

You could write an excel macro and transfer the data via ODBC.

You could export it as CSV then import it using PHPMyAdmin

You could export the CSV then write an awk script to convert the csv data into a set of insert statements

You could use (insert language of choice here)

Do you have an ODBC link? Do you have any programming languages? PHPMyAdmin? Some other MySQL tool?

查看更多
虎瘦雄心在
6楼-- · 2020-02-29 04:12

I have done this in the past by exporting the file as a CSV. Creating your table in MySQL, reading the CSV line by line in PHP and using explode to build your query.

$file = fopen("myfile.csv", "r");
$sql = "INSERT INTO table(field1, field2, field3...";
while(!feof($file))
  {
      $fields = explode(',', fgets($file));
      $sql .= "VALUES("
      foreach($fields as $field)
      {
         $sql .= "$field, ";
      }
      $sql = substr($sql, 0, -2);
      $sql .= "), ";
  }
  $sql = substr($sql, 0, -2);
 fclose($file);
  mysql_query($sql);
查看更多
趁早两清
7楼-- · 2020-02-29 04:13

Save as CSV; use the "Import text file" feature of a graphical mySQL client like HeidiSQL to import.

查看更多
登录 后发表回答