mysql fronend

Giorgos Keramidas keramida at ceid.upatras.gr
Sun Sep 12 20:49:58 EEST 2004


On 2004-09-12 17:45, comzeradd <mail at comzeradd.net> wrote:
> sorry gia prin! tora to stelno apo sylpheed:-)

Ευχαριστώ :)

> epanalamvano:
> ftiaxno ena site xrisimopoiontas mysql. mipos kserei kapoios kanena
> kalo frontend gia na peraso ta dedomena pio grigora-eukola?

Δεν ξέρω αν θες ντε και καλά να χρησιμοποιήσεις κάποιο "front-end" αλλά
ένας καλός τρόπος είναι να μη χρησιμοποιήσεις κάποιο GUI, web-based ή άλλου
είδους front-end.

Η mysql περιέχει το εργαλείο `mysql' που μπορεί να διαβάσει μια σειρά από
εντολές (τόσο SQL όσο και δικές του) από ένα αρχείο και να "τρέξει" αυτές
τις εντολές.  Αν έχεις π.χ. μια σειρά από ονόματα και email που θες να
προσθέσεις σε ένα πίνακα, μπορείς να γράψεις ένα μικρό πρόγραμμα που θα
δημιουργήσει τα απαραίτητα INSERT queries.  Έστω ότι έχεις ένα αρχείο όπως
το παρακάτω:

        % cat email.txt
        Giorgos Keramidas <keramida at freebsd.org>
        Giorgos Keramidas <keramida at ceid.upatras.gr>
        Giorgos Keramidas <keramida at linux.gr>
        Giorgos Keramidas <keramida at hellug.gr>
        Giorgos Keramidas <freebsd at freemail.gr>

και θέλεις να εισάγεις τα δεδομένα σε ένα πίνακα που λέγεται "email" κι
έχει δυο πεδία (NAME, EMAIL).  Με ένα μικρό Perl script μπορείς να
δημιουργήσεις 'on the fly' τα INSERT queries που πρέπει να γίνουν:

% cat -n gen-email.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5  my ($lc, $line,                     # line count and text of current line
     6      @row, $item,                    # temp vars
     7      $name, $email                   # user information to INSERT
     8  );
     9
    10  $lc = 0;
    11  while (defined($line = <STDIN>)) {
    12      chomp $line;
    13      $lc++;
    14      @row = split /\s+/, $line;
    15      if ($#row == 0) {
    16          $item = $row[0];
    17          if ($item =~ m/^.*@.*$/) {
    18              $item =~ s/^<//;
    19              $item =~ s/>$//;
    20              $email = $item;
    21              $name = "";
    22          }
    23      } else {
    24          $item = $row[$#row];
    25          die "no email found in line $lc"
    26              unless ($item =~ m/^.*@.*$/);
    27          $item =~ s/^<//;
    28          $item =~ s/>$//;
    29          $email = $item;
    30          $#row--;                # strip off email from @row
    31          $name = join(' ', @row);
    32      }
    33      print "INSERT INTO email (name,email) VALUES('$name','$email')\n";
    34  }

Τρέχοντας το παραπάνω script με είσοδο από το αρχείο email.txt θα δεις:

        % perl -w gen-email.pl < email.txt
        INSERT INTO email (name,email) VALUES('Giorgos Keramidas','keramida at freebsd.org')
        INSERT INTO email (name,email) VALUES('Giorgos Keramidas','keramida at ceid.upatras.gr')
        INSERT INTO email (name,email) VALUES('Giorgos Keramidas','keramida at linux.gr')
        INSERT INTO email (name,email) VALUES('Giorgos Keramidas','keramida at hellug.gr')
        INSERT INTO email (name,email) VALUES('Giorgos Keramidas','freebsd at freemail.gr')

κάτι που μπορείς άνετα ύστερα να το περάσεις με pipe στο mysql(1) utility:

	% perl -w gen-email.pl < email.txt | mysql -u USER emaildb

- Γιώργος




More information about the Linux-greek-users mailing list