360 likes | 370 Views
Chapter 5. Updating Records in a Table. Updating a table. Updating a table refers to editing (changing) records, adding new records (inserting) or deleting records. Contents in a table can be changed interactively using the BROWSE or EDIT Windows.
E N D
Chapter 5 Updating Records in a Table Foxpro Chapter 5
Updating a table • Updating a table refers to editing (changing) records, adding new records (inserting) or deleting records. • Contents in a table can be changed interactively using the BROWSE or EDIT Windows. • Alternatively they can be changed using a FoxPro command. Foxpro Chapter 5
Updating vs. Querying • Updating: • changing the data value within a table or the structure of the table itself. • Querying: • reading data values but not changing them. Foxpro Chapter 5
Ways to update • Editing && fields of a record • Inserting && a record • Deleting && a record • Importing && a file • Exporting && a file • Summing && numeric fields Foxpro Chapter 5
EDITINGRecords can be changed in the Browse/Edit Windows, orthey can be changed using theReplace command Foxpro Chapter 5
Replace Several Fields USE project ... REPLACE date WITH {01/03/2010},; maxTemp WITH 22, minTemp WITH 16 Further Practice 4 Q.1 Foxpro Chapter 5
Replace All Records or Certain Records • USE formPos Further Practice 1A Q.5 ... REPLACE position WITH RECNO() ALL • USE patient Further Practice 5A Q.5 ... REPLACE name WITH ‘Mr. ’+name FOR sex =‘M’ Foxpro Chapter 5
Replace Contents of Memo Let remark be a field of memo type in table student USE job REPLACE remark with; ‘Urgent. Delay more than one week!”; ADDITIVE for … Further Practice 8A Q.2 Foxpro Chapter 5
BLANK USE project … BLANK FIELDS minTemp, maxTemp, ; range, aveTemp ALL Further Practice 4A Q.5 Foxpro Chapter 5
INSERTINGNew records can be added to a table by using the command APPEND Foxpro Chapter 5
APPEND • Put an additional record at the end of the table. • Open an input window for you to key in data. • If an index is in effect, the record will immediately be moved to its appropriate location. • Usually used interactively. Foxpro Chapter 5
APPEND BLANK • Put an additional record to the end of the table. • If an index is in effect, the record will immediately be moved to its appropriate location. • Usually used in programs. Foxpro Chapter 5
DELETING Foxpro Chapter 5
DELETE and PACK Use anyfile go 2 DELETE && The 2nd record is marked for deletion (logically deleted) PACK && The 2nd record is permanently deleted (physically deleted) Foxpro Chapter 5
SET DELETE ON • SET DELETE ON DELETE ALL LIST && no records will be seen • SET DELETE OFF && default DELETE ALL LIST && all records will be shown Foxpro Chapter 5
RECALL DELETE ALL && mark all record for deletion RECALL ALL && unmark all Foxpro Chapter 5
DELETED() USE student Go 2 DELETE ? DELETED() && output .T. RECALL ? DELETED() && output .F. Foxpro Chapter 5
DELETED() To browse only records marked for deletion: USE student BROWSE FOR DELETED() = .T. or, BROWSE FOR DELETED() Foxpro Chapter 5
PACK • Physically remove all records marked for deletion. • Cannot be recovered or recalled. • Can you think of a way to recover the physically deleted records? Foxpro Chapter 5
ZAP Equivalent to DELETE ALL PACK • No record is left • The table is empty but not the .DBF file • The .DBF file still holds information about the structure of the table • The .DBF file is the same as that formed by COPY STRUCTURE Foxpro Chapter 5
COPY STRUCTURE Use f1 COPY STRUCTURE TO f2 USE student COPY STRUCTURE TO namelist FIELDS class, classno, name Foxpro Chapter 5
IMPORTING Foxpro Chapter 5
APPEND FROM a delimited file USE student APPEND FROM newstxt TYPE DELIMITED • A delimited file is an ASCII text file in which records are separated from one another by a carriage return and linefeed. Field contents are by default separated by commas. Foxpro Chapter 5
APPEND FROM a SDF file USE student APPEND FROM newssdf TYPE SDF • SDF stands for System Data Format in FoxPro. An SDF file is an ASCII text file in which records have a fixed length and end with a carriage return and linefeed. Fields are not delimited. Each row of data in the SDF is filled into a record according to the width of each fields Foxpro Chapter 5
APPEND FROM [a dbf file] USE complex APPEND FROM temp Further Practice 6A Q.5 Foxpro Chapter 5
APPEND FROM an excel file USE student APPEND FROM newsxls TYPE XL5 With our machines, Excel files can be imported only if they are of version 5 or earlier. If the source file is in Excel97 or newer version, first save it in Excel95 version. Foxpro Chapter 5
EXPORTING Foxpro Chapter 5
COPY FILE CLOSE ALL COPY FILE anyfile TO newfile • anyfile must not be open. • wildcard characters are not allowed. • The filenames concerned must contain extension if there are any. • Behind the screen: FoxPro call the OS to perform a DOS copy or something similar. Foxpro Chapter 5
COPY TO USE student COPY TO studback USE f2001 COPY TO f2002 FOR PROMOTED=.T. COPY TO repeater FIELDS class, classno, name FOR PROMOTED=.F. Foxpro Chapter 5
Exporting to text files USE student COPY TO stcomma.txt TYPE DELIMITED COPY TO stsdf.txt TYPE SDF Foxpro Chapter 5
Exporting to Excel 5 USE student COPY TO stxls.xls TYPE XL5 Foxpro Chapter 5
SUMMING Foxpro Chapter 5
TOTAL • The total command computes totals for numeric fields in the table currently in use. • The results will be stored in a newly created table with the same structure (by default) as the one in use. Foxpro Chapter 5
TOTAL • Records should first be arranged in groups • In this case class should be grouped USE APStud TOTAL ON class FIELDS award_cnt, ; punish_cnt TO result Further Practice 7A Q.5 Foxpro Chapter 5
TOTAL • For non-numeric fields, values in the first record of each group will be shown (In SQL, that of the last record is shown). • This can be quite misleading in most circumstances. • If only numeric fields and non-numeric fields with consistent value within each group are shown, the result will be more satisfactory. Foxpro Chapter 5
Backing Up • Grandfather-Father-Son Concept • see p.142-143 • Done by you, not the computer • Use 3 tapes A, B and C f1 --> f2 --> f3 --> f4 A --> B --> C --> A grandfa --> father --> son data in f1 are lost as tape A is overwritten with data in f4 Foxpro Chapter 5