Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Jack H.Integration and automation of manufacturing systems.2001.pdf
Скачиваний:
86
Добавлен:
23.08.2013
Размер:
3.84 Mб
Скачать

page 114

Select order_number, name, description, location FROM Orders O, Customers C, Parts P WHERE O.customer_id = C.customer_id, O.part_id = P.part_id

Ordersorder_numbername

descriptionlocation

00103

I.M. Reech and Co.valvebin 2-3

00103

I.M. Reech and Co.cylinderbin 5-4

00103

I.M. Reech and Co.hosebin 8-2

00134

Widgets Inc.hose

bin 8-2

Figure X.6 - A More Advanced Query

The SQL queries are easily used when interacting with a command interface. Although, it is

more common for these commands to be used from within computer programs that call the data-

base to make automatic queries.

5.2 DATABASE ISSUES

Databases handle problems of,

-data locking (only allow one user to modify at once)

-data sharing (other users can view)

-searching

-etc

Database design

-first order normal, etc.

-flexibility

-verification

5.3 LABORATORY - SQL FOR DATABASE INTEGRATION

page 115

Purpose:

To learn the basic command language, SQL, that is used to interact with relational databases.

Overview:

Databases store information in tables. The users can use or manipulate this data to suit other purposes. The fundamental language for interacting with the databases is SQL. This lab will offer a simple introduction, and then go on to interface with the database using C programs in the following lab.

Pre-Lab:

Read the SQL and database material.

In-Lab:

First Time Installation:

1.Check to see if the database is installed. One way to do this is to look for the database server using ’which postmaster’. If it is not installed it can be installed from the Redhat distribution CD, or by downloading it from www.postgresql.org.

2.Log in as root with ’su - root’ and edit the file ’/var/lib/pgsql/.bashrc’ to include the following lines. (Note: the .bashrc file may not exist, but it will be created by the editor.)

PGLIB=/usr/lib/pgsql

PGDATA=/usr/lib/pgsql/data export PGLIB PGDATA

3.Change the ownership of the postgres directory to the user postgres with the command ’chown postgres /usr/lib/pgsql’.

4.Log in as the user ’postgres’ - An account called ’postgres’ is normally defined in most modern Linux distributions, but the account password is disabled. To log in the first time you must be logged in as root, then log in as postgres with ’su - postgres’. At this point you can change the password so you can log in directly as postgres in the future with ’passwd postgres’. Verify that you are logged in as postgres before continuing with ’whoami’.

5.Set up the databases with the command ’initdb’. This will set up all of the needed files and directories.

6.At this point the database should be ready for use, but the database server is not running. It can be started with ’postmaster &’.

7.Use ‘createuser <YOUR_LOGIN_NAME_GOES_HERE>’ to add yourself as a valid database user. Answer ‘y’es when asked if you are allowed to create databases. And answer ‘y’es when asked if you can create new users. These choices will allow you full control over the database. Note: ‘destroyuser <YOUR_NAME>’ can be used if you need to remove a user.

8.Log out from the postgres account with ’exit’.

Before Use:

1. Start the database when logged in as root with ’postmaster &’. This should start the database server. You can check to see if it is accepting connections with the

page 116

command ’netstat -a | grep postmaster’. If the postmaster program is listed the database is ready for use.

2. The database can also be set up to run automatically each time the computer is rebooted by adding it to the ’system V’ initialization list. But, this step is not necessary unless setting up a permanent database.

Creating and Using a New Database

1.Create a new database with ‘createdb test’.

2.Connect to the database with ‘psql test’. This is a simple program that allows interaction with the database using typed commands. Type ‘\h’ to see a list of commands.

3.Create a new database table using the SQL command below. This program requires that you end each line with a ‘\g’.

CREATE TABLE grades (name CHAR(10), grade CHAR(3), year INT)\g 6. Display the table (it is empty) with the command below. Note: upper and lower

case values are used to make the SQL commands stand out. SELECT * FROM grade

7.Add data with the commands below. After adding each datapoint, print out the table values. (Note: using the up and down cursor keys will allow you to recall previously entered commands.)

INSERT INTO grades (name, grade, year) VALUES (‘egr 101’, ‘D’, 1997) INSERT INTO grades (name, year, grade) VALUES (‘egr 101’, 1998,

‘B+’)

INSERT INTO grades (name, grade, year) VALUES (‘egr 103’, ‘A’, 1999) INSERT INTO grades (name, grade, year) VALUES (‘egr 209’, ‘B+’,

1999)

INSERT INTO grades (name, year) VALUES (‘egr 226’, 1999) INSERT INTO grades (year) VALUES (2000)

8.Follow the tutorials in the ‘/usr/share/doc/postgres*’ directory.

9.Develop a database (of your own design) that will keep customer information, and inventory levels.

Submit (individually):

1. A completed customer information database.

5.4 LABORATORY - USING C FOR DATABASE CALLS

Purpose:

To access a database using a simple C program.

Overview:

The program listing in Figure X.8 can be used to access the Postgres database. It uses a database access library called ’libpq’. This library of functions allows SQL database queries. The results of the query can then be easily retrieved.

page 117

In this example the program begins with an attempt to connect to the database using the function ’PQconnectdb’, and the status of the connection is then checked using ’PQstatus’. An SQL query is passed to the database using the ’PQexec’ command, and the results are returned into the ’PGresult’ structured called ’res’ in this example. The results can be checked using ’PQresultStatus’, and retrieved using the ’PQgetvalue’ function. The ’PQntuples’ function returns the number of matching results.

After each query the results structure should be released using ’PQclear’, and when all database access is complete the connection to the database should be terminated with ’PQfinish’.

#include <stdio.h> #include <stdlib.h>

#include <pgsql/libpq-fe.h>

int main(){

 

char

grade[3];

char

query_string[256];

PGconn

*conn;

PGresult

*res;

int

i;

conn = PQconnectdb("dbname=test"); if(PQstatus(conn) != CONNECTION_BAD){

printf("Enter a grade: "); scanf("%2s", grade); sprintf(query_string,

"SELECT name FROM grades WHERE grade = ’%s’", grade); res = PQexec(conn, query_string); if(PQresultStatus(res) == PGRES_TUPLES_OK){

int i;

for(i = 0; i < PQntuples(res); i++) printf("name = %s \n", PQgetvalue(res, i, 0));

} else {

printf("The query failed \n");

}

PQclear(res);

PQfinish(conn);

} else {

printf("Could not open the database \n");

}

return 0;

}

Figure X.8 - C Program for Database Access (dbtest.c)

all: dbtest CC = gcc

CFLAGS = -Wall LIBS = -lpq dbtest: dbtest.c

$(CC) $(CFLAGS) dbtest.c -o dbtest $(LIBS)

Figure X.9 - Makefile for Database Program

page 118

Pre-Lab:

1. Examine the database program in the Overview section.

In-Lab:

1.Enter the program and makefile given in the Overview section. Use ’make’ to compile the program, and run it to verify that it does access the database.

2.Write a program that allows jobs to be entered into the customer information database created in the previous laboratory.

Submit (individually):

1. The C program to access the customer information database.