Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 86 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
86
Dung lượng
1,78 MB
Nội dung
778 CHAPTER 37 ■ IMPORTING AND EXPORTING DATA Copying Data to and from a Table Copying data from a table to a text file or standard output is accomplished using the COPY tablename TO {filename | STDOUT} variant of the COPY command. The complete syntax follows: COPY tablename [(column [, ])] TO {'filename' | STDOUT} [[WITH] [BINARY] [OIDS] [DELIMITER [AS] 'delimiter'] [NULL [AS] 'null string'] [CSV [HEADER] [QUOTE [AS] 'quote'] [ESCAPE [AS] 'escape'] [FORCE NOT NULL column [, ]] Copying data residing in a text file to a table or standard output is accomplished using the same syntax as that for copying from a table, except for three slight variations of the syntax. These changes are bolded in the syntax that follows: COPY tablename [(column [, ])] FROM {'filename' | STDIN} [[WITH] [BINARY] [OIDS] [DELIMITER [AS] 'delimiter'] [NULL [AS] 'null string'] [CSV [HEADER] [QUOTE [AS] 'quote'] [ESCAPE [AS] 'escape'] [FORCE QUOTE column [, ]] As you can see, COPY has quite a bit to offer. Perhaps the best way to understand its many capabilities is through several examples. Copying Data from a Table To begin, let’s dump data from a table containing employee information to standard output: psql>COPY employee TO STDOUT; This returns the following: 1 JG100011 Jason Gilmore jason@example.com 2 RT435234 Robert Treat rob@example.com 3 GS998909 Greg Sabino Mullane greg@example.com 4 MW777983 Matt Wade matt@example.com Gilmore_5475C37.fm Page 778 Tuesday, February 7, 2006 2:31 PM CHAPTER 37 ■ IMPORTING AND EXPORTING DATA 779 To redirect this output to a file, simply specify a filename, like so: psql>COPY employee TO '/home/jason/sqldata/employee.sql'; Keep in mind that the PostgreSQL daemon user will require the necessary privileges for writing to the specified directory. Also, an absolute path is required, because COPY will not accept relative pathnames. ■Note On Windows, forward slashes should be used to specify the absolute path. So, for example, to COPY data to PostgreSQL’s data directory, the path might look like c:/pgsql/data/employee.sql. Copying Data from a Text File Copying data from a text file to a table is as simple as copying data to it. Let’s begin by importing the employee data dumped to employee.sql in the earlier example into an identical but newly named and empty employee table: psql>COPY employeenew FROM '/home/jason/sqldata/employee.sql'; Now, SELECT the data from employeenew and you’ll see the following output: employeeid | employeecode | name | email + + + 1 | JG100011 | Jason Gilmore | jason@example.com 2 | RT435234 | Robert Treat | rob@example.com 3 | GS998909 | Greg Sabino Mullane | greg@example.com 4 | MW777983 | Matt Wade | matt@example.com (4 rows) Note that an absolute path must be used to refer to the file. Additionally, the PostgreSQL daemon user must be capable of reading the target file. Finally, keep in mind that COPY will not attempt to perform any processing on the file to determine whether the data in each field can legally be placed in a particular table column; rather, it will simply incrementally match each field in the text file to the table column of the same offset. COPY FROM presumes each field is delimited by a predefined character string, which is by default a tab (\t) for text files, and a comma for CSV files (see the later section “Working with CSV Files”). Furthermore, each row is presumed to be delimited by a similar predefined string, which is by default newline (\n). These predefined characters can be changed if necessary; see the later section “Changing the Default Delimiter” for more information. Binary This clause tells PostgreSQL to copy data using a custom format, resulting in a slight increase in performance. However, executing COPY FROM BINARY can only be used when the data was previously written using COPY TO BINARY. Furthermore, this has nothing to do with storing data such as Word documents or images. It’s merely a slightly more efficient way to copy large files. Gilmore_5475C37.fm Page 779 Tuesday, February 7, 2006 2:31 PM 780 CHAPTER 37 ■ IMPORTING AND EXPORTING DATA Exporting the Table OIDs If the target table was created with OIDs (object identifiers), you can specify that they are output along with the rest of the table data by using the OIDS clause. For example: psql>COPY employee TO STDOUT OIDS; This produces: 24627 1 GM100011 Jason Gilmore jason@example.com 24628 2 RT435234 Robert Treat rob@example.com 24629 3 GS998909 Greg Sabino Mullane greg@example.com 24630 4 MW777983 Matt Wade matt@example.com Changing the Default Delimiter Note the apparent white space found in between each column in the previous example’s output. This is actually a tab (\t), which is the default delimiter. By using the DELIMITER clause, you can change the default to, for instance, a vertical pipe (|): psql>COPY employee TO STDOUT DELIMITER '|'; This returns the preceding output (minus the OIDs) in the following format: 1|GM100011|Jason Gilmore|jason@example.com 2|RT435234|Robert Treat|rob@example.com 3|GS998909|Greg Sabino Mullane|greg@example.com 4|MW777983|Matt Wade|matt@example.com Likewise, if a text file you’d like to import does not use tab characters to delimit fields, specify it similarly to the previous command: psql>COPY employeenew FROM '/home/jason/sqldata/employee.sql' DELIMITER |; Copying Only Specific Columns If you wanted to copy just the employees’ names and e-mail addresses to standard output, specify the column names like so: psql>COPY employee (name,email) TO STDOUT; This produces the following: Jason Gilmore jason@example.com Robert Treat rob@example.com Greg Sabino Mullane greg@example.com Matt Wade matt@example.com Gilmore_5475C37.fm Page 780 Tuesday, February 7, 2006 2:31 PM CHAPTER 37 ■ IMPORTING AND EXPORTING DATA 781 Likewise, if a text file contains only some of the fields to be inserted in a table, you can specify them as was done previously. Keep in mind, however, that the other table columns need to either be nullable or possess default values. Dealing with Null Values While e-mail is a crucial communications tool for individuals working in an office environment, suppose some of the employees work solely in the warehouse, negating the need for e-mail. Therefore, some of the e-mail values in the employee table might be null. When exporting data using COPY, the default for null values is \N, and when using CSV mode (discussed later in this section), it’s an empty string. However, what if you want to declare a custom string for such instances, no email for example? You should use the NULL clause, like so: psql>COPY employee TO STDOUT NULL 'no email'; This produces output similar to this (presuming some of the employee e-mail addresses have been set to null): Jason Gilmore no email Robert Treat rob@example.com Greg Sabino Mullane greg@example.com Matt Wade no email Similarly, if you are importing data from a text file and a NULL value is specified, anytime that value is located, the corresponding column will be nulled. Working with CSV Files A comma-separated value (CSV) file is a format accepted by possibly every mainstream rela- tional database in existence today, not to mention a wide variety of products such as Microsoft Excel. You can easily create a CSV file from a PostgreSQL table by using COPY accompanied by the CSV clause. For instance, to create a file capable of immediately being viewed in Microsoft Excel or OpenOffice.org Calc, execute the following command: psql>COPY employee (name, email) TO '/home/jason/sqldata/employee.csv' CSV HEADER; Specifying the HEADER clause as indicated above causes the names of the retrieved columns to be listed in the first row as column headers. For example, executing this command and opening the employee.csv file in Microsoft Excel produces output similar to that shown in Figure 37-1. Figure 37-1. Viewing the employee.csv file in Microsoft Excel Gilmore_5475C37.fm Page 781 Tuesday, February 7, 2006 2:31 PM 782 CHAPTER 37 ■ IMPORTING AND EXPORTING DATA If you are reading a CSV file into a table using COPY FROM and the HEADER clause is declared, the first line will be ignored. Some data may be delimited by single or double quotes, which have special significance within PostgreSQL, so you need to be aware of them to make sure they are properly accounted for. You can use the QUOTE clause to specify this character, which by default is set to double quotes. The specified quotation character can then be escaped using the character identified by the ESCAPE clause, which also defaults to double quotes. If you’re exporting data from a table and use FORCE NOT NULL, it is presumed that no value is null; if any null value is encountered, it will be inserted as an empty string. If you’re importing data into a table and use FORCE QUOTE, then all non-null values will be quoted, either using the default double quotes or whatever value is specified if the QUOTE clause is declared. Calling COPY from a PHP Script While the COPY command as described previously is useful for developers and database administrators, certainly a more intuitive solution is required for end users. To satisfy this need, the pg_copy_from() and pg_copy_to() functions (introduced in Chapter 30) are made available via PHP’s PostgreSQL extension. Both functions operate identically to the previously introduced COPY FROM and COPY TO commands, respectively, except that they’re also easily executable from within your Web application. In this section, we’ll consider a real-world example in which pg_copy_to() is used to copy data from a PostgreSQL table to a text file. Copying Data from a Table to a Text File Suppose you want to create an interface that allows managers to create CSV files consisting of employee contact information. These files are saved by date to a folder made available to a directory placed on a shared drive. The code for doing so is found in Listing 37-1. Listing 37-1. Saving Employee Data to a CSV File (saveemployeedata.php) <?php $pg = pg_connect("host=localhost user=jason password=secret dbname=corporate") or die("Could not connect to db server."); // Copy the employee table data to an array $array = pg_copy_to($pg, "employee", ","); // Retrieve current date for file-naming purposes $date = date("Ymd"); // Open the file $fh = fopen("/home/reports/share/employees-$date.csv", "w"); Gilmore_5475C37.fm Page 782 Tuesday, February 7, 2006 2:31 PM CHAPTER 37 ■ IMPORTING AND EXPORTING DATA 783 // Collapse the array to a newline-delimited set of rows $contents = implode("\n", $array); // Write $contents to the file fwrite($fh, $contents); // Close the file fclose($fh); ?> Once the script has executed, open the newly created file and you’ll see output similar to this: 1,JG1000011,Jason Gilmore,jason@example.com 2,RT435234,Robert Treat,rob@example.com 3,GS998909,Greg Sabino Mullane,greg@example.com 4,MW777983,Matt Wade,matt@example.com Now try opening this in a spreadsheet program such as Microsoft Excel or OpenOffice.org Calc! You can also easily add a header to the CSV file by writing a line to it before outputting the array contents, like so: fwrite($fh, "Employee ID,Name,Email\n"); Importing and Exporting Data with phpPgAdmin If you’re looking for a convenient and powerful administration utility that is capable of being accessed from anywhere you have a Web browser, phpPgAdmin (http://www.phppgadmin.net/) is the most capable solution around. First introduced in Chapter 27, phpPgAdmin is capable of managing your database with ease, in addition to both importing and exporting data in a variety of formats. ■Note At the time of writing, using this phpPgAdmin feature with Windows is not supported. To export data, navigate to the target table and click the Export link located in the top-right corner of the page. Doing so produces the interface found in Figure 37-2. Gilmore_5475C37.fm Page 783 Tuesday, February 7, 2006 2:31 PM 784 CHAPTER 37 ■ IMPORTING AND EXPORTING DATA Figure 37-2. phpPgAdmin’s export interface As you can see, you can export data in three ways: • Only the table data: If you want to export only the data, you can do so in six different formats, including COPY, CSV, SQL, Tabbed, XHTML, and XML. • Only the table structure: If you want to export just the table structure, then the table creation SQL syntax is exported. Checking the corresponding Drop checkbox causes table DROP commands to be inserted at the top of the output file so that any preexisting tables of the same name are dropped before being re-created. • Both the data and structure: If you want to export both the table structure and the table data, you can choose to export it in both COPY and SQL formats. Checking the corre- sponding Drop checkbox causes table DROP commands to be inserted at the top of the output file so that any preexisting tables of the same name are dropped before being re-created. Note that in all cases, you can either output the information to the browser or download it. Choosing to download it saves the information in a file with the extension .sql before prompting you to download it to your local computer. Importing data is accomplished by navigating to the target table and clicking the Import menu item. Doing so produces the interface found in Figure 37-3. Figure 37-3. phpPgAdmin’s import interface Imported files are accepted in four formats: Auto, CSV, Tabbed, and XML. The purpose of each format should be obvious, except for perhaps Auto. Selecting Auto causes phpPgAdmin to Gilmore_5475C37.fm Page 784 Tuesday, February 7, 2006 2:31 PM CHAPTER 37 ■ IMPORTING AND EXPORTING DATA 785 choose one of the other three formats by examining the file extension (valid extensions are .csv, .tab, and .xml). Also, if any null characters are found in the file, you can specify whether they appear using the sequence \N, the word NULL, or as an empty string. Summary As you learned in this chapter, you have several options at your disposal for importing data into and exporting data from your PostgreSQL database. You can do it manually via the command line or through scripting by using the COPY command. Or, you can incorporate these features into a Web application by using PHP’s pg_copy_to() and pg_copy_from() functions. Alternatively, you can rely on applications such as phpPgAdmin to facilitate the process. This concludes the book. We hope you enjoyed reading it as much as we enjoyed the process of writing it. Good luck! Gilmore_5475C37.fm Page 785 Tuesday, February 7, 2006 2:31 PM Gilmore_5475C37.fm Page 786 Tuesday, February 7, 2006 2:31 PM 787 Index ■Numbers and symbols .NET Framework SDK, 512 ? command, psql, 613 @@ operator using full-text indexes, PostgreSQL, 757 401 (Unauthorized access) message. hard coded authentication, 329 HTTP authentication, 325 sending to user, 327 404 (File not found) message, 321 ■A A (IPv4 Address Record) record type, DNS, 360 A6 (IPv6 addresses) record type, DNS, 360 AAAA (IPv6 Address Record) record type, DNS, 360 abstract classes, OOP, 168–169 abstract classes or interfaces, 169 description, 157 inheritance, 168 instantiation, 168 abstract keyword, 169 abstract methods, 146 accented languages localized formats, 280 AcceptPathInfo directive configuring Apache lookback feature, 316 access privilege system, PostgreSQL, 651–662 accessibility, 475 accessors, 140 getter (_get) method, 142 Account Domain/Name parameters installing PostgreSQL, 586 ACID tests, transactions, 765 actor parameter SoapClient constructor, 503 SoapServer constructor, 508 addDays method, 294 addFunction method creating SOAP server, 509 adding data ldap_mod_add function, 416 adding entries ldap_add function, 416 addition (+) operator, 71 addl_headers parameter mail function, 368 addl_headers parameter, mail() sending e-mail with additional headers, 369 addl_params parameter mail function, 368 addMonths method, 295 Addresses option installing PostgreSQL, 587 addslashes function, 34 AddType directive installing PHP on Linux/Unix, 13 installing PHP on Windows, 15 addWeeks method, 296 addYears method, 297 adl attribute, messages, 379 administration PostgreSQL, 593–610 Administrator Account option installing PostgreSQL, 587 affectedRows function, PostgreSQL, 691 Gilmore_5475Index.fm Page 787 Thursday, February 9, 2006 5:29 PM [...]... COPY FROM command, 7 78 CREATE DATABASE command, 626 delimited fields, 779 CREATE DOMAIN command, 646 COPY TO command, 7 78 CREATE FUNCTION command, 727 copying data to /from tables, 7 78 782 CREATE GROUP command, 659 changing default delimiter, 780 CREATE INDEX command, 753 copying data from a table, 7 78 CREATE RULE command, 709 copying data from a text file, 779 CREATE SCHEMA command, 627 copying data from. .. invoking parent constructors, 150 opening, SQLite, 5 38 539 invoking unrelated constructors, 150 PDO_ATTR_CONNECTION_STATUS attribute, 560 overloaded constructors, 185 PostgreSQL database class, 693 PHP 4, 1 48 securing PostgreSQL, 661 sqlite_close function, 540 sqlite_open function, 5 38 connections, PostgreSQL overloading, 151 constructors, OOP, 1 48 151 inheritance and constructors, 164–165 containers... setting, PostgreSQL, 5 98 casting, 54 feeds, MagpieRSS, 485 page caching, 4 68 Smarty templating engine, 450 $cache_lifetime attribute, 4 68 471 creating multiple caches per template, 470 is_cached method, 469 Calendar package, PEAR, 285 – 288 classes, 286 creating monthly calendar, 286 – 288 date and time classes, 286 decorator classes, 286 installing, 285 isValid method, 288 tabular date classes, 286 validating... datatype, PostgreSQL, 6 38 double quotes string interpolation, 75 downloads Apache, 9 10 PHP, 10 11 PostgreSQL, 579– 581 Unix version, 580 Windows version, 580 – 581 ACID tests for transactions, 766 dynamic extensions PHP configuration directives, 39 ■E e option, psql, 614 E_XYZ levels PHP error reporting levels, 30, 1 78 each array function, 113 echo statement, PHP, 48 effective_cache_size setting, PostgreSQL,... 777– 785 phpPgAdmin, 783 – 785 mailbox administration, 388 – 389 in_array array function, 111 mailboxes and messages, 375–3 78 in_reply _to attribute, messages, 380 message administration, 389 include directory NNTP protocol, 372 opening and closing connections, 374 opening connections to IMAP mailboxes, 374 POP3 protocol, 372 purpose and advantages, 372 requirements, 373–374 retrieving messages, 3 78 386 sending... pg_copy _to, 683 function parameters pg_delete, 685 variable scope, PHP, 61 pg_execute, 686 functional indexes, PostgreSQL, 754 pg_fetch_array, 6 78 functions, 91 101 pg_fetch_assoc, 680 see also string functions; string function actions pg_fetch_object, 680 80 9 Gilmore_5475Index.fm Page 81 0 Thursday, February 9, 2006 5:29 PM 81 0 ■I N D E X pg_fetch_row, 680 getArray method, 291 pg_free_result, 6 78 getAttribute... PGSQL_ERRORS_DEFAULT, 6 78 brief introduction, 177 PGSQL_ERRORS_TERSE, 6 78 closelog function, 182 PGSQL_ERRORS_VERBOSE, 6 78 define_syslog_variables function, 181 PGSQL_FATAL_ERROR, 675 logging options, 182 PGSQL_NONFATAL_ERROR, 675 openlog function, 181 retrieving error information, 562 permissions, 180 sending custom message to syslog, 182 syslog function, 182 using logging functions, 181 error messages PHP file... exporting data, 777– 785 phpPgAdmin, 783 – 785 retrieving and displaying data, PostgreSQL, 6 78 681 rows selected and rows modified, 681 sanitizing user data, 524–5 28 data encryption, 5 28 532 Auth_HTTP class, PEAR, 337 MCrypt, 531 mcrypt_decrypt function, 532 mcrypt_encrypt function, 531 md5 function, 529 mhash function, 529, 530 PHP 4 features, 3 PHP s encryption functions, 5 28 data handling deleting LDAP... option installing PostgreSQL from source, 583 datatypes, PostgreSQL, 635–640 attributes of, 635, 640–644 CHECK, 640 authenticating user against PostgreSQL table, 332 DEFAULT, 641 authenticating using login pair and IP address, 333 NULL, 642 database class see PostgreSQL database class Database Cluster option installing PostgreSQL, 587 Database Drivers options category installing PostgreSQL, 586 DATABASE... Page 788 Thursday, February 9, 2006 5:29 PM 788 ■I N D E X Afilias Inc PostgreSQL users, 576 running with VACUUM, 603 AND (&&) operator, 73 AFTER trigger, PostgreSQL, 740, 741 answered attribute, messages, 379, 383 aggregate functions, PostgreSQL, 724 ANY record type, DNS, 360 aggregate functions, SQLite Apache creating, 551–553 sqlite_create_aggregate function, 552 aggregators, RSS MagpieRSS, 483 popular . PEAR, 285 – 288 classes, 286 creating monthly calendar, 286 – 288 date and time classes, 286 decorator classes, 286 installing, 285 isValid method, 288 tabular date classes, 286 validating dates and. directive, 4 28 Coordinated Universal Time, 271 COPY command, PostgreSQL, 777– 783 BINARY keyword, 779 calling from PHP script, 782 – 783 COPY FROM command, 7 78 delimited fields, 779 COPY TO command, 7 78 copying. 582 command-line interface, PostgreSQL, 611 commands escapeshellcmd function, 527 PGSQL_COMMAND_OK value, 674 commands, PostgreSQL, 667–671 commands, psql, 613–614 controlling command history,