Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslating Perl to Python
    primarykey
    data
    text
    <p>I found this Perl script while <a href="https://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql/25860">migrating my SQLite database to mysql</a></p> <p>I was wondering (since I don't know Perl) how could one rewrite this in Python?</p> <p>Bonus points for the shortest (code) answer :)</p> <p><strong>edit</strong>: sorry I meant shortest code, not strictly shortest answer</p> <pre><code>#! /usr/bin/perl while ($line = &lt;&gt;){ if (($line !~ /BEGIN TRANSACTION/) &amp;&amp; ($line !~ /COMMIT/) &amp;&amp; ($line !~ /sqlite_sequence/) &amp;&amp; ($line !~ /CREATE UNIQUE INDEX/)){ if ($line =~ /CREATE TABLE \"([a-z_]*)\"(.*)/){ $name = $1; $sub = $2; $sub =~ s/\"//g; #" $line = "DROP TABLE IF EXISTS $name;\nCREATE TABLE IF NOT EXISTS $name$sub\n"; } elsif ($line =~ /INSERT INTO \"([a-z_]*)\"(.*)/){ $line = "INSERT INTO $1$2\n"; $line =~ s/\"/\\\"/g; #" $line =~ s/\"/\'/g; #" }else{ $line =~ s/\'\'/\\\'/g; #' } $line =~ s/([^\\'])\'t\'(.)/$1THIS_IS_TRUE$2/g; #' $line =~ s/THIS_IS_TRUE/1/g; $line =~ s/([^\\'])\'f\'(.)/$1THIS_IS_FALSE$2/g; #' $line =~ s/THIS_IS_FALSE/0/g; $line =~ s/AUTOINCREMENT/AUTO_INCREMENT/g; print $line; } } </code></pre> <p>Some additional code was necessary to successfully migrate the sqlite database (handles one line Create table statements, foreign keys, fixes a bug in the original program that converted empty fields <code>''</code> to <code>\'</code>. </p> <p>I <a href="https://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-sqlite3-to-mysql/1067365#1067365">posted the code on the migrating my SQLite database to mysql Question</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload