SERVER142 ... con manos de programador

martes, noviembre 08, 2005

Migrar datos de Access a MYSQL

Migrar datos de Access a MYSQL

Cuando comenzamos a desarrollar aplicaciones web, es normal comenzar con Access, pero cuando la cosa se pone seria es mejor emigrar los datos a un gestor autentico de bases de datos, por costos es logico pensar en MySql.
MySql a diferencia de Access, cuenta con carácteristicas que permitiran a la aplicación mejorar su desempeño, así mismo prevendrá posibles fallos que Access pese a su amigable interfaz y fácil manejo llegará a tener ante un alto contenido de información.
A continuación muestro una de las distintas formas de emigrar sus datos de manera sencilla y con únicamente las dos herramientas principales, es decir sin ningún programa adicional aparte de MySql y Access.

Comenzámos

1.-Debemos checar la integridad de la tabla de la BD que queremos importar muchas veces tenemos un desorden en los datos por lo que debemos verificar que los campos sean respetados como fueron declarados desde un inicio es decir que en un campo que diga numero halla realmente números, en un campo que sea señalado como Not Null realmente siempre tenga información etc.



2.-DESPUES DE HABER CHECADO LA INTEGRIDAD DE LA TABLA PROCEDEMOS A CREAR NUESTRO ARCHIVO DE TEXTO

SEGUIMOS LAS SIG IMAGENES:



DAMOS UN CLICK DERECHO SOBRE LA TABLA QUE VAMOS A EXPORTAR



SE DEBE ESCOGER EL ARCHIVO DE TEXTO ES DECIR PARA QUE EL ARCHIVO SEA DE LA FORMA "c:\articulos.txt"




EN ESTA VENTANA VAMOS A ESCOJER LA OPCION Delimited "Delimitado"



SOLO MARCAREMOS TAB "TABULACION" TODO LO DEMAS VA EN BLANCO COMO SE VE EN LA PANTALLA



HEMOS GENERADO NUESTRO ARCHIVO DE TEXTO PLANO .

3.-AHORA PROCEDEREMOS A CREAR NUESTRA TABLA EN MYSQL DONDE VACIAREMOS LA INFORMACION CONTENIDA EN NUESTRO ARCHIVO DE TEXTO .ES IMPORTANTE ESTE PASO YA QUE DEBE HABER UNA CONCORDANCIA A LA HORA DE DISEÑAR NUESTRA BD BASANDONOS EN EL ARCHIVO DE TEXTO QUE GENERAMOS.

DESDE EL SHELL DE MYSQL PODEMOS REALIZAR LA TAREA, EN CASO DE QUE TUVIERAMOS UN PROGRAMA TAL COMO MYSQLYOG O PHPMYADMIN O CUALQUIER OTRO QUE PUEDA ENVIAR QUERY'S A LA SHELL DEL SERVIDOR TENDRÍAMOS QUE COPIAR LOS QUERYS EN LA PANTALLA SEGUN CORRESPONDA.
PARA ESTE EJEMPLO LO HAREMOS DESDE LA MISMA SHELL DE MYSQL.

EJEMPLO

mysql> CREATE TABLE MARCAS (
-> ID INT(20) NOT NULL AUTO_INCREMENT,
-> MARCA VARCHAR(50) NOT NULL,
-> PRIMARY KEY (ID)
-> ) ;
Query OK, 0 rows affected (0.86 sec)
mysql>


4.- YA HABIENDO CREADO LA TABLA AHORA PROCEDEMOS A LLENARLA CON LA INFORMACION DE NUESTRO ARCHIVO.
DE NUEVO EN EL SHELL DE MY SQL PONEMOS EL SIG QUERY:

mysql>LOAD DATA INFILE 'C:\articulos.txt'
INTO TABLE articulos ;

Si file_name.txt se encuentra en un directorio, debes poner la ruta 'c:\ruta\file_name.txt' y si no te funciona utiliza 'c:/ruta/file_name.txt' cambiando '\' a '/'.
Si todo lo anterior lo hicimos bien debemos de haber obtenido un resultado satisfactorio puedes checar el contenido de la tabla a través de un query desde el shell de mysql.
Es todo.