简体   繁体   中英

Migrating table data from a MySQL database to another MySQL database

I'm having some trouble understanding how this works. I know you can easily migrate a new database to MySQL through some of workbench's tools, but what I want to do here, is to get certain data from tables of another database to insert it into my own database. The issue is that this other database does not have the same tables or the same amount of data. Some of it is useless to me, some of it doesn't fit with my tables.

So how does one regularly get around to doing this? I basically need something that can capture data, no matter how it may have been arranged in the past, and place it into my database. It sounds hard to do when the program doesn't know what to expect, so basically that is my issue. I'd appreciate it if someone could point me in the right direction.

Can't you do? Perhaps I missed somthing

-- First create your new table, avoid the columns that you don't care about.    
CREATE TABLE newTable......

-- select from the old table only the columns that you need and insert them in the new table
    INSERT INTO newTable(col1, col2, col3)
SELECT col1, col2, col3
FROM oldTable
WHERE .....

And if you want to merge data between multiple columns use CONCAT_WS

   INSERT INTO newTable(col1, col2, col3)
SELECT CONCAT_WS(' ', col1, col2) AS col1, col3
FROM oldTable
WHERE .....

What you want to do is an import program. I'm not sure there an application out there that does this kind of thing automatically.

Basically, you write a program that connects to you first database, extract the data, connect to the second database and generate insert statement from the extracted data.

Here is a simple python pseudo code that coudl inspire you:

import mysql.connector

cnx = mysql.connector.connect(user='username', password='password',
                              host='127.0.0.1',
                              database='yourdatabase')
cnx2 = mysql.connector.connect(user='username2', password='password2',
                              host='127.0.0.1',
                              database='yourdatabase2')
cursor = cnx.cursor()
cursor.execute("SELECT * FROM ...")
for (data) in cursor:
  cursor2 = cnx2.cursor()
  cursor2.execute("INSERT INTO ...")       // Use the data variables
  cursor2.close()
cursor.close()
cnx.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM