|
|
| go ahead... be a heretic | |
| PerlMonks |
Re: Class::DBI newbieby 7stud (Deacon) |
| on Feb 02, 2013 at 04:44 UTC ( #1016640=note: print w/ replies, xml ) | Need Help?? |
|
The simplest setup employs one additional file next to your program file:
some_dir/ my_prog.pl Users.pmHere is the table I'm using:
CREATE TABLE TempAccountRegistry (
id INT(12) not null auto_increment primary key,
username VARCHAR(255),
password VARCHAR(50),
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
1) Create the file Users.pm, and put this code in it:
2) Now you are ready to write a program that uses the features of Class::DBI. Create a program file called my_prog.pl in the same directory as Users.pm, and put this code in it:
Here is the effect on your database: mysql> select * from TempAccountRegistry; +----+----------+----------+---------------------+ | id | username | password | created | +----+----------+----------+---------------------+ | 1 | Joe | 123 | 2013-02-02 00:12:00 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec) You can retrieve a row like this:
If you insert another row:
mysql> select * from TempAccountRegistry; +----+----------+----------+---------------------+ | id | username | password | created | +----+----------+----------+---------------------+ | 1 | Joe | 123 | 2013-02-02 00:12:00 | | 2 | Jim | 456 | 2013-02-02 00:12:00 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec) ...you can retrieve all the rows like this:
You can interpolate method calls into a string like this:
You can change the information in a row like this:
mysql> select * from TempAccountRegistry; +----+----------+----------+---------------------+ | id | username | password | created | +----+----------+----------+---------------------+ | 1 | Joe | 123 | 2013-02-02 00:12:00 | | 2 | Kathy | 456 | 2013-02-02 00:12:00 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec) Finally, you can delete a row like this:
mysql> select * from TempAccountRegistry; +----+----------+----------+---------------------+ | id | username | password | created | +----+----------+----------+---------------------+ | 2 | Kathy | 456 | 2013-02-02 00:12:00 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec) Read the docs for the various ways you can search the database and update info. Also, according to the docs: It's usually wise to set up a "top level" class for your entire application to inherit from, rather than have each class inherit directly from Class::DBI. This gives you a convenient point to place system-wide overrides and enhancements to Class::DBI's behavior. If you want to do that, here are the files you'll need:
/some_dir
my_prog.pl
MyDBIDatabase.pm
Users.pm
1) Create the file MyDBIDatabase.pm. Then put this code in MyDBIDatabase.pm:
2) Create another file called Users.pm. Put this code in Users.pm:
Any additional classes that you want to map to tables will also inherit from MyDBIDatabase instead of Class::DBI. 3) You can use the same code as above for my_prog.pl. If you have a larger project, you can use a more sophisticated directory structure:
some_dir/
my_prog.pl
MyApp/
MyDBIDatabase.pm
Users.pm
1) Create the subdirectory MyApp. 2) cd into the MyApp directory, and create the file MyDBIDatabase.pm (can be any name with a .pm extension). Then put this code in MyDBIDatabase.pm:
3) Create another file called Users.pm (also in the MyApp directory, can be any name with a .pm extension). Put this code in Users.pm:
4) Change directories back to the higher directory from which you came( cd ..). 5) Create a program file called my_prog.pl (can be any name), and put this code in it:
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||