Advertisement
dev017

banco de dados. Com PERL.

Jul 29th, 2023 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.21 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use DBI;
  4.  
  5. my $database = "banco_de_dados";
  6. my $hostname = "localhost";
  7. my $port = "3306";
  8. my $username = "usuario";
  9. my $password = "senha";
  10.  
  11. my $dbh = DBI->connect("DBI:mysql:database=$database;host=$hostname;port=$port", $username, $password) or die "Error connecting to the database: $DBI::errstr";
  12.  
  13. my $create_table_query = <<'SQL';
  14. CREATE TABLE dinheiro (
  15.     id INT AUTO_INCREMENT PRIMARY KEY,
  16.     tipo VARCHAR(50) NOT NULL,
  17.     valor DECIMAL(10, 2) NOT NULL,
  18.     quantidade INT NOT NULL
  19. );
  20. SQL
  21.  
  22. $dbh->do($create_table_query) or die "Error creating table: $DBI::errstr";
  23.  
  24. my $insert_query = <<'SQL';
  25. INSERT INTO dinheiro (tipo, valor, quantidade)
  26. VALUES
  27.     ('Nota', 100.00, 10),
  28.     ('Nota', 50.00, 20),
  29.     ('Moeda', 1.00, 50),
  30.     ('Moeda', 0.50, 100);
  31. SQL
  32.  
  33. $dbh->do($insert_query) or die "Error inserting data: $DBI::errstr";
  34.  
  35. my $select_query = "SELECT * FROM dinheiro";
  36. my $sth = $dbh->prepare($select_query);
  37. $sth->execute();
  38.  
  39. while (my $row = $sth->fetchrow_hashref) {
  40.     print "ID: $row->{id}\n";
  41.     print "Tipo: $row->{tipo}\n";
  42.     print "Valor: $row->{valor}\n";
  43.     print "Quantidade: $row->{quantidade}\n";
  44.     print "\n";
  45. }
  46.  
  47. $dbh->disconnect();
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement