loading values from txt to database

N

Nikos

I have this code to load the name of the games, descriptions and how
many times a game was downloaded.
problem is that if i change the description file, by for example adding
more games or editing existing ones this snippet will have to rerun form
games.pl thts is inside.

Can i do something that even if i cahnge the descriptions.txt file i
wont have to initialize all this evry tiem games.pl is loading and the
gamecounetr not to be lost?

Thank you and here si the snippet:

#===============================================================================

my @row;
$sth = $dbh->prepare( 'INSERT INTO games (gamename, gamedesc,
gamecounter) VALUES (?, ?, ?)' );

open (FILE, "<../data/games/descriptions.txt") or die $!;
while (<FILE>) {
chomp;

@row = split /\t/;
push @row, 0;

$sth->execute( @row );
}
close (FILE);

#===============================================================================
 
N

Nikos

Jim Gibson wrote:

{instructions]
The exact SQL (DBI) statements to accomplish either of the above is
left as an exercise.

Good luck.

Thanks, iam workign on it.
I likes the 2nd approach better.
 
M

Mark Clements

Nikos said:
I have this code to load the name of the games, descriptions and how
many times a game was downloaded.
problem is that if i change the description file, by for example adding
more games or editing existing ones this snippet will have to rerun form
games.pl thts is inside.

Can i do something that even if i cahnge the descriptions.txt file i
wont have to initialize all this evry tiem games.pl is loading and the
gamecounetr not to be lost?
Er - how about keeping the game descriptions directly in the database
and not bothering with descriptions.txt. You could mount it via ODBC and
enter the data using eg Access. This is *not* the place to ask how to do
this, but I guess you will ignore that I have said that.
Thank you and here si the snippet:

#===============================================================================



my @row;
$sth = $dbh->prepare( 'INSERT INTO games (gamename, gamedesc,
gamecounter) VALUES (?, ?, ?)' );

This is more of a MySQL question, but as per usual you have no interest
in posting questions to the correct newsgroup. As I've stated before
(you chose, yet again, not to listen), you don't need to bind all three
column values since one of them is invariant. I would tell you to read
the MySQL documentation for "replace", but you won't because you are too
rude to do any reading for yourself. Shame!
 
N

Nikos

This is what i manges to do to load the the already existing gamename
and descriptions from the .txt to the database and update the if the
game is already there or inserting a new database entry if new game is
added to descriptions.txt

But still it isnt working. I cant see why.


my @row;
my $select = $dbh->prepare( "SELECT * FROM games WHERE gamename=?" );
my $insert = $dbh->prepare( "INSERT INTO games (gamename, gamedesc,
gamecounter) VALUES (?, ?, ?)" );
my $update = $dbh->prepare( "UPDATE games SET gamedesc=? where
gamename=?" );

open (FILE, "<../data/games/descriptions.txt") or die $!;
while (<FILE>) {
chomp;

my ($gamename, $gamedesc) = split /\t/;
$select->execute( $gamename );

my $count;
while( my $row = $select->fetchrow_arrayref ) {
$count = $row->[0];
}

if( $count == 0 ) { #a new game
$insert->execute( $gamename, $gamedesc, 0 );
}
else {
$update->execute( $gamedesc, $gamename );
}
}
close (FILE);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top