I need to INSERT 35,000+ records into a custom table created and used by a plugin.
I am currently doing the insert, by manually running an SQL query "from a file" using MySQLMan.
I would like to import the data 'during' the plugin install OR via a setup button 'after' installing the plugin.
I am open to pros/cons of each method.
I have already created the table, and extracted an .sql file to a tmp directory.
my $error;
$creator->cols ( {
ZipCode => { pos => 1, type => 'varchar', size => '10', not_null => 1, default => ''},
Latitude => { pos => 2, type => 'double', not_null => 0, default => '0'},
Longitude => { pos => 3, type => 'double', not_null => 0, default => '0' },
State => { pos => 4, type => 'char', size => '2', not_null => 0, default => '' }
});
# set the primary key, auto-increment and index
$creator->pk(['ZipCode','Latitude','Longitude']);
if (! $creator->create()) {
$GT::SQL::errcode ||= ''; #silence -w warning.
$GT::SQL::errcode eq 'TBLEXISTS' ? ($error = "Could not create table ZipCodes (table already exists)\n") :
($error = "Could not create table ZipCodes: $GT::SQL::error)");
}
$creator->load_table();
$file = $tar->get_file('ZipCodes.sql');
$file->name("$CFG->{admin_root_path}/tmp/ZipCodes.sql");
$file->write or return Plugins::ZipCodeSearch->error("Unable to extract file '$CFG->{admin_root_path}/tmp/ZipCodes.sql': $GT::Tar::error", 'WARN');
Currently the ZipCodes.sql file contains a crapload of insert statements....
INSERT INTO `glinks_ZipCodes` (ZipCode, Latitude, Longitude, State) VALUES ('00601', 18.180103, -66.749472, 'PR');
INSERT INTO `glinks_ZipCodes` (ZipCode, Latitude, Longitude, State) VALUES ('00602', 18.363285, -67.180247, 'PR');
...
Problems:
The INSERT INTO specifies the table with a prefix of glinks_
I need a more generic insert that will insert into ZipCodes table regardless of the prefix, because I have
a different install of Gossamer Links that uses a prefix of lsql_
So, I should just have a text file of comma separated values without multiple INSERT statements correct? i.e.
INSERT INTO glinks_ZipCodes VALUES('00601','18.180103','-66.749472','PR'),('00602','18.363285','-67.180247','PR') ...
Ok, well I just made a new file reflected in the above line by dumping, using multiple-row output option.
I think all I need to know is how to run the sql file from the plugin's install sub.
the table name is easily created using:
my $prefix_plus_table = $DB->prefix;
$prefix_plus_table .= 'Links';
So close, just not sure how to finish, and it's late.
Thanks for any help, comments or suggestions.
Chris
RGB World, Inc. - Software & Web Development.
rgbworld.com
I am currently doing the insert, by manually running an SQL query "from a file" using MySQLMan.
I would like to import the data 'during' the plugin install OR via a setup button 'after' installing the plugin.
I am open to pros/cons of each method.
I have already created the table, and extracted an .sql file to a tmp directory.
Code:
my $creator = $DB->creator ('ZipCodes'); my $error;
$creator->cols ( {
ZipCode => { pos => 1, type => 'varchar', size => '10', not_null => 1, default => ''},
Latitude => { pos => 2, type => 'double', not_null => 0, default => '0'},
Longitude => { pos => 3, type => 'double', not_null => 0, default => '0' },
State => { pos => 4, type => 'char', size => '2', not_null => 0, default => '' }
});
# set the primary key, auto-increment and index
$creator->pk(['ZipCode','Latitude','Longitude']);
if (! $creator->create()) {
$GT::SQL::errcode ||= ''; #silence -w warning.
$GT::SQL::errcode eq 'TBLEXISTS' ? ($error = "Could not create table ZipCodes (table already exists)\n") :
($error = "Could not create table ZipCodes: $GT::SQL::error)");
}
$creator->load_table();
$file = $tar->get_file('ZipCodes.sql');
$file->name("$CFG->{admin_root_path}/tmp/ZipCodes.sql");
$file->write or return Plugins::ZipCodeSearch->error("Unable to extract file '$CFG->{admin_root_path}/tmp/ZipCodes.sql': $GT::Tar::error", 'WARN');
Currently the ZipCodes.sql file contains a crapload of insert statements....
INSERT INTO `glinks_ZipCodes` (ZipCode, Latitude, Longitude, State) VALUES ('00601', 18.180103, -66.749472, 'PR');
INSERT INTO `glinks_ZipCodes` (ZipCode, Latitude, Longitude, State) VALUES ('00602', 18.363285, -67.180247, 'PR');
...
Problems:
The INSERT INTO specifies the table with a prefix of glinks_
I need a more generic insert that will insert into ZipCodes table regardless of the prefix, because I have
a different install of Gossamer Links that uses a prefix of lsql_
So, I should just have a text file of comma separated values without multiple INSERT statements correct? i.e.
INSERT INTO glinks_ZipCodes VALUES('00601','18.180103','-66.749472','PR'),('00602','18.363285','-67.180247','PR') ...
Ok, well I just made a new file reflected in the above line by dumping, using multiple-row output option.
I think all I need to know is how to run the sql file from the plugin's install sub.
the table name is easily created using:
my $prefix_plus_table = $DB->prefix;
$prefix_plus_table .= 'Links';
So close, just not sure how to finish, and it's late.
Thanks for any help, comments or suggestions.
Chris
RGB World, Inc. - Software & Web Development.
rgbworld.com