1 / 14

PostgreSQL

PostgreSQL. עבודה עם שרת PostgreSQL : PSQL – מפענח שורת פקודה LIBPQ – ספריית C WebDB – ממשק מבוסס Web (לסטודנטים של הקורס) חשבונות: מבוססים על חשבונות CSL (חשבונות LINUX בחוות מחשבים בפקולטה) נפתחים אוטומטית למי שנרשם ברישום מוקדם (לפני תחילת הסמסטר). PSQL – מפענח שורת פקודה.

cala
Télécharger la présentation

PostgreSQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PostgreSQL עבודה עם שרת PostgreSQL: PSQL – מפענח שורת פקודה LIBPQ – ספריית C WebDB – ממשק מבוסס Web (לסטודנטים של הקורס) חשבונות: מבוססים על חשבונות CSL (חשבונות LINUX בחוות מחשבים בפקולטה) נפתחים אוטומטית למי שנרשם ברישום מוקדם (לפני תחילת הסמסטר) DBMS - 236363, PostgreSQL

  2. PSQL – מפענח שורת פקודה מחשבים עליהם מותקן ה-client של PostgreSQL: יש מספר מחשבים עם ה-client בגרסאות שונות אבל רק על csl1 ו-csl מותקנת גרסה מתאימה לקורס הזה. התחברות: /usr/local/pgsql/bin/psql -h pgsql.cs.technion.ac.il alex alex Password: xxxxxxxx alex => יציאה: alex => \q DBMS - 236363, PostgreSQL

  3. PSQL – פקודות פנימיות פקודות פנימיות של המפענח: alex=> \? – קבלת רשימה של הפקודות alex=> \h select – קבלת עזרה על הפקודה alex=> \q – יציאה מהמפענח alex=> \d – קבלת רשימת טבלאות alex=> \d person – קבלת מידע על הטבלה DBMS - 236363, PostgreSQL

  4. Year COUNT(Book_Id) 1988 1 alex=> (3 rows) 2001 1 1985 1 PSQL – פקודות SQL הרצת פקודות SQL: alex=> SELECT Year, COUNT(Book_Id) alex=> FROM Books alex=> GROUP BY Year alex=> HAVING AVG(Pages) > 400; DBMS - 236363, PostgreSQL

  5. LIBPQ – ספריית פונקציות C שימוש בפונקציות של LIBPQ בתוכנית C: #include <libpq-fe.h> הידור וקישור התוכנית: gcc -I/usr/local/pgsql/include -L/usr/local/pgsql/lib –lpq –o try try.c DBMS - 236363, PostgreSQL

  6. שלד של תוכנית C המשתמשת ב-LIBPQ PGconn *conn;בתחילת התוכנית: int main(void) { /* Make a connection to the DB. If parameters omitted, default values are used */ conn = PQconnectdb(“host=pgsql.cs.technion.ac.il “ “dbname=alex user=alex password=alex1”); /* check to see that the backend connection was successfully made */ if (!conn || PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to server failed: %s\n", PQerrorMessage(conn)); PQfinish(conn); return 1; } … DBMS - 236363, PostgreSQL

  7. LIBPQ: שלד של תוכנית C – המשך בסיום התוכנית: … /* Close the connection to the database and cleanup */ PQfinish(conn); return 0; } DBMS - 236363, PostgreSQL

  8. LIBPQ: ביצוע שאילתות/פקודות SQL ביצוע שאילתות/פקודות SQL: PQexec() PGresult *res; … res = PQexec(conn, “SELECT BOOK_NAME, ORDER_DATE “ “FROM ORDERED WHERE CUST_ID=123456”); if(!res || PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "Error executing query: %s\n", PQresultErrorMessage(res)); PQclear(res); return; } … DBMS - 236363, PostgreSQL

  9. LIBPQ: PQexec() – המשך שימוש במחרוזת עזר: char cmd[200]; int cust_id; /* … */ cust_id = 20; /* could be result of some input or computation */ sprintf(cmd, “SELECT BOOK_NAME, ORDER_DATE “ “FROM ORDERED WHERE CUST_ID=%d”, cust_id); res = PQexec(conn, cmd); /* … */ שימו לב! יש לוודא שב-cmd יש מספיק מקום בשביל להכיל את הפקודות! DBMS - 236363, PostgreSQL

  10. LIBPQ: בדיקת תוצאה של שאילתה ערך מוחזר מפונקציה PQresultStatus(): עבור שאילתה ("SELECT ..."): PGRES_TUPLES_OK – השאילתה הצליחה אחרת – השאילתה נכשלה עבור פקודות אחרות (DML/DDL): PGRES_COMMAND_OK – הפקודה הצליחה אחרת – הפקודה נכשלה שימו לב: יצירת מבט היא פקודת DDL, ולא שאילתה! DBMS - 236363, PostgreSQL

  11. LIBPQ: שליפת רשומות של תוצאה במידה והשאילתה הצליחה, נוכל לשלוף מידע מהתוצאה: int PQntuples(res) : מס' הרשומות (שורות) בתוצאה (יכול להיות 0) int PQnfields(res): מספר השדות (עמודות) בתוצאה char *PQfname(res, field_index): שם השדה לפי מספר (החל מ-0) int PQfnumber(res, field_name): מספר השדה לפי שם char *PQgetvalue(res, tup_num, field_num): תוכן השדה המתאים בתוצאה int PQgetisnull(res, tup_num, field_num): NULL מחזיר 1 אם השדה הוא void PQclear(res): משחרר את המשאבים שהוקצו לתוצאה אם PQexec() לא הצליחה, נוכל לקבל תאור מילולי של השגיאה: char *PQresultErrorMessage(res) DBMS - 236363, PostgreSQL

  12. LIBPQ: דוגמא דוגמא: בכל שנה יש להציג את מספר הספרים שיצאו לאור באותה שנה (ללא שימוש ב-GROUP BY) int books_by_year(PGconn *conn){   PGresult *res_year, *res_num; int i, year; char query[120]; res_year = PQExec(conn, “SELECT DISTINCT YEAR FROM BOOK”); if(!res_year || PQresultStatus(res_year) != PGRES_TUPLES_OK){ fprintf(stderr, "Error executing query: %s\n", PQresultErrorMessage(res_year)); PQclear(res_year); return -1; }  DBMS - 236363, PostgreSQL

  13. LIBPQ: דוגמא – המשך for(i=0;i<PQntuples(res_year);i++){ year = atoi(PQgetvalue(res_year,i,0)); sprintf(query, “SELECT COUNT(DISTINCT BOOK_NAME) “ “FROM BOOKS WHERE YEAR = %d”, year); res_num = PQexec(conn,query); if(!res_num || PQresultStatus(res_num) != PGRES_TUPLES_OK){ fprintf(stderr, "Error executing query: %s\n", PQresultErrorMessage(res_num)); PQclear(res_num); PQclear(res_year); return -1; } printf(“YEAR=%d Num.Books=%s\n”, year, PQgetvalue(res_num,0,0)); PQclear(res_num); } PQclear(res_year); return 0; } DBMS - 236363, PostgreSQL

  14. WebDB:http://pgsql.cs.technion.ac.il:8888/webdb/ DBMS - 236363, PostgreSQL

More Related