Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: [Beginner] Programming Challenge: 7

  1. #1
    Join Date
    Apr 2007
    Beans
    14,781

    [Beginner] Programming Challenge: 7

    This new challenge with be something useful. It will be dealing with databases. This means, you will have to learn a new API (don't worry, they are usually very simple) and possibly a bit of SQL, although you can get around that with various modules.

    The Challenge
    Write a program which takes two input fields, name and age. Name with be a string (assume that 64 characters is the limit, if you need a limit), age will be a positive integer.

    After checking if the data is valid, the program will put them in a database.

    The program will have to somehow also be able to retrieve the records and output them. I suggest a simple text interface:

    Code:
    Ask user if they want to input data or retrieve
                           |
                           |
    _______________________|_______________________
    |                                             |
    |                                             |
    Retrieve: Output all                        Input 
    records neatly                                |
                                    Get names ____|____ Exit on "exit"
                                    and ages, 
                                    validating 
                                    as it goes

    The Reasons
    This challenge has aspects of the others. A new API, a new language (possibly, but SQL isn't hard at all), input, and it will benefit from structured programming.

    Databases are extremely useful and knowing how to use them will help anyone.

    Hints:
    This challenge will be easiest with high level languages. I recommend sqlite, because it is small and very easy to use. See http://laroza77.wikidot.com/sql on database engines/libraries and how to use SQL (see the individual modules for the language on the interfaces).

    Some languages come with database capabilities. Java and Python are two that I know.

    This database will be one table, one text field and one integer field.

    How it will be graded
    It won't be. You'll get feedback (hopefully) from those who can give it. You should focus on learning and trying to use new concepts.

  2. #2
    Join Date
    Jun 2008
    Location
    California, USA
    Beans
    1,030
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [Beginner] Programming Challenge: 7

    Mmm, databases. This is a good excuse for me to get familiar with them.

    Blog | I'm available for programming contributions. C & Python.
    Intel Core i7 920 | EVGA x58 SLI | NVidia GeForce 8600 GT | WD 500GB HDD | Corsair XMS3 3GB | Ubuntu 9.04

  3. #3
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [Beginner] Programming Challenge: 7

    WHOA! Now I'm excited!

    I've never had the chance to practice with databases before, so this will be a first. I'm going to think about how I'm going to go about designing this while I sleep tonight, and get to work tomorrow.

    Thanks again, LaRoza!
    Programming is an art. Learn it. Live it. Love it.

  4. #4
    Join Date
    Nov 2006
    Location
    Pennsylvania
    Beans
    423

    Re: [Beginner] Programming Challenge: 7

    I am pretty excited for this project. Hopefully this will motivate me to set up my box that is marked to be a web server / db server
    Your Ubuntu User number is # 15355

    A must Read for anyone interested in Computer Programming.

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 7

    A prehistoric and simple sequential database, dBase.
    Code:
    <?php
    if( isset($_POST["submit"]) ){
        if ( (!empty($_POST['username']) && strlen($_POST['username']) <=64) &&
           ( !empty($_POST['age']) && $_POST['age']< 100 ) )        
        {    
            $dbfields = array(
                array("username","C",64),
                array("age","N",3,0)
            );
            define("USERDB", "/tmp/userdb.dbf");
            if( !file_exists(USERDB) ) {
                if ( !dbase_create(USERDB,$dbfields) ){                            
                    echo '<p>Cannot create database</p><br/>';
                    exit();
                }else{
                        //create dummy user at first time use,open db for r,w
                        $dbid = dbase_open(USERDB,2);
                        $dummy= array( "dummy",1.0);
                        dbase_add_record($dbid,$dummy);
                        dbase_close($dbid);            
                }
            }
            
            $username = trim($_POST['username']);
            $age = trim($_POST['age']);
            //found user flag.
            $found=0;
    
            //open database r,w and retrieve information from db
            if ( $dbid = dbase_open(USERDB,2) ){
                $recnum = dbase_numrecords($dbid); 
                for($i=1;$i<=$recnum;$i++){
                    $row = dbase_get_record_with_names($dbid,$i);
                    $rname = trim($row['username']);
                    $rage= trim($row['age']);
                    if  ( $rname == $username){
                        echo '<h1><p>Found userName: '.$rname.' , Age: '.$rage.'</p></h1><br/>';
                        $found=1;
                        break;
                    }
                }
                if ( !$found ){
                    echo '<h1><p>Created user: '.$username."Age: $age</p></h1><br/>";
                    $record = array($username,$age);
                    dbase_add_record($dbid,$record);               
                    dbase_close($dbid);
                }
                
            }else{
                echo '<p>Error opening database</p>';
            }
            
        }else{
            echo '<p>Invalid input</p>';   
        }
            
        
    }else{
    ?>
        <!-- show form -->
        <html><head>
                <title>Demo DBase (DBM style) database</title>
              </head>
        <body>
                <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
                    <table align="left">
                    <tr>
                        <td>Enter name:</td>
                        <td><input name="username" maxlength="64" size="64" type="text" />
                    </tr>
                    <tr>
                        <td>Age:</td>
                        <td><input name="age" size="3" maxlength="3" type="text" />
                    </tr>                
                    <tr>
                        <td colspan="2">
                            <input type="submit" name="submit">
                        </td>
                    </tr>
                    </table>
    
                </form>
        
        </body>
        </html>
        
    <?php
    }
    ?>

  6. #6
    Join Date
    Sep 2006
    Location
    Central Europe
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: [Beginner] Programming Challenge: 7

    Quote Originally Posted by LaRoza View Post
    but SQL isn't hard at all
    I'd be careful about saying that (unless you ment the syntax).

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 7

    Quote Originally Posted by MacUntu View Post
    I'd be careful about saying that (unless you ment the syntax).
    Well, the level of understanding needed for this challenge.

  8. #8
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 7

    do i have to use a database or can i create my own file extension file (.data) and parse it?

  9. #9
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 7

    Quote Originally Posted by jimi_hendrix View Post
    do i have to use a database or can i create my own file extension file (.data) and parse it?
    You don't have to do anything The point of the exercise is using a database, not getting around using one though.

    Although it isn't stated, an additional requirement was going to be to allow people to use the application to get the ages of people whose names are entered or to get all people of inputted ages.

  10. #10
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 7

    odd...i cant figure out how to install sqlite...none of the files i try will install it

Page 1 of 4 123 ... LastLast

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
  •