views:

254

answers:

5

I am developing an attendance management program, used to maintain the absence record of a student. Users of this software will need to enter various dates, updated once in a month: for instance, a list of dates on which a student was absent for that particular month would be entered, and my program must then store them into a database with each date added as a new row in the appropriate table.

I have the dates stored using arrays internally, how might I transfer these into the database? How should I proceed?

A: 

Iterate over the array and execute insert SQL for each date.

Bhushan
why why why? because I don't think iterating over queries without reflection and consideration of better options is very good. but as I said to johnnietheblack, my triggerfinger was too fast and I removed my downvote.
tharkun
+2  A: 

You have not mentioned the database system being used, so my reply is general in nature. The usual way to do this is to run multiple insert statements one after another:

INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('c', 'd');
INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('e', 'f');
GO

The trick way to do this is to use the UNION ALL statement:

INSERT INTO Table1 (FirstColumn, SecondColumn)
SELECT 'a', 'b'
UNION ALL
SELECT 'c', 'd'
UNION ALL
SELECT 'e', 'f'
GO

Versions of SQL Server prior to 2008 support only these methods. But SQL 2008 and MySQL 3.22 and above support the Row construction method as well:

INSERT INTO Table1 (FirstColumn, SecondColumn)
VALUES ('a', 'b'),
VALUES ('c', 'd'),
VALUES ('e', 'f')
GO

Now you can use any of the above methods to iterate through your array and add individual attendance rows to the database.

Cerebrus
I like the third way if it works
Cyril Gupta
Yup, it's kinda cool, isn't it!
Cerebrus
+2  A: 
foreach($arrayName as $arrayValue) {

// run your query here!

}

for example:

$myArray = array('apple','orange','grape');

foreach($myArray as $arrayFruit) {
$query = "INSERT INTO `Fruits` (`FruitName`) VALUES ('" . $arrayFruit . "')";
mysql_query($query, $connection);
}

does that makes sense / fit what you were thinking?

johnnietheblack
you might want to use bind variables...
Thilo
very true...i second that...just giving a simple example tho. if the variables are provided by the user, or you have a ton of variables in the array, then def use bind variables
johnnietheblack
alright, fair enough...but id like to understand how to do it otherwise? im serious...if you have a resource it'd be sweet.
johnnietheblack
What's wrong with putting code inside of a commonplace control structure? Would it be any better if the database query was put in a recursive function? Or put in a custom function that was called from a loop?
Calvin
ok, let me explain. my morning trigger finger was maybe a bit fast. I took the downvote off.. BUT still: whenever you can avoid putting queries into loops of any kind, do it! Cerberus' solutions are good! For reading queries, this is even more important!!!
tharkun
There will be a HUGE impact on performance, if the whole thing scales up and you're adding or reading more and more entries! I've made the mistake once and I've rewritten that app.
tharkun
oh my... something is wrong with the removal of votes.
tharkun
thanks for the rethinking, tharkun...check your comment below...im still curious as to the most efficient method
johnnietheblack
+1  A: 

Do you want to store the dates seperately so you can juggle with them, query them, etc.? Or do you just want to store the array as is?

If you want to store the dates separately you may want to create a table with an FK to students, a column for date and a column for the nature of the date, like absence, late, ...

Then you would indeed store the single dates into that table. If you must, by iterating but if you can with one of Cerbrus' solutions!. It is not recommended to have db-queries within loops.

If you just need to store that array somewhere, you can serialize it and store the serialized string in a text or varchar column.

tharkun
so, do you suggest dynamically building a UNION statement with the loop - if the array is dynamic in nature? ...and then instead of iterating multiple queries, just using the UNION once?
johnnietheblack
yes! not maybe for a small app which I know will never scale. or a small task which I know will never take more than a few iterations because of its nature.
tharkun
right...good technique for sure. this is valuable to me because typically i have to run pretty large dynamic loops for what i do...which is why i wanted to know
johnnietheblack
that's what I do as well... and I've had a case where I managed to push a dedicated server to and over the limit just because of a "few" recursive read queries. after that I started learning SQL.
tharkun
A: 

What language and what type of database are you using? Is this a web application? A desktop application? We can't help you without more information.

Depending on your situation, any of the above solutions could work. Or you could even load all of the attendance records at once as a CSV or XML document. Perhaps a little more research on your part will help you ask a more useful question?

Calvin