New call-to-action
Results 1 to 14 of 14

Thread: XBasic Restructuring Table

  1. #1
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default XBasic Restructuring Table

    I have a table with 200 records which I imported from a comma delimited text file that I want to restructure. I have tried the code below, but it throws away the records. Is there a way that the table structure can be altered using XBasic, without throwing away the data? or is there another approach to this

    dim tblCh as P
    tblch = table.open("1wthdrxp",FILE_RW_EXCLUSIVE)
    tblch.table_restructure(<<%str%
    CUSTOMER_ID,Character,8,0
    FIRSTNAME,Character,25,0
    LASTNAME,Character,25,0
    COMPANY,Character,32,0
    PN=PHONE,Character,20,0
    FAX,Character,20,0
    DOB,Date,8,0
    %str%)
    tblch.close()

    Manay Thanks,

    Peter

  2. #2
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default

    Quote Originally Posted by gagsoft
    I have a table with 200 records which I imported from a comma delimited text file that I want to restructure. I have tried the code below, but it throws away the records. Is there a way that the table structure can be altered using XBasic, without throwing away the data? or is there another approach to this

    dim tblCh as P
    tblch = table.open("1wthdrxp",FILE_RW_EXCLUSIVE)
    tblch.table_restructure(<<%str%
    CUSTOMER_ID,Character,8,0
    FIRSTNAME,Character,25,0
    LASTNAME,Character,25,0
    COMPANY,Character,32,0
    PN=PHONE,Character,20,0
    FAX,Character,20,0
    DOB,Date,8,0
    %str%)
    tblch.close()

    Manay Thanks,

    Peter
    My first question would be why would you need to do this with xbasic when you could simply open the table in the control panel and restructure to suit?

    A second thought would be to import to a temporary table and then append the records to a created table with the desired sturcture.

  3. #3
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,322

    Default

    Peter,

    the help file explanation for the table_restructure() method mentions several different ways data loss can occur. I suspect one of two things. One of the methods mentioned in the documentation occurred, or your table name is causing you grief.

    From the help files:

    Table and Field Names

    Alpha Five recommends that table names, field names, and paths to your files start with a letter (A-Z or a-z) and be composed of letters, numbers (0-9), and underscore (_) characters. Do not use tilde "~" characters.
    Your table name begins with a digit, which is NOT recommended.

    What was your original table structure?

    --tom

  4. #4
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default

    Quote Originally Posted by Stan Mathews
    My first question would be why would you need to do this with xbasic when you could simply open the table in the control panel and restructure to suit?

    A second thought would be to import to a temporary table and then append the records to a created table with the desired sturcture.
    Hi Stan

    The append operation does not work because there are 2 date fields in the final table. The table that gets imported is a comma delimited text file which will be imported by the user. I want the user to be able to do the imorting and appending in one go.

    Thanks,

    Peter

  5. #5
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default

    Quote Originally Posted by Tom Cone Jr
    Peter,

    the help file explanation for the table_restructure() method mentions several different ways data loss can occur. I suspect one of two things. One of the methods mentioned in the documentation occurred, or your table name is causing you grief.

    From the help files:



    Your table name begins with a digit, which is NOT recommended.

    What was your original table structure?

    --tom
    Thanks Tom

    I will try renaming the table name.

    Many thanks,
    Peter

  6. #6
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default

    Quote Originally Posted by gagsoft
    Hi Stan

    The append operation does not work because there are 2 date fields in the final table. The table that gets imported is a comma delimited text file which will be imported by the user. I want the user to be able to do the imorting and appending in one go.

    Thanks,

    Peter
    You can append an expression which would convert the value in the temporary table from character to date. I import and append such files on a regular basis. Usually the files I import are recognized as all character values. I use expressions to make some fields in the final table numeric and some become dates. You can even append a value to the final table that doesn't even exist in the transaction (temporary table) by specifying a hard coded value, like the current date, or the value of some variable that is set before the append occurs.

    This can all be done with saved operations or the modified xbasic of the same. One button used by the user can trigger both.

  7. #7
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default XBasic Restructuring Table

    Hi Tom

    I did the following:
    Renamed the table to test.
    Renamed all field with no underscores
    Renamed the fields in the script removing the underscores see below:
    dim tblRes as P
    tblRes = table.open("Test",FILE_RW_EXCLUSIVE)
    tblRes.table_restructure(<<%str%
    CUSTOMERID,Character,8,0
    FIRSTNAME,Character,25,0
    LASTNAME,Character,25,0
    COMPANY,Character,32,0
    PHONE,Character,20,0
    FAX,Character,20,0
    ADDRESS1,Character,40,0
    ADDRESS2,Character,40,0
    BCITY,Character,20,0
    DStart,Date,8,0
    BPOSTALCODE,Character,10,0
    BCOUNTRY,Character,20,0
    SADDRESS1,Character,40,0
    DateReq,Date,8,0
    SCITY,Character,20,0
    SSTATEREGION,Character,20,0
    SPOSTALCODE,Character,10,0
    SCOUNTRY,Character,20,0
    SSAME,Logical,1,0
    EMAIL,Character,60,0
    %str%)
    tblRes.close()

    Maybe I should also mention that the fields in the table being restructured are named as follows: Field1, Field2, Field3..........

    It Still throws away the data.
    Any ideas?

    Many Thanks,

    Peter

  8. #8
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default

    Hi Stan

    Please accept my apolgies. I only saw your reply now. Would it be possible to show me an extract of the code to put me on the right track?

    Many Thanks,

    Peter.

  9. #9
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,322

    Default

    Peter,

    Let me ask again.

    What is the original table structure?

    You've supplied fieldnames (Field1, Field2, etc.) but not data types or widths.

    -- tom

  10. #10
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default XBasic Restructuring Table

    Hi Tom

    Humble apologies for that oversight.
    The table Structure for the imported table

    1 FIELD1 C 6 0
    2 FIELD2 N 7 0
    3 FIELD3 C 25 0
    4 FIELD4 C 7 0
    5 FIELD5 N 8 2
    6 FIELD6 N 11 6
    7 FIELD7 C 3 0
    8 FIELD8 N 6 0
    9 FIELD9 C 25 0
    10 FIELD10 C 8 0
    11 FIELD11 C 6 0
    12 FIELD12 C 1 0
    13 FIELD13 C 3 0
    14 FIELD14 C 8 0
    15 FIELD15 C 5 0
    16 FIELD16 C 1 0
    17 FIELD17 C 1 0
    18 FIELD18 C 1 0
    19 FIELD19 C 4 0
    20 FIELD20 N 7 0
    21 FIELD21 C 6 0
    22 FIELD22 C 1 0
    23 FIELD23 C 2 0

    Thanks,
    Peter

  11. #11
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,322

    Default

    Ok. I think the data loss is happening because of mismatches in the table structures. The sequence in your original table does not match the sequence in your final structure. Alpha Five has to move particular field values to new slots in each record's structure. This is failing. Stan's suggestion to use a temporary table and then do an append, where the append matches source and target fields explicitly, sounds like a practical solution. If time permits I'll do a bit of experimenting with an xbasic solution.

    ----------------------------------------------------------------------------
    Update
    ----------------------------------------------------------------------------
    Peter, you'll need to read the help file closely for the options available with <tbl>.table_restructure(). It's quite powerful and flexible.

    Turns out your syntax is wrong, mainly by failing to explicitly tie new fieldnames to old fieldnames.

    The attached example database contains two scripts in the Code page of the control panel. The first script (Chg_Fld_Seq) simply reorganizes the existing fields putting them in a different order within each record. The second script (Put flds to orig seq) puts them back in the original order.

    This same approach can be used to change the field width and field name of any field in the table.

    I haven't experimented with changing data types using this table method. Just rearranging field order within each record, changing field names, and changing field widths. These three work as advertised, but you MUST follow the specific sequence detailed in the help file.

    --------------------------------------------------------------
    Update 2
    --------------------------------------------------------------

    Turns out the table_restructure() method can handle data type conversions just fine, within the same limits applicable to manual restructuring of table (as explained the help docs).

    Using the attached Example database, here is another script which does several interesting things in one step:

    a) re-sequences the field order within each record
    b) changes the data type for the Id_num field from c,5,0 to n,5,0
    c) changes the field name for the Age field to C_Age
    d) changes the data type for the Age field (now C_Age) from n,3,0 to c,3,0

    Pretty impressive!

    Code:
    'Date Created: 26-Dec-2005 03:41:12 PM
    'Last Updated: 26-Dec-2005 03:41:12 PM
    'Created By  : TEC
    'Updated By  : TEC
    tbl = table.open("People")
    
    'Rearrange the order of fields within table structure
    'Original field order and structure is
    'Id_num,c,5,0
    'Age,n,3,0
    'Bdate,d,8,0
    'Fname,c,20,0
    'Lname,c,20,0
    
    'AND then change Id_num to N, 5, 0
    
    'AND then change name of Age field to c_Age
    
    'AND then change data type for C_Age field to C, 3
    
    list = <<%str%
    Lname = Lname,c,20,0
    Fname = Fname,c,20,0
    c_Age = Age,c,3,0
    Bdate = Bdate,d,8,0
    Id_Num = Id_num,n,5,0
    %str%
    tbl.table_restructure(list)
    -- tom
    Last edited by Tom Cone Jr; 12-26-2005 at 03:46 PM.

  12. #12
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default

    Quote Originally Posted by gagsoft
    Hi Stan

    Please accept my apolgies. I only saw your reply now. Would it be possible to show me an extract of the code to put me on the right track?

    Many Thanks,

    Peter.
    Attachment is an example.
    Last edited by Stan Mathews; 03-28-2006 at 02:24 PM.

  13. #13
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default

    Quote Originally Posted by Tom Cone Jr
    Ok. I think the data loss is happening because of mismatches in the table structures. The sequence in your original table does not match the sequence in your final structure. Alpha Five has to move particular field values to new slots in each record's structure. This is failing. Stan's suggestion to use a temporary table and then do an append, where the append matches source and target fields explicitly, sounds like a practical solution. If time permits I'll do a bit of experimenting with an xbasic solution.

    ----------------------------------------------------------------------------
    Update
    ----------------------------------------------------------------------------
    Peter, you'll need to read the help file closely for the options available with <tbl>.table_restructure(). It's quite powerful and flexible.

    Turns out your syntax is wrong, mainly by failing to explicitly tie new fieldnames to old fieldnames.

    The attached example database contains two scripts in the Code page of the control panel. The first script (Chg_Fld_Seq) simply reorganizes the existing fields putting them in a different order within each record. The second script (Put flds to orig seq) puts them back in the original order.

    This same approach can be used to change the field width and field name of any field in the table.

    I haven't experimented with changing data types using this table method. Just rearranging field order within each record, changing field names, and changing field widths. These three work as advertised, but you MUST follow the specific sequence detailed in the help file.

    --------------------------------------------------------------
    Update 2
    --------------------------------------------------------------

    Turns out the table_restructure() method can handle data type conversions just fine, within the same limits applicable to manual restructuring of table (as explained the help docs).

    Using the attached Example database, here is another script which does several interesting things in one step:

    a) re-sequences the field order within each record
    b) changes the data type for the Id_num field from c,5,0 to n,5,0
    c) changes the field name for the Age field to C_Age
    d) changes the data type for the Age field (now C_Age) from n,3,0 to c,3,0

    Pretty impressive!

    Code:
    'Date Created: 26-Dec-2005 03:41:12 PM
    'Last Updated: 26-Dec-2005 03:41:12 PM
    'Created By  : TEC
    'Updated By  : TEC
    tbl = table.open("People")
    
    'Rearrange the order of fields within table structure
    'Original field order and structure is
    'Id_num,c,5,0
    'Age,n,3,0
    'Bdate,d,8,0
    'Fname,c,20,0
    'Lname,c,20,0
    
    'AND then change Id_num to N, 5, 0
    
    'AND then change name of Age field to c_Age
    
    'AND then change data type for C_Age field to C, 3
    
    list = <<%str%
    Lname = Lname,c,20,0
    Fname = Fname,c,20,0
    c_Age = Age,c,3,0
    Bdate = Bdate,d,8,0
    Id_Num = Id_num,n,5,0
    %str%
    tbl.table_restructure(list)
    -- tom
    Hi Tom

    Thanks for all the help, I got sorted.
    Enjoy what is left of the festive season.

    Many thanks,

    Peter

  14. #14
    Member
    Real Name
    Peter Gagiano
    Join Date
    Dec 2004
    Location
    Cape Town South Africa
    Posts
    424

    Default

    Quote Originally Posted by Stan Mathews
    Attachment is an example.
    Hi Stan

    Thank you for all the help.
    I got the import going perfectly now.

    Have a happy new year.
    Many Thanks,

    Peter

Similar Threads

  1. Restructuring a Table
    By Frank Cashman in forum Alpha Five Version 5
    Replies: 14
    Last Post: 05-02-2005, 07:41 PM
  2. Restructuring table with xbasic
    By Greg Fong in forum Alpha Five Version 4
    Replies: 3
    Last Post: 05-16-2002, 03:18 PM
  3. Restructuring a large database
    By Colin Younger in forum Alpha Five Version 1
    Replies: 2
    Last Post: 06-13-2001, 09:27 AM
  4. Restructuring alpha filing sys
    By Wanda Friesen in forum Alpha Five Version 4
    Replies: 9
    Last Post: 02-09-2001, 12:31 PM
  5. Restructuring Table
    By Michael Hernandez in forum Alpha Five Versions 2 and 3
    Replies: 0
    Last Post: 12-12-2000, 05:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •