[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.
Re: [Beginner] Programming Challenge: 7
Mmm, databases. This is a good excuse for me to get familiar with them.
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!
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 :)
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
}
?>
Re: [Beginner] Programming Challenge: 7
Quote:
Originally Posted by
LaRoza
but SQL isn't hard at all
I'd be careful about saying that (unless you ment the syntax). ;)
Re: [Beginner] Programming Challenge: 7
Quote:
Originally Posted by
MacUntu
I'd be careful about saying that (unless you ment the syntax). ;)
Well, the level of understanding needed for this challenge.
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?
Re: [Beginner] Programming Challenge: 7
Quote:
Originally Posted by
jimi_hendrix
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.
Re: [Beginner] Programming Challenge: 7
odd...i cant figure out how to install sqlite...none of the files i try will install it