Ultimater
10-02-2005, 10:06 PM
I've been using plain files for databases for quite a while and my database looks something like this when viewed in Excel:
+------+----------+--------+--------+
|MEM ID|BUS NAME |BUS INFO|CONTACT |
+------+----------+--------+--------+
|000007|aplustv |repairs |Charles |
|000034|zenith |company |Jack |
|000002|mitsubishi|company |Bill |
+------+----------+--------+--------+
sub createDataBase{
open(CSVFILE, "> ".$da_csv) or die "fatal error creating file: $da_csv";print CSVFILE "MEM ID,BUS NAME,BUS INFO,CONTACT";close(CSVFILE);chmod 0700, $da_csv;
}
sub getPost{
unless(-e $da_csv){&createDataBase;}
unless(-e $da_csv){
print "<span style=\'color:red;\'>The file: ".$da_csv." Could not be created!<\/span>";
;exit}
open(CSVFILE, $da_csv) or die "fatal error opening file: $da_csv";
my @incoming=<CSVFILE>;
close(CSVFILE);
grep(chomp $_, @incoming);
our $getRecord = $_[0];
foreach my $Line (@incoming){
our ($userid1,$busname1,$businfo1,$contactperson1,)= split(/\,/, $Line);
if($userid1 eq $getRecord){
our @tmp=($userid1,$busname1,$businfo1,$contactperson1);
}#if
}#foreach
for(my $i=0; $i<=$#tmp;$i++){
$tmp[$i]=unescape($tmp[$i]);
}
return @tmp;
}#sub
sub databaseWrite{
local ($da_csv,$userid,$busname,$businfo,$contactperson)=@_;
unless(-e $da_csv){open(CSVFILE, "> ".$da_csv) or die "fatal error creating file: Events.csv";print CSVFILE "MEM ID,BUS NAME,BUS INFO,CONTACT PERSON";close(CSVFILE);chmod 0700, $da_csv;}
open(CSVFILE, ">> ".$da_csv) or die "fatal error appending to file: $da_csv";
print CSVFILE "\r\n".$userid.",".escape($busname).",".escape($businfo).",".escape($contactperson);
close(CSVFILE);
}
Any ideas how to convert this to a MySQL database? I've never used MySQL before, All I know is I'm running on a windows server and that it supports MySQL. Is it as easy as creating a SQL file or something? Even a simple example would help that uses a single field. Thanks in advance.
p.s. I know that there is a Text::CSV (http://cpan.uwinnipeg.ca/htdocs/Text-CSV/Text/CSV.html) module and I could have used it in my above code but I really need a database that doesn't slow down with each write -- so my old CSV file approch is no longer an alternitive.
+------+----------+--------+--------+
|MEM ID|BUS NAME |BUS INFO|CONTACT |
+------+----------+--------+--------+
|000007|aplustv |repairs |Charles |
|000034|zenith |company |Jack |
|000002|mitsubishi|company |Bill |
+------+----------+--------+--------+
sub createDataBase{
open(CSVFILE, "> ".$da_csv) or die "fatal error creating file: $da_csv";print CSVFILE "MEM ID,BUS NAME,BUS INFO,CONTACT";close(CSVFILE);chmod 0700, $da_csv;
}
sub getPost{
unless(-e $da_csv){&createDataBase;}
unless(-e $da_csv){
print "<span style=\'color:red;\'>The file: ".$da_csv." Could not be created!<\/span>";
;exit}
open(CSVFILE, $da_csv) or die "fatal error opening file: $da_csv";
my @incoming=<CSVFILE>;
close(CSVFILE);
grep(chomp $_, @incoming);
our $getRecord = $_[0];
foreach my $Line (@incoming){
our ($userid1,$busname1,$businfo1,$contactperson1,)= split(/\,/, $Line);
if($userid1 eq $getRecord){
our @tmp=($userid1,$busname1,$businfo1,$contactperson1);
}#if
}#foreach
for(my $i=0; $i<=$#tmp;$i++){
$tmp[$i]=unescape($tmp[$i]);
}
return @tmp;
}#sub
sub databaseWrite{
local ($da_csv,$userid,$busname,$businfo,$contactperson)=@_;
unless(-e $da_csv){open(CSVFILE, "> ".$da_csv) or die "fatal error creating file: Events.csv";print CSVFILE "MEM ID,BUS NAME,BUS INFO,CONTACT PERSON";close(CSVFILE);chmod 0700, $da_csv;}
open(CSVFILE, ">> ".$da_csv) or die "fatal error appending to file: $da_csv";
print CSVFILE "\r\n".$userid.",".escape($busname).",".escape($businfo).",".escape($contactperson);
close(CSVFILE);
}
Any ideas how to convert this to a MySQL database? I've never used MySQL before, All I know is I'm running on a windows server and that it supports MySQL. Is it as easy as creating a SQL file or something? Even a simple example would help that uses a single field. Thanks in advance.
p.s. I know that there is a Text::CSV (http://cpan.uwinnipeg.ca/htdocs/Text-CSV/Text/CSV.html) module and I could have used it in my above code but I really need a database that doesn't slow down with each write -- so my old CSV file approch is no longer an alternitive.