Sei sulla pagina 1di 10

11/26/2007

Programming and Databases

NET212

What kinds of features do you find in a


programming lang?

Programming Language features


Variables
Functions
If – then statements
While loops
For loops

1
11/26/2007

BASH Shell Scripting


Variables
myvar=“floppy”

If-then statements
if [ myvar = “floppy”
floppy ]
then
echo Paperweight
fi

Variables
How does a variable help us and what are they?

Variables
` They names that are associated with places in memory
` They are placeholders

Memory (RAM)
--------------
| “floppy”
floppy | < --- myvar (address 10)
| |
| |
|_______|
` Variables can be used to store data and perform
computations.
` If a computer counts to some number, then how can it do so w/o
storing a variable?

2
11/26/2007

If-then statements
` How can if-then statements be helpful and what are they?

If-then statements
` They allow a script (or program) to be flexible
` Respond to situations differently depending on the context

For instance, you want to make script that deletes internet


caches for every user except admin. How do that w/o an if-
statement?

if [ $USERNAME != “admin” ] #note that $USERNAME is env.


var.
then
echo “Deleting internet caches”

fi

Loops
` Loops will run a set of statements over and over again
until a condition is met

While loop
stoppoint=3
i=1
while [ $i –lt $stoppoint ]
do
echo i
i = $[$i + 1]
done

3
11/26/2007

While loop example


stoppoint=3
i=1
while [ $i –lt $stoppoint ]
do
echo i
i = $[$i + 1]
done

Results:

1
2

Perl
` Like shell scripts Perl is interpreted
` An interpreted language is not compiled into binary code
` It is parsed through an interpreter
` Perl is a general purpose programming language, so it can
do just about anything
` What is the interpreter for BASH shell scripts and the
commands you type in text mode?

Interpreted vs. compiled

Interpreted Language (Perl, BASH shell scripts)

Source code (text) Æ Interpreter Æ Execute Program

Compiled Language (c)

Source code (text) Æ Compiler Æ Binary file Æ Execute


Program

4
11/26/2007

Compilation example
` A simple c program:

#include <stdio.h>
main(){
printf( hello!\n );
printf(“hello!\n”);
return 0;
}
Compile it to produce a binary file Æ gcc –o helloprog
helloprog.c
gcc –o programname sourcecode

Perl variables
` Define a variable
#!/bin/perl
$myvar = “floppy”; # pound sign is for a comment
$num = 1; #notice the semicolons after each statement!

print “I have $num $floppy”; #print will output to the


console some text (it read variables too)

Run it:
./testperl.pl
I have 1 floppy

Arrays (list of values)


` An array is a list of values, such as 1 2 3 4 5 6
#!/bin/perl

@mynums = {1, 2, 3, 4, 5, 6}; # array example (with numbers)


@names = {“sally”,”tom”}; #another array (with strings)

print “$mynums[0] $mynums[1] $mynums[2] \n”;


#\n is a carriage return (stands for newline)
print “$names[1]”;

Output
123
tom

5
11/26/2007

Run a shell command from Perl


` You can run a command just like with BASH and store it
in a variable

$myresult = `wc –l /etc/passwd`; #notice the backquotes


`wc –l /etc/passwd
p > numusers.txt`;; #without storingg as
variable

print “$myresult”;

Perl if-statements
#!/bin/perl
$mynum = 4;
$mystr = “sally”;

if ( $mynum == 4){ #other operators are >, <, !=


print “it’s four !\n”;
}
If ( $mystr eq “sally”){ #strings have double quotes around them
print “it’s sally!\n”;
}
Output
It’s four!
It’s sally

Perl if-statements
Numerical conditional operators
==
!=
>
<
String conditional operators
eq
ne

6
11/26/2007

Perl loops
For loops
#!/bin/perl
for ( $i = 0; $i < 2 ; i++){
print “hey there!\n”;
}

@myarray = ( “sally”,”tom”);
foreach $person (@myarray){ #special for each loop; will do for each element in
myarray
print “I know $person! \n”; #$person is a way we can assign piece of array to use
}
Output
Hey there!
Hey there!
I know sally!
I know tom!

While loops
While loop
#!/bin/perl
$i = 3
while ( $i > 0 ){
print “$i”;
$i = $i + 1; # What’s wrong with this?
}
Output
3
4
5

mysql
` Mysql is a free database managements system
(DBMS)
` A DBMS allows you to create, modify, and read databases
` What are some other DBMSs?
` A database is basically table with columns and rows
` A column is an attribute of a row
` A row is a record in the table

7
11/26/2007

Example table
Student ID Paid ` Each record (row) has the
Tom 1 Y
following attributes
Sally 2 Y
` Student, ID, Paid
Frederick 3 N
` Each attribute has a data
type
` Student : TEXT
` ID : INT
` Paid : CHAR

Mysql data types


` There are a number of mysql datatypes
` INT Æ Integer: … -2, -1, 0, 1, 2,…
` CHAR Æ Character: a,b,c, …
` TEXT Æ A bunch of characters up to 65,535
` DATE Æ A date string: 2007-11-19

Creating databases and tables


You login into mysql and create a database first, before you
create any tables.

CREATE DATABASE net212;

USE net212;
212 Å Not
N use it!
i!

CREATE TABLE students (


student TEXT,
id INT,
paid CHAR);

8
11/26/2007

Putting new values into a table


` I want to insert the record: Penquin, 4, n

INSERT INTO students (student, id, paid)


VALUES (“Penquin”, 4, ‘n’);

Updating information
` I want to change the Penquin row, so he is now paid

UPDATE students SET paid = ‘y’


WHERE name = “Penquin”;

` I want to change everyone, so they are paid

UPDATE students SET paid = ‘y’;

Deleting records
` I want to delete the first record

DELETE FROM students


WHERE id = 1;

` I want to delete all records with records >= 1

DELETE FROM student


WHERE id >= 1;

9
11/26/2007

Destroying tables and databases


` Delete a table

DROP students;

` Delete a database

DROP net212;

Create a new user and granting access


` Mysql can be used for controlled access to databases and
assign encrypted passwords

Create the user tom with a password of cat

CREATE USER ‘tom’@’localhost’ IDENTIFIED BY ‘cat’;

Give this user access to the net212 database and all its tables

GRANT ALL PRIVILEGES ON net212.* TO ‘tom’@’localhost’


IDENTIFIED BY ‘cat’;

Reading records from a database


` I want to see all records in the students table

SELECT * FROM students;

` I want to see records of students that have not paid

SELECT * FROM student


WHERE paid = ‘n’;

10

Potrebbero piacerti anche