Sunday, January 23, 2011

Configuring the MySQL database for use in PRADO Application with Active Records

If you are creating a web application and not using a database, well that's pretty unlikely. PRADO support majority of open source databases including MySQL, SQLite, PostgreSQL. So you have plenty of options to choose from based on your pocket and interest. My personal favourite has always been MySQL and its used most widely (as of yet, though PostgreSQL inspires me a lot) as well. So, here I am demostrating configuring your MySQL database which you intend to use in your application.

First things first, you need to have a database created for your applications first. Refer to MySQL manual if you don't remember the syntax. I always forget that... ;-) :-D. Also, create a user that has exclusive rights for that database. You would use GRANT command in MySQL. Check out here. Also, if you are lazy enough to write the SQL queries, like one of my friend, use phpMyAdmin.

Okay here are my details.
MySQL Server - localhost (I have my MySQL server installed in my local system only)
MySQL Server Port - 3306 (Default Port)
Database Name - sample_db
Database user - test_user
Database Password - test_user$123

We will configuring the database to be used with Active Record configuration. Active Records helps to simplify the database operations in the code drastically. And PRADO provides a healthy set of libraries for using Active Records efficiently. Please refer here for learning more about Active Records.

Lets start with the configuration steps.

First you need to include the database connection details in your application.xml configuration file like this.



<paths>
<using namespace="System.Data.*">
<using namespace="System.Data.ActiveRecord.*">
</paths>

<modules>
<module id="db" class="TActiveRecordConfig" enablecache="True">
<database connectionstring="mysql:host=localhost;dbname=sample_db"
username="test_user" password="test_user$123" />
</module>
</modules>


TActiveRecordConfig class is the ActiveRecord manager. It manages all the DB calls for all the Active Record classes. Learn more about it here.

Okay. So your configuration is done. Congratulations. What you thought it would be longer...

Go try out your configuration setting with any of your Active Record classes.

Need help with that too ? Okay, how about something which you might use soon or later in your application. Lets populate a drop down from the values in a table from the database. Sounds useful and interesting? I already have another tutorial for the same - Populate DropDown from the values in Database with Active Records. check out...

Or you are in too hurry to get things rolling. Then test your configuration by generating a Active Record class from PRADO application shell. That would test it all.

Happy PRADO...

No comments:

Post a Comment