Skip to content

Instantly share code, notes, and snippets.

@roblabla
Created January 11, 2014 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblabla/8376826 to your computer and use it in GitHub Desktop.
Save roblabla/8376826 to your computer and use it in GitHub Desktop.
Import into mysql the contents of authme flatfile
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
##############################
# EDIT THESE CONFIG SETTINGS #
##############################
my $host = "localhost";
my $database = "authme";
my $username = "authme";
my $password = "password";
my $auth_file = "C:\\Users\\Konrad\\Desktop\\auth.db";
###############################
# DO NOT EDIT BELOW THIS LINE #
###############################
open FILE, "$auth_file" or die $!;
my $dbh = DBI->connect("DBI:mysql:$database;host=$host", "$username", "$password") or die "Could not connect to database: $DBI::errstr";
$dbh->do('CREATE TABLE `authme` (
`id` INTEGER AUTO_INCREMENT,
`username` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`ip` VARCHAR(40) NOT NULL,
`lastlogin` BIGINT,
CONSTRAINT `table_const_prim` PRIMARY KEY (`id`));');
my $st = 'INSERT INTO `authme` (`username`, `password`, `ip`, `lastlogin`) VALUES ';
my $i = 0;
while(<FILE>) {
if($i == 1000) {
$i = 0;
$dbh->do($st);
$st = 'INSERT INTO `authme` (`username`, `password`, `ip`, `lastlogin`) VALUES ';
}
my @auth = split(':');
if($i != 0) {
$st .= ", ";
}
$st .= "(\"$auth[0]\", \"$auth[1]\", ";
$st .= "\"" . ($auth[2] || '198.18.0.1') . "\", ";
$st .= ($auth[3] || '0') . ")";
$i++;
}
if($i > 0) {
$dbh->do($st);
}
$dbh->disconnect();
close FILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment