CSV Database - Demo program

by shian ⌂, Sunday, September 13, 2020, 14:27 (5 days ago) @ shian

CSV (Comma Separated Values) is a very simple text file database. Used by many applications, therefore useful for export and import data to/from other programs.

You can download the demo from here.
OK, that's the program (I can use Euphoria 3 syntax coloring!!!) :ok: :

 
-- short demo of creating and using CSV (Comma Separated Values) database.
-- in this database we store 4 fields in each line: name, gender, age, hobby.
--
-- shian, v1.00, 13/September/2020 (hopefully without bugs)
 
 
include get.e
include string.e    -- Lib2 1.41
include machine2.e  -- Lib2 1.41
 
 
-- with trace -- for debugging
 
 
-- file constants
constant FILE = "hobby.csv",    -- name of CSV database
         DELIMITER = ",",       -- delimiter string, normally a comma ','
         REPLACEMENT = ";",     -- replace delimiter in-string with another char
         NEW_LINE = "\r\n",     -- (CR+LF is the Internet standard of New-line)
         ALL_DELIMITERS = DELIMITER & NEW_LINE  -- used by split() function
 
 
 
-- append a single record to the end of the database file
procedure append_record()
    integer fn -- Euphoria's file number
    sequence name, hobby
    object gender, age
    sequence line
 
    -- trace(1) -- for debugging
 
    -- open text file for appending
    fn = open(FILE, "a")
 
    if fn = -1 then -- open error?
        puts(1, "\nCannot open '" & FILE & "' for appending!\n")
        abort(1)
    end if
 
    -- get user's input
    clear_screen()
    name   = prompt_string("Enter Name: ")
    gender = prompt_number("Enter Gender [Male = 0; Female = 1]: ", {0, 1})
    age    = prompt_number("Enter Age: ", {1, 120})
    hobby  = prompt_string("Enter Hobby: ")
 
    -- make valid strings
    name   = translate(name, DELIMITER, REPLACEMENT)
    gender = str(floor(gender))
    age    = sprintf("%g", age)
    hobby  = translate(hobby, DELIMITER, REPLACEMENT)
 
    -- append line to the CSV database and close file
    line = join({name, gender, age, hobby}, DELIMITER)
    puts(fn, line & NEW_LINE)
 
    close(fn)
end procedure
 
 
-- print all records in the database file
procedure print_records()
    integer fn -- Euphoria's file number
    object line
    integer line_number
 
    -- open text file for reading
    fn = open(FILE, "r")
 
    if fn = -1 then -- open error?
        puts(1, "\nCannot open '" & FILE & "' for reading!\n")
        abort(1)
    end if
 
    line_number = 0
 
    -- display all records in database
    while TRUE do
        line = gets(fn)
 
        if atom(line) then
            puts(1, "\nDone... ")
            pause()
            exit
        end if
 
        line_number += 1
 
        -- split line into fields,
        -- e.g. {"field1", "field2", "field3", "field4"}
        line = split(line, ALL_DELIMITERS)
 
        -- avoid crash, make sure that 4 fields exist
        if length(line) >= 4 then
 
            -- convert 0 or 1 to gender
            line[2] = iif(equal(line[2], "0"), "Male", "Female")
 
            printf(1, '\n' &
                "Line #%03d\n" &
                "=========\n" &
                "[1] Name:   %s\n" &
                "[2] Gender: %s\n" &
                "[3] Age:    %s\n" &
                "[4] Hobby:  %s\n", line_number & line[1..4])
 
            puts(1, "...\n")
            pause()
        end if
    end while
end procedure
 
 
-- main menu runs the program
procedure main_menu()
    integer choice
 
    -- put menu inside endless loop
    while TRUE do
        clear_screen()
        puts(1,
            " Hobby CSV Database\n" &
            " ******************\n\n")
 
        -- get user's choice
        choice = floor(prompt_number(
            " 0=Quit, 1=Add record, 2=Print records: ", {0, 2}))
 
        if choice = 1 then
            append_record()
        elsif choice = 2 then
            print_records()
        else
            exit -- end.
        end if
    end while
end procedure
 
 
main_menu() -- start here
 
 
-- End.
 

Tags:
Database, demo, csv


Complete thread:

 RSS Feed of thread

powered by my little forum