Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use DBI;
- my $database = "banco_de_dados";
- my $hostname = "localhost";
- my $port = "3306";
- my $username = "usuario";
- my $password = "senha";
- my $dbh = DBI->connect("DBI:mysql:database=$database;host=$hostname;port=$port", $username, $password) or die "Error connecting to the database: $DBI::errstr";
- my $create_table_query = <<'SQL';
- CREATE TABLE dinheiro (
- id INT AUTO_INCREMENT PRIMARY KEY,
- tipo VARCHAR(50) NOT NULL,
- valor DECIMAL(10, 2) NOT NULL,
- quantidade INT NOT NULL
- );
- SQL
- $dbh->do($create_table_query) or die "Error creating table: $DBI::errstr";
- my $insert_query = <<'SQL';
- INSERT INTO dinheiro (tipo, valor, quantidade)
- VALUES
- ('Nota', 100.00, 10),
- ('Nota', 50.00, 20),
- ('Moeda', 1.00, 50),
- ('Moeda', 0.50, 100);
- SQL
- $dbh->do($insert_query) or die "Error inserting data: $DBI::errstr";
- my $select_query = "SELECT * FROM dinheiro";
- my $sth = $dbh->prepare($select_query);
- $sth->execute();
- while (my $row = $sth->fetchrow_hashref) {
- print "ID: $row->{id}\n";
- print "Tipo: $row->{tipo}\n";
- print "Valor: $row->{valor}\n";
- print "Quantidade: $row->{quantidade}\n";
- print "\n";
- }
- $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement