3 Handy MySQL/Linux Commands

I have finally thrown in the towel, I don’t think at the moment I am able to complete fully fledged tutorials. I always fail at the last one 😦 As a result I have decided to be doing short posts highlighting some of the functions/commands that I frequently use. The main reason I am posting them is so that I can have a place I can quickly refer as I always end up spending some time looking for some particular command. I have listed them in no particular order

Importing a database from the terminal

Most of the time I deal with MySQL datasets that break the PHP file upload limit. As a result I can’t import the sql file from phpmyadmin. The following command ran in the terminal usually comes in handy

  • mysql -u mysql_user -p database_name < file_to_import.sql

where mysql_user is the mysql username, database_name is the name of the database I am exporting to and file_to_import.sql is the sql to import from. For the above command to work you have to be in the root directory where the .sql file is

Changing file and folder permissions recursively

Most Linux servers are strict on file and folder permissions. If the permissions are not correct then it normally throws error 500. You can use FileZilla to recursively change file and folder permissions but this normally takes a very long time. I ran across the following Linux command which greatly helps

  • sudo find path_to_folder -type f -print -exec chmod 644 {} \;

We are using sudo as changing file permissions is a privileged command. We are using the find command to locate the files then change their permissions. path_to_folder is the location of your files. -type is used to indicate whether we are looking for files or folders, f for files and d for directories/folders. -print informs the system to print out all the files it has found on the terminal. -exec tells the system to execute the command chmod 644 to the files which have been located. The command below works on  for files

  • sudo find path_to_folder -type d -print -exec chmod 755 {} \;

You can change the permissions to some other like 444 or something. I am not going to cover folder and file permissions in this post. A modification of this command that allows us to search for files/folders with particular permissions and change them to some other permission is as follows

  • find file_path -type d -perm 777 -exec chmod 755 {} \;

The above command looks for folders in the specified path with the permission 777 and changes it to 755. This is achieved using the -perm switch. Find is a very useful command, try playing around with it.

Changing File Ownership

File ownership and file permissions are two very different things. Normally I work on a project on localhost then at some stage transfer it to the actual server. On localhost the files usually belong to root or me(the currently logged in user). When I transfer this to the on-line server by default they belong to root. This limits actions the Apache user (www-data) can do. You will try to upload a component(this is for Joomla) and it fails. You can use the chown command to change file ownership

  • sudo chown -R www-data:www-data path_to_project

-R tells the system to recurse through all files and folders. www-data is the default Apache user. The first www-data indicates that the files and folders will be owned by www-data. The www-data after the colon indicates that the files and folders will be in the group www-data. Keep in mind that you can use any other user or group other than www-data. path_to_project is the absolute path to your files.

That will be it for today, I hope the above commands will be helpful. Cheers!



Connecting your J2ME application to the internet – Part Four

In our last tutorial we created a parser but we didn’t use it anywhere in our application. In this tutorial we are going to create the J2ME application and then in the next tutorial which is going to be the final one we will final connect our application to the internet.

You can download the pdf version of this tutorial here Continue reading

Connecting your J2ME application to the internet – Part Three

In this tutorial I will introduce the KXML Parser and use it to parse data from our server.  The parser will use the Person’s class we did in our last tutorial to manipulate the data we will obtain from the server and store it. Let us straight away dive into it. We are still using theJ2ME project we created in the last tutorial. Continue reading

Connecting your J2ME App to the internet: Part two

In the previous tutorial we were able to generate the web services needed so that we can log in to our app hosted on an online server. In this tutorial we are going to create a J2ME parser that will consume the web services. Continue reading

Connecting your J2ME application to the internet – Part One

In the last tutorial I did some code snippets on how to connect your J2ME application to the internet but judging from the response I got I seem to have lost many people. In this tutorial I am going to guide you step by step on how to build an application and connect it to a server. The tutorial will be broken into three parts namely

  1. Building the web services on the server ( I will be using PHP to do this)
  2. Creating a parser in J2ME for the web services generated in part one
  3. Using the parser to fetch and display the data on our application

Without wasting time let us dive into the first part; building the required web services.
Building the web services
A J2ME application can’t consume data directly from the web and so we have to pass data to it in a specified format so that it is able to pass it. Web pages have too much data and the data is normally poorly structured. We have to ways of providing highly structured data

  •  JSON
  • XML – SOAP or Restful web services

I will not go much into the details of the above technologies but if you are really interested there are so many resources online. For this tutorial I will be using user generated XML. JSON is far much easier to generate and consume but I will use that in a later tutorial.
Our application will enable a user to login to an online server; you can add more functionality later. As a result I am going to use Joomla CMS for all the back end stuff as user management has been automated. The assumption is that you know how to install Joomla and configure it. I am going to modify the Joomla user component so that we can make queries to it from our application and it returns data in xml format. After login we will just display the user information.
Step 1
Install Joomla, any version can work but installing the latest version is recommended.  The last J2ME project I did was in Joomla 1.5 so I will use the same code. I have therefore installed Joomla 1.5 on my machine.
Step 2
After you have successfully installed Joomla navigate to the site folder. The site folder looks like the screen shot below

Folder structure

Folder structure

Navigate into the components folder and open the component named com_user. Once inside this folder open controller.php using your favorite editor. I am using Eclipse but any editor works. Extensions in Joomla are built using the MVC (Model-View-Controller) convention.You will find several methods in this class. We are going to introduce one more method; loginPhone(). We will make a call to this method and pass to it a username and password. If the username+password combination matches then we will get an xml feed containing the user details. If they don’t match the xml will return zero as the user id. The code is as shown below
// Check for request forgeries
//JRequest::checkToken(‘request’) or jexit( ‘Invalid Token’ );

/*the lines below are used by joomla to determine where to redirect the user after successful login */
if($return = JRequest::getVar(‘return’, ”, ‘method’, ‘base64’)) {
$return = base64_decode($return);
if(!JURI::isInternal($return)) {
$return = ”;
/*the options array will not be used here. It is normally for the browser and allows the user
to specify options like rememmber me */
$options = array();

$options[‘remember’] = JRequest::getBool(‘remember’, false);
$options[‘return’] = $return;

$credentials = array();
/* JRequest is similar to $_$_REQUEST. We use getVar when we are not sure what format the data we are getting is
Other options are getString and getInt */
$credentials[‘username’] = JRequest::getVar(‘username’, ”);
$credentials[‘password’] = JRequest::getString(‘passwd’, ”);
//create an instance of the database object
/* build our query. Before we try to login the user let us check if he/she is in the sessions table */
$query=”SELECT session_id AS sid,userid FROM #__session WHERE username='”.$credentials[‘username’];
//set the query
/* fetch the result. We are using loadObject since we expect one result. For more information check the Joomla API */
/* using the user id from the session table let us fetch the user details from the user table */
$query=”SELECT myusertype FROM #__users WHERE id={$user->id}”;
/* let us now build our xml. we start by defining the encoding and version */
$returnXml  ='<?xml version=”1.0″ encoding=”iso-8859-1″?>’;
/* the opening tag. You should be very careful with xml, it is very strict */
$returnXml .='<user>’;
//let us now insert our data
$returnXml .='<userid>’.$user->id.'</userid>’;
$returnXml .='<name>’.$user->name.'</name>’;
$returnXml .='<email>’.$user->email.'</email>’;
//close the opening tag
$returnXml .='</user>’;
header(“Content-type: text/xml; charset: iso-8859-1″);
//the lines below are required.
//we echo the xml we created
/* cautionary measure to make sure that the xml is flushed to display */
/* exit makes sure that we are not redirected to the browser */
/* preform the login action since we have determined the user is not in the session table */
$error = $mainframe->login($credentials, $options);


$returnXml  ='<?xml version=”1.0″ encoding=”iso-8859-1”?>’;
$returnXml .='<user>’;
$session =&JFactory::getSession();
$returnXml .='<token>’.$session->getToken().'</token>’;
$returnXml .='<sessid>’.$session->getId().'</sessid>’;
$returnXml .='<userid>’.$user->id.'</userid>’;
$returnXml .='<name>’.$user->name.'</name>’;
$returnXml .='<email>’.$user->email.'</email>’;
$returnXml .='</user>’;
header(“Content-type: text/xml; charset: iso-8859-1″);
//login credentials don’t match
$returnXml  ='<?xml version=”1.0″ encoding=”iso-8859-1”?>’;
$returnXml .='<user>’;
$returnXml .='<userid>0</userid>’;
$returnXml .='</user>’;
header(“Content-type: text/xml; charset: iso-8859-1”);
The above code is not wellformatted; at the end of the tutorials I will upload the entire project on Git.A note about the way we are constructing the sql statements; we are not calling the actual table name but rather prefixing with #__. This is Joomla’s convention and it will be replaced by the database prefix set when we installed Joomla.
Step 3
The above function can be accessed using the browser. In my case I would use a url like

It all depends on the technology you are using so all you need to do is find out how to invoke a method and pass parameters to it. You can download the tutorial in pdf format here
What Next
In the next tutorial we will create a J2ME parser and then make a call to our method with several parameters from the app. Cheers!

J2ME and Lwuit tutorial code now available on Git

Hey guys, as promised though very late the code for the J2ME and LWUIT tutorial is now available for download on Git hub. My user name on Git is kiruik. Follow this link to download. I am currently working on a tutorial which will guide you on how to connect your J2ME application to a server on line.

Using KXML Parser to Fetch Data from a Server in J2ME

This tutorial is not as detailed as the previous ones. It seeks to guide you on how you can connect your J2ME application to an online server or a server on your local machine so that you are able to fetch data dynamically. I have done some code snippets but as I said it is just a simple guide so please do more research by ourself. The steps are as follows

1.    Generating XML on the server side
You can either use RSS feeds or generate your own xml. For most websites we have automated RSS generators. The parsing methods are the same since both of them are basically xml
The code snippet below shows how you can generate your own XML feed
/* the first step is retrieving the data from the database. My assumption is you have a MySQL  resultset stored in the variable $_venue */
$returnXml  = ‘<?xml version=”1.0″ encoding=”iso-8859-1″?>’; //create an xml //file
$returnXml.='<details>’; //the opening tag for your xml file
$returnXml.='<id>’.$_venue->id.'</id>’; //stuff you want to retrieve
$returnXml.='<checkins>’.$_venue->checkins.’ are checked in at this venue</checkins>’;
//you can add more attributes depending on the data you are retrieving
$returnXml.='<reviews>’; //you can do xml nesting.
foreach ($reviewDet as $review) /* $reviewDet is an array of results so we need to loop through it to get each individual field */
$rev_link=’index.php?option=com_eventlist&view=events&layout=venue&device=phone&action=review&rev_id=’. $review->id;
$rev_link = JRoute::_( $rev_link );

$returnXml.='</reviews>’; //close the review
$returnXml.='</item>’;//close item
$returnXml.='</details>’; //close details
header(“Content-type: text/xml; charset: iso-8859-1”); //set xml content type
ob_start(); //start display
echo $returnXml; //echo the data we have appended to $returnXml
ob_flush(); //flush it to the display, not a must but a good precaution
exit;    /* return control to invoking method, ensures that the user is not to the website */

To access this xml all we need to do is pass the relevant url to the invoking method and it will return a properly formatted xml document.
Example in Joomla

The above url depends entirely on how you make calls to functions in your implementation

2.    Configuring KXML Parser in J2ME
This actually is a simple step. Just as we included the LWUIT jar, in order to use KXML parser we just need to include the jar in our resource path and we are done. Search for the KXML jar online, I have a very old version

3.    Consuming XML in J2ME – Creating the parser
This involves two steps
•    Creating an class to use to store our objects
•    The actual parsing
The code snippet below shows how to create the class for holding our data. It is the normal Java class. To create it you select Java class and not a midlet. This class is using the data we created in the first step but without the review part
public class RssItem { //you can change the name to whatever you want
public String id; //declare the variables you are fetching from server side
public String location;
public String checkins;
public RssItem(String theId,String location,String checkins) /*you can add more parameters depending on the data you are fetching */
public RssItem()  //we need the default constructor, you must create it

Now that we have created the class we need to parse the data. The code snippet below shows how this can be achieved

public Vector parse(final String rssUrl) throws Exception {  /* the data is going to be returned as a vector. We pass the url you are retrieving from as a parameter to allow us re-use this method */

final Vector items = new Vector();
genP.progressBar();  /* this is just a method to indicate to the user that something is going on in the background */
Display.getInstance().invokeAndBlock(new Runnable() {  //this is how threading is done in Lwuit

public void run() {
KXmlParser parser = null;  /* create an instance of KXML parser, you also need the objects created below */
HttpConnection conn = null;
InputStream rssStream = null;
InputStreamReader isr = null;
try {
parser = new KXmlParser();
conn = (HttpConnection) Connector.open(rssUrl); //open the connection
conn.setRequestMethod(“GET”); // set the request method. For post we don’t need to parse
conn.setRequestProperty(“User-Agent”, “Profile/MIDP-1.0 Configuration/CLDC-1.0”); /* required by browsers , differentiates between requests from different sources */
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {  /* There are several responses, we need an OK response to proceed . */
String str;
rssStream = conn.openInputStream();
int lenght = (int) conn.getLength();
System.out.println(“lenght of stream: ” + lenght);
if (lenght != -1) {
byte serverData[] = new byte[lenght];
str = new String(serverData);
} else {
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = rssStream.read()) != -1) {
str = new String(bStrm.toByteArray());
byte[] payload = str.getBytes();
ByteArrayInputStream bain = new ByteArrayInputStream(payload);
isr = new InputStreamReader(bain);
//at this point we have retrieved the necessary, now we need to parse it in order to separate the data

parser.require(XmlPullParser.START_TAG, null, “details”);  /* state the opening tag for our xml, needs to match with the one we specified otherwise parsing will fail
while (parser.getEventType() != XmlPullParser.END_TAG) { /* loop to make sure we get to the end of our xml */
String nodeName = parser.getName(); //get the name of the current node

if (nodeName.compareTo(“item”) == 0) { /*comparison necessary if there are several choices for tags */
items.addElement(parseRssItem(parser)); /* we are adding items to our vector. The invoked method returns an RssItem */
// System.out.println(“Item added”);
} else {
} catch (Exception ioe) {
Dialog.show(“Error”, “Error connecting to the internet”, “Exit”, null);
} finally {
try {
if (isr != null) {
if (rssStream != null) {
if (conn != null) {

} catch (IOException i) {


return items;
The method below does the actual data retrieval and adds the data to the RssItem object that we will create. We then append it to our vector. Note that this method is invoked by the above method
RssItem parseRssItem(KXmlParser parser) throws Exception { /* the method returns an RssItem, the parameter is the KXML Parser created in the previous method */
RssItem item = new RssItem(); //create the RssItem we will append data into


while (parser.getEventType() != XmlPullParser.END_TAG) { //make sure we loop through all tags
String nodeName = parser.getName();

if (nodeName.compareTo(“id”) == 0) { /*do a comparison then append to the RssItem as defined in the RssItem class */
item.id = parser.nextText();  /*remember we created a String called id in RssItem, we are accessing it using the . operator */
} else if (nodeName.compareTo(“location”) == 0) {
item.location = parser.nextText();
} else if (nodeName.compareTo(“checkins”) == 0) {
item.checkins = parser.nextText();
} else {
return item;

The steps above have enabled use insert our data from the server side into vector, how do we access them?

4.    Using the Vector in J2ME
To use the data we just need to invoke the method we created, pass a url and it will return a vector. This is demonstrated in the code snippet below

KXmlRssParser parser=new KXmlRssParser(); //this is the parser class we created
Vector detailsVector = null;
try {
detailsVector = parser.parse(url);
/*depending on our result set we can either loop through the vector or just access the data directly using index 0. */
String location = “”;
location = ((RssItem) detailsVector.elementAt(0)).location; /*Cast to RssItem, elementAt(0) if we know the exact location otherwise loop, .location is how we added it in the parser */
//you can retrieve the rest and use it the way you want

} catch (Exception e) {

You can download a pdf version of this tutorial here If you have any questions, suggestions or comments feel free to leave a comment.