Saturday, May 5, 2012

Ubuntu Cmake issue with mysql c++ connector

I have encountered an error while i try to follow the installation instructions on this page for compiling mysql c++ client. One of the errors was the misleading info in download link of the page . It direct you to linux generic code page and if you download the linux generic code you are dhoomed (you will get bunch of libraries and nothing to compile).

So first you have to download the source code for the connector ( not the linux generic code ).

Then i encountered this weird problem when i try to compile things.


-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/cc
-- Check for working CXX compiler: /usr/bin/cc -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- ENV{MYSQL_DIR} = 
CMake Error at FindMySQL.cm:165 (MESSAGE):
  mysql_config wasn't found, -DMYSQL_CONFIG_EXECUTABLE=...
Call Stack (most recent call first):
  CMakeLists.txt:55 (INCLUDE)


CMake Error at FindMySQL.cm:167 (MESSAGE):
  Cannot find MySQL.  Include dir: MYSQL_INCLUDE_DIR-NOTFOUND library dir:
  cxxflags:
Call Stack (most recent call first):
  CMakeLists.txt:55 (INCLUDE)


-- Configuring incomplete, errors occurred!

Here I can see more than one problems, in tutorial you have no instruction to declare a variable named MYSQL_DIR. But here you need a one so type 

Which mysql

and export it as

export MYSQL_DIR=/usr/bin/mysql

Then for the configuration file missing error you have to install libmysqlclient16-dev library to have mysql_configure file in your system. So type,


sudo apt-get install libmysqlclient16-dev

How to handle octave configure error in package instalation

If you try to install a package in Octave programming language using


pkg install package_name.tar.gz


you might encounter this error which stops the installation of package.

example :

configure: WARNING: no mkoctfile found on path
./configure: line 2918: conftest.cc: command not found
configure: error: Could not run 
the configure script returned the following error: checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for mkoctfile... no
error: called from `pkg>configure_make' in file /usr/share/octave/3.2.4/m/pkg/pkg.m near line 1240, column 2
error: called from:
error:   /usr/share/octave/3.2.4/m/pkg/pkg.m at line 714, column 5
error:   /usr/share/octave/3.2.4/m/pkg/pkg.m at line 287, column 7

For me the reason was i have not installed the octave header files when i m installing octave on my linux (ubuntu 11.10). So installing the headers for my octave version fixed the problem.

In linux terminal type,

sudo apt-get install octave3.2-headers libncurses5-dev

And after the installation finished run octave in super user mood to install the packages.

sudo octave





Thursday, May 3, 2012

How to copy a google chrome cache flash video in linux

I wanted to copy a flash video which is playing in my google chrome browser. In windows this can be achieved using simple tool called video cache viewer. But in linux story is much more different as it is difficult.

So with this article i will explain how to achieve this task.

1. open a terminal and type


ps -ef | grep flash

this will show the process id of you flashplayer (make sure to select the flash process which used by chrome browser)


2. then go to "/proc" directory and go into the folder named by the process id that you have obtained in     step 1. And go into the "fd" folder.


ex: lets assume that the process id of the flash used in chrome is 2424
now you should be inside "~/proc/2424/fd" directory

3. then type


ls -al | grep *flash*


this will show all the flash file descriptors  like below

lrwx------ 1 user user 64 2012-05-02 23:15 21 -> /tmp/FlashXXtD6PiG (deleted) lr-x------ 1 user user 64 2012-05-02 23:15 27 -> /tmp/FlashXXrotLjm (deleted) lrwx------ 1 user user 64 2012-05-02 23:15 3 -> socket:[22151] lrwx------ 1 user user 64 2012-05-02 23:15 4 -> socket:[25121]


Ones that has been highlighted in orange  color are the file descriptors of the flash files that have been opened. Walah!!! we r done copy them to where ever folder you want by simple


cp <file_des_no> destination




Tuesday, May 1, 2012

SQL join and optimization experience

I came across my sql join few days ago. The database i created was a temporary one and it is not for a product or something, this just used to store some web  logs that i used for my final year research.

So what i wanna do was join two tables that has about 100 thousand records in both of the tables. But with my simple inner join query it took more than 10 hours but still did not complete.

So i was searching for the ways to increase the performance of the join query and found out that introducing indexes increase the performance in considerable amount of time.

two things i have done was,


  1. Adding primary key constrains 
  2. Adding foreign key constrains 
to my two tables which i have not add previously because this was just temporary tables.

And then the query resulted within 2 minutes for the same number of records.


Saturday, March 31, 2012

Writing custom cursor adapter to use data from a SQLITE database with AutoCompleteTextView

In this post i am going to discuss how to implement a custom cursor adapter that take the data from a sqlite database and populate the  AutoCompleteTextView on the fly.

First let us write our database code which return a cursor. ( Here i only display the related method to return cursor. You have to write a new class extending  "android.database.sqlite.SQLiteOpenHelper" and write the database initialization and open logic in the class. )



1:  public synchronized Cursor getAutoCompleteCursor(String prefix){  
2:         Cursor c = null;  
3:         if(prefix.compareTo("")==0){  
4:              c = myDataBase.query("dict", new String[]{ID_COLUMN_NAME},   
5:                     ID_COLUMN_NAME+" like '' || ? || '%'",   
6:                     new String[]{"*"},   
7:                     null,   
8:                     null,   
9:                     null);  
10:         }else{  
11:              c = myDataBase.query("tableName",   
12:                        new String[]{COLUMN_NAME},   
13:                     ID_COLUMN_NAME+" like '' || ? || '%'",   
14:                     new String[]{prefix},   
15:                     null,   
16:                     null,   
17:                     null);  
18:         }  
19:         if(c == null & c.getCount() <= 0){  
20:              new Exception("Cursor " + (c==null?"null":" fill with wrong data"));  
21:         }  
22:         return c;  
23:    }  





Here "tableName" is the name of the table and "COLUMN_NAME" is the name of the column in table you want to bind with auto complete view.

My database is a very large one so when query for empty string it returns a large number of rows. This will lead to throw an exception in GPU level (creating a view larger than 40000 rows). So i used a conditional check to verify the empty string and at the initial point i query for a string that is not in database to retrieve a empty cursor and on the second letter enter and onward it will query for the prefix and get suggestion list. Another method i used to overcome this memory issue is to use a threshold in  AutoCompleteTextView so it will start to display suggestions only after the number of characters exceed threshold.

Then we write a class extending CursorAdapter class and implementing the filterable interface to include the view generation logic with the listening to key events in background and query the database to obtain suggestion words.


1:  
2:  import android.content.Context;  
3:  import android.database.Cursor;  
4:  import android.view.LayoutInflater;  
5:  import android.view.View;  
6:  import android.view.ViewGroup;  
7:  import android.widget.AutoCompleteTextView;  
8:  import android.widget.CursorAdapter;  
9:  import android.widget.Filterable;  
10:  import android.widget.TextView;  
11:  public class CustomCursorAdaptor extends CursorAdapter implements Filterable {  
12:       private DatabaseHelper dbHelper;  
13:       private LayoutInflater inflater ;  
14:       public CustomCursorAdaptor(Context context, Cursor c) {  
15:            super(context, c);  
16:            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
17:            this.dbHelper = DatabaseHelper.getDatabaseHelper(context);  
18:       }  
19:       @Override  
20:       public void bindView(View view, Context context, Cursor cursor) {            
21:            String keyword = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ID_COLUMN_NAME));  
22:      TextView tv = (TextView) view.findViewById(R.id.sugestionTextView);  
23:      tv.setText(keyword);  
24:       }  
25:       @Override  
26:       public View newView(Context context, Cursor cursor, ViewGroup parent) {  
27:            return inflater.inflate(R.layout.list_item, null);            
28:       }  
29:     @Override
31: public CharSequence convertToString(Cursor cursor) {
32:  
33:  return cursor.getString(cursor.getColumnIndex(DatabaseHelper.DICT_ID_COLUMN_NAME));
34: }
35:       @Override  
36:       public Cursor runQueryOnBackgroundThread(CharSequence constraint) {  
37:            //return super.runQueryOnBackgroundThread(constraint);  
38:            if(getFilterQueryProvider() != null){  
39:                 return getFilterQueryProvider().runQuery(constraint);  
40:            }  
41:            String filter = "";  
42:            if(constraint != null){  
43:                 filter = constraint.toString();  
44:            }  
45:            return dbHelper.getIdCursor(filter);  
46:       }  
47:  }  

Here at the construct i initialize the LayoutInflater and obtain the reference to my DatabaseHelper class which was written by extending the SQLiteOpenHelper and included the getAutoCompleteCursor method.

We have to override two abstract methods in the class which are newView and bindView.

In newView  write our code to use the inflater and populate a view return it to current cursor position. Here i used this method to populate my custom written "list_item" layout and return a view of that layout.

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:id="@+id/sugestionTextView"  
4:    android:layout_width="fill_parent"  
5:    android:layout_height="fill_parent"  
6:    android:padding="10dp"  
7:    android:textSize="16sp"  
8:    android:textColor="#000">  
9:  </TextView>  

"list_item.xml" file

In bindView we used to populate an existing view that is being reused with data in our cursor. Here i get a string from the cursor, create a text view by finding a sugestionTextView which is located in my "list_item" xml file and set the text view with the string i obtained from the cursor.

In the method runQueryOnBackgroundThreadhas been used to run a background thread each time a new character been entered to AutoCompleteTextView and obtain a cursor with the prefix text that is in the AutoCompleteTextView at that point.

Finally in the Activity class write the following code inside onCreate method to initialize the new CursorAdapter implementation class and set the adapter to AutoCompleteTextView.


1:  autoText = (AutoCompleteTextView) findViewById(R.id.autoCompleteEnglishWordText);  
2:  autoText.setThreshold(2);  
3:  CustomCursorAdaptor cursurAdaptor = new CustomCursorAdaptor(this, null);  
4:  autoText.setAdapter(cursurAdaptor);


Tuesday, March 20, 2012

Python list comprehensions for string sub listing

I am still learning python language which apparently needed for implement some tools regarding my research. And i came through this amazing feature in python lists.
Although i know python can do amazing stuff that no other popular languages can do, this thing suprised me a lot.

So what is this list comprehensions ?
A list comprehension is a way to describe a list. If you have taken mathematics basic class you might be familiar with set theories and for each of set we use we have a definition for that set.

For an example lets say a list that has all the even numbers. We can write this definition in mathematical way like below.

E = { x | x%2=0}
This means set s has all the x values that suppress the condition of "remaining of x by 2 is 0".

When it comes to set of strings, we can define those sets in the same way.

Alphabet = { x | x in 'a' to 'z'} 
V = { a , e , i , o , u }

So in python language it has been facilitated to use the similar notation we used in mathematical definitions.

Say we want to obtain a separate list of items from a main list that has a special string within it.

prefix = "http"
dirListing = os.listdir(sys.argv[1])
httpList = [x for x in dirListing if prefix in x]

Above python sample code demonstrate how to sub list the set of strings that contains "http" within the strings from set of strings.
Here we say that "httpList" list should contain all the x values where x is in "dirListing" list and x contains "dnsString" within it.





Sunday, March 18, 2012

Configure manually Apache HTTPD and PHP in your machine.

There are "WAMP" and "XAMP" softwares that install all three apache httpd web server, php interpreter and mysql server, and automate the configuration process with just few clicks. But just  think of a situation where you have previously installed mysql instance running !


Either you have to uninstall the current instance of mysql server and install one of above "three in one" packs or change the port number of the newly installing my sql server. But what is the point of running two mysql instances at the same machine ???? (adding another service to windows means resources will be eaten more). Even though you uninstall current mysql server and install WAMP or XAMP there might be high chance that you will end up those installations not working due to some conflict with previously installed and now removed mysql instance and the one in WAMP or XAMP.


So it is always better to keep the current mysql instance and configure the other two with the existing one. And after all it is fun! and you can  be proud of your self about what you have done because almost all the php developers depends on wamp and when it comes to the server configuration and deployment environment you cant depend on a WAMP ! its just a development tool (and what if you do you client wants MS SQL server instead of mysql offer in WAMP ? ). So be an adult and do things by your self.


Ok enough talking lets start the work.


First download the latest releases of Apache HTTPd web server, PHP interpreter from the links. (Please download the executable files since that will be helpful with some auto configurations )


Then create a directory called "web" in the location c drive "c:\web". (name of the directory can be anything you like but without spaces, we dont install to "program files" because of the space in file path, try to avoid spaces in file path.)


Installing apache httpd server


Launch the setup (double click on setup.exe) and choose custom. Then choose the directory as "c:\web\apache" and enter the information of your server in to the following window.
( I prefer,
Network Domain - localhost
server name - localhost
Email Address - anything@you.want  If you intend to use the server in local development)


apache-install 
  Click next and choose content to install as showed in below.


apache-install-2 


And finish the setup.


Installing PHP setup.


Launch the setup and when prompt to select a location select the "c:\web\php" as the location.


In next window select the server which u wish to configure the PHP interpreter with shown as below. ( here we choose apacheXXX where XXX is the version of the apache httpd server that you have downloaded and installed previously)


php-install-2 


In next prompt choose the apache configuration directory in our case "c:\web\Apache\conf". 


On next window choose the libraries you wanted to install with PHP.


(Caution!!! some of the modules will not be compatible with the current PHP interpreter version that we are installing this might give us errors when we run the interpreter. If you are a newby most of the time you will miss understand this errors as miss configurations. So its better to leave not installing modules for now since we can add modules by editing "php.ini" manually later.)


Configuring apache server


First you have to add the a map to localhost and your server name if it is not "localhost" that you have entered in the beginning of apache server installation process. To do this open the host file in the "c:\windows\system32\drivers\etc\hosts" location using text editor (Most of the time if you are not in the administrator account you wont be able to save the edited host file. In that case copy the host file to Desktop edit it, save it there and copy back to the location of the original host file by overwriting it).



127.0.0.1 localhost
127.0.0.1 yourDomain


Then we are going to edit apache configuration files. ( keep in mind to use forward slash '/' instead of back slash '\' in file paths ).

(First of all copy and backup the "c:\web\apache\conf\httpd.conf" file in case of things gone wrong)

Open "c:\web\apache\conf\httpd.conf" and search for the lines.

1. DocumentRoot
DocumentRoot "c:/web/Apache/htdocs"

change this to any directory you prefer to use to  store your html and php files. (lets say "c:\web\www").

DocumentRoot "c:/web/www"
2. Then change 

<Directory "c:/web/Apache/htdocs">
to
<Directory "c:/web/www">

3. Add entry for php in the apache configuration file.
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/web/PHP"
LoadModule php5_module "C:/web/PHP/php5apache2_2.dll"
AddType application/x-httpd-php .php
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL

4. Include index.php to apache directory index files.
DirectoryIndex index.html index.htm index.php




Configuring PHP

(First of all copy and backup the "c:\web\php\php.ini " file in case of things gone wrong) 

Edit php.ini file located in "c:\web\php\php.ini".

1. Change the display errors on.
display_errors = Off
to
display_errors = On

2. Copy all the "php5apache2_2.dll" in php root directory and ext directory to "c:\windows\system32" directory with edited php.ini file.


Adding or removing modules to php.ini

Edit php.ini file and add entries for required modules.

Eg:

[PHP_MCRYPT]
extension=php_mcrypt.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_PDO]
extension=php_pdo.dll
[PHP_SOAP]
extension=php_soap.dll
[PHP_SQLITE]
extension=php_sqlite.dll
[PHP_XMLRPC]
extension=php_xmlrpc.dll
[PHP_ZIP]

Adding MySQL support to PHP


Here i assumed that you have already installed mysql server and configured it. Do not store your mysql host, user and password fields in php.ini file even though php mysql configuration allows to store them.

[MySQLi]

; Maximum number of links.  -1 means no limit.
mysqli.max_links = -1

; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysqli.default_port = 3306

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysqli.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysqli.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysqli.default_user =

; Default password for mysqli_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysqli.default_pw")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysqli.default_pw =

; Allow or prevent reconnect
mysqli.reconnect = Off

Testing
 



Write the following php code. Store it as index.php in your webroot location. ( here we use "c:\web\www" ).

<?php phpinfo(); ?>



Then open a browser and type localhost in address bar. You will get a similar page as below.