Friday, March 15, 2019

Palindrome using Recursion in Python

Recursion is one that calls itself.  Any loop can be represented in recursion.

Take for instance the example I wrote to check whether a word is a palindrome or not.

Palindrome is  a word, number, phrase or other sequence of characters which reads the same backward as forward such as madam, racecar or the number 10801.

See how the two functions are implemented, one using ordinary loop and one using recursion.

You can try the code and see whether the words are palindrome or not.

Monday, February 18, 2019

Integrating all Database Modules Using Python

We have done all CRUD operations of inserting records, reading records from the tables, updating records and finally deleting the records if necessary.  Click on the links if you need to review these blog articles.

The modules are all useless if they are called separately and you check your XAMPP SQL database back and forth  to see if changes have indeed happened.

This is now where we create our user interface by having a menu.  Now you may want to create hierarchical menus like the example below.

You can modify this, so, you may just have one main menu only.

 The import commands above are to link your menu to the CRUD modules you have done previously. So, modify the names if your files are named  differently.

Once the menu is shown, the if . . else  condition executed. So, if I choose 1, the insertSalesPerson module found in the file named connectsqlinsertdynamic will be called. 

Remember, however, that once we are done with one module (example Insert), we make sure that our main menu will appear again until we choose to exit.

Now, it is your turn to create the menu interface to call all CRUD modules of insert, read, update and delete database records.

Updating and Deleting Records From the Database Using Python

I have previously created blog articles how to insert a record into our database and  how to read database records using Python. Now, it is time to update and / or delete records from our database.

When we update, we can ask our user for a record to update, search it in our database and once this record is found, we retrieve it for updating.

The sample Python program code below shows how we can update the age of a certain person using the fname(first name) as our search condition (see highlighted text below).

You can change the age as a user input to make your code more dynamic.

Now, if you need to delete a record, we follow the same process we did with updating but we change our SQL command to enforce record deletion.

In the Python program code below, we ask the user for a first name to locate and delete, once we found the record, the SQL Delete command shall be executed.

You may need to refresh your database table to see effect.

Now, it is your turn to practice these modules on your own database!

Monday, February 11, 2019

Reading From Database Using Python

After inserting the records into our database table, it is only expected that we should read from the table using our Python code.

Our database table EMPLOYEE looks like this:

You can display ALL the entries of your table below:

The fechall() will actually read all the records from your table. Using the loop, each of the record will be displayed.  The row[0], for instance, acts as the variable to hold the value of the first field(using our table above, First_Name), and row[1] will be be for our field Last_Name.

Don't forget that the Python code above is a function. If you don't have a main menu, then, simply call this module by typing, read( ), in your main code.

To search and display a specific record from the table, your Python code should be like this. You can double click to enlarge the photo.

Now, try this on other tables you have!