[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Perl: regexp escape \'



Davide Prina wrote:

il comando con sed che ti ho spedito dovrebbe fare esattamente quello.
Ma lo hai guardato?


Hai ragione, facevo confusione io.

In questi giorni ho "tradotto" il tuo script in Perl e l'ho integrato
nel mio script per la conversione di un dump file MySQL in un database
SQLite ().

Riporto di seguito lo script nel caso fosse utile a qualcuno. Ogni
suggerimento è ben accetto. Non voglio attribuirmi meriti non miei:
lo script è basato su quello trovato qui:
http://www.sqlite.org/cvstrac/wiki?p=ConverterTools

Per usarlo occorre prima creare il dump file (serve MySQL 4.1.0 o sup.):

$ mysqldump --compact --compatible=ansi --default-character-set=binary --user=[NOME_UTENTE] --host=[NOME_HOST] [NOME_DATABASE] > dump_file.sql

A questo punto si può usare lo script (nell'esempio l'ho chiamato mysql2sqlite) così:

$ ./mysql2sqlite dump_file.sql sqlite.db

E' necessario il tool a riga di comando sqlite3.

A presto,

Giuseppe
--------------------------8<-------------------------------------------
#!/usr/bin/perl

#**********************************************************************#
#                                                                      #
# MySQL to SQLite translator vers. 0.1                                 #
# ==================================================================== #
#                                                                      #
# Copyright (c) 2005 by Giuseppe Bordoni (geppo<at>geppozone<dot>com)  #
# http://www.geppozone.com                                             #
#                                                                      #
# This program is free software. You can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License.       #
#                                                                      #
#**********************************************************************#

# Verifica parametri
if (@ARGV !=2) {
	die ("\nMySQL to SQLite translator\nUsage: $0 MySQLDump SQLiteDB\n\n");
};

$mysqlDump = @ARGV[0];
$sqliteDB = @ARGV[1];

if (!(-e $mysqlDump)) {
	die ("\nMySQL dump file not found\n\n");
};

# Apertura del file sorgente
open (SOURCE, $mysqlDump) || die ("\nError opening MySQL dump file\n\n");

print "\nImporting...\n";
$testo = "BEGIN;\n";
while ($riga = <SOURCE>) {

	# Eliminazione eventuali ritorni a capo in stile DOS
	$riga =~ s/\r\n/\n/g;
	
	# Eliminazione definizione KEY ed UNIQUE KEY
	if ($riga !~ /^  (UNIQUE )?KEY "/) {
    	
		# Formattazione comando INSERT	
		if ($riga =~ /^(INSERT.+?)\(/) {
			$insertCmd = $1;
        	
			# Eliminazione escape apice singolo e back-slash
			$riga =~ s/\\\\'/<STRINGA_NON_PRESENTE_NEL_DB>/g;
			$riga =~ s/\\'/''/g;
			$riga =~ s/<STRINGA_NON_PRESENTE_NEL_DB>/\\'/g;
			$riga =~ s/\\\\/\\/g;
			# Eliminazione escape doppi apici
			$riga =~ s/\\"/"/g;
			
			# Eliminazione escape ritorno a capo
			$riga =~ s/\\n/\n/g;
			
			# Ripetizione comando INSERT per ogni record
			$riga =~ s/\),\(/\);\n$insertCmd\(/g;
		};
		$testo .= $riga;
	};
};

# Eliminazione virgole superflue dovute all'eliminazione di KEY
$testo =~ s/,\n\)/\n\)/gs;

$testo .= "COMMIT;\n";

open IMPORT, "| sqlite3 $sqliteDB > mysql2sqlite.log 2>&1";
print IMPORT $testo;
close (IMPORT);
close (SOURCE);

if (-z "mysql2sqlite.log") {
    print "\nDone successfully.\n\n";
} else {
    print "\nSome error are occurred. Please see log file.\n\n";
};
--------------------------8<-------------------------------------------

--
 _      Giuseppe Bordoni   .oOo.   http://www.geppozone.com       ( _)
(o)>  -------------------------------------------------------  \\\'',)
//\   Al mondo esistono 10 tipi di persone: quelle che           \/  \ ^
V_/_  conoscono la notazione binaria e quelle che la ignorano    _\'_/_)



Reply to: