Tuesday, 16 October 2012

How to convert Mysql database to mysqlite

Many times you would like to use your mysql database in your android application. For this, you would need to convert it to sqlite as Android uses sqlite database.

First save the script below in a file and name it as mysqltosqlite.sh.

#!/bin/bash


if [ "x$1" == "x" ]; then

echo "Usage: $0 "

exit

fi



cat $1


grep -v ' KEY "'


grep -v ' UNIQUE KEY "'


grep -v ' PRIMARY KEY '


sed '/^SET/d'


sed 's/ unsigned / /g'


sed 's/ auto_increment/ primary key autoincrement/g'


sed 's/ smallint([0-9]*) / integer /g'


sed 's/ tinyint([0-9]*) / integer /g'


sed 's/ int([0-9]*) / integer /g'


sed 's/ character set [^ ]* / /g'


sed 's/ enum([^)]*) / varchar(255) /g'


sed 's/ on update [^,]*//g'


sed 's/\\r\\n/\\n/g'


sed 's/\\"/"/g'


perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"'


perl -pe '

if (/^(INSERT.+?)\(/) {

$a=$1;

s/\\'\''/'\'\''/g;

s/\\n/\n/g;

s/\),\(/\);\n$a\(/g;

}

' > $1.sql

cat $1.sql
sqlite3 $1.db > $1.err

ERRORS=`cat $1.err
wc -l`

if [ $ERRORS == 0 ]; then

echo "Conversion completed without error. Output file: $1.db"

rm $1.sql

rm $1.err

else

echo "There were errors during conversion. Please review $1.err and $1.sql for details."

fi

Now take a dump of your database:
mysqldump -u root -p --compatible=ansi --skip-opt generator > dumpfile

And now finally run the conversion :

mysql-to-sqlite.sh dumpfile

If the command executes successfully, you should have a dumpfile.db which can be used with sqlite3.

How to Use your SQlite Database in Android Applications

Most of the tutorials found on the web assumes that you want to create and populate your own SQlite database at run time and use it in your application. But there are cases when you would like to use your own existing SQLite database in android application.

In this article, I will provide you a step by step guide to use your own SQLite database in android applications. The SQlite database should be kept in the "assets" folder of your application and this databse needs to be copied in the system database path of the application. This will enable the application to open and access it normally using the SQLiteDatabase API of android.

Step 1 : Preparing the SQlite Database File

In order to use the SQLite database in Android, some minor modifications are requried. You can use any opensource SQLite Database Browser or sqlite manager  for this purpose.

Add a new table to your database "android_metadata" using the following SQL command:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

Now add a single row in this table with text "en_US" in the android_metadata table.

INSERT INTO "android_metadata" VALUES('en_US')




The next important step is to rename all the primary fields of your table to "_id" . This helps Android bind the id fields of your tables.


Rename Primary Field of your sqlite tables to _id
Renaming of the primary field can be easily done by pressing the "Modify Table" button in SQlite Database Browser and then choosing the field you want to rename.

Once the above steps are done, your database is ready to use in your application.

Step 2: Using the Sqlite Database in Android Application

Copy the sqlite database file in "assets" folder and create a DatabaseHelper class by extending the SQLiteOpenHelper class from the "android.database.sqlite" package.

The DatabaseHelper class should look like this:

package com.da.db;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
//http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
public class DatabaseHelper extends SQLiteOpenHelper{
    
    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
    //YOUR_PACKAGE should be replaced by your package name e.g. com.da

    private static String DB_NAME = "da.db";;

    private SQLiteDatabase myDataBase;

    private final Context myContext;
    private Cursor favCursor = null;
   
    private final static int version = 1;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DatabaseHelper(Context context) {

        super(context, DB_NAME, null, version);
        this.myContext = context;
    }   

  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    public boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }
   
    public void deleteDataBase()
    {
        boolean exists = checkDataBase();
        if(exists)
        {
            //delete
            File file = new File(DB_PATH + DB_NAME);
            file.delete();
        }
    }
  
   
    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    }
   
    public void openDataBase(int mode) throws SQLException{
        
        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, mode);

    }
  
   
    @Override
    public synchronized void close() {

            if(myDataBase != null)
               
                myDataBase.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

        // Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.

}


Now your database is ready for use. Just create an instance of this DataBaseHelper class and call its createDataBase() and openDataBase() methods.

Point to Remember : Make sure you rename the YOUR_PACKAGE in DB_PATH to your application packge e.g. com.da

...
            

DataBaseHelper myDbHelper = new DataBaseHelper();
            myDbHelper = new DataBaseHelper(this);
            try {
             myDbHelper.createDataBase();
            } catch (IOException ioe) {
          throw new Error("Unable to create database");
          }      try {
          myDbHelper.openDataBase();
              }catch(SQLException sqle){
            throw sqle;
      }
       



Sunday, 7 October 2012

What is Dalvik Virtual Machine?

Dalvik is an Open Source Software originally written by Dan Bornstein who named it after the fishing village of Dalvik in Iceland.

Dalvik Virtual Mahcine is a process virtual machine (VM) in Android Operating System. It is a software that runs the applications on Android Devices. Dalvik is used primarily in smartphones and tablets but recently it is also being used in smart Tvs and media streamers.

Programs are written in Java and compiled to java bytecode. They are then converted from Java Virtual Machine .class files to Dalvik compatible .dex (Dalvik Executable)files. The compatc Dalvik Executable format is suitable for systems that are constrained in terms of memory and processor speed.

Salient Features of Dalvik VM

  • Dalvik VM uses a register based architecture instead of stack based architecture used by Java Virtual Machine.
  • VM is trimmed down to limit memory space requirement.
  • The constant pool has been modified to use only 32-bit indices to simplify the interpreter
  • Standard Java bytecode executes 8-bit stack instructions. Local variables must be copied to or from the operand stack by separate instructions. Dalvik instead uses its own 16-bit instruction set that works directly on local variables. The local variable is commonly picked by a 4-bit 'virtual register' field. This lowers Dalvik's instruction count and raises its interpreter speed.
  • Dalvik has the capacity to compress resources that you have in your application there by reducing the final apk size and makes the device run multiple instances of the VM efficiently
Difference between Dalvik VM and Java VM

                                            Dalvik                               JVM
Architecture                          Register                            Stack
OS Support                          Android                            Many
Executables                          APK                                 JAR
Constant Pool                       Per Application                 Per Class
Memory Reqt                       Low                                  High

Licenses

Dalvik is said to be a clean-room implementation rather than a development on top of a standard Java runtime, which would mean it does not inherit copyright-based license restrictions from either the standard-edition or open-source-edition Java runtimes. Dalvik is published under the Apache 2 license.
(Source: wikipedia)



Basic Difference between Android and Linux

Android consists of a kernel based on the Linux kernel 2.6 and Linux Kernel 3.x (Android 4.0 onwards), with middleware, libraries and APIs written in C and application software running on an application framework which includes Java-compatible libraries based on Apache Harmony. Android uses the Dalvik virtual machine with just-in-time compilation to run Dalvik dex-code (Dalvik Executable), which is usually translated from Java bytecode.The main hardware platform for Android is the ARM architecture.


Android System Architecture


Google has made many changes in Android's Linux Kernel. Android does not support the full set of standard GNU libraries making it difficult to port existing Linux applications or libraries to Android.

Linux included the autosleep and wakelocks capabilities in the 3.5 kernel, after many previous attempts at merger. The interfaces are the same but the upstream Linux implementation allows for two different suspend modes: to memory (the traditional suspend that android uses), and to disk (hibernate, as it is known on the desktop).In August 2011, Linus Torvalds said that "eventually Android and Linux would come back to a common kernel, but it will probably not be for four to five years".In December 2011, Greg Kroah-Hartman announced the start of the Android Mainlining Project, which aims to put some Android drivers, patches and features back into the Linux kernel, starting in Linux 3.3. further integration being expected for Linux Kernel 3.4.

Android devices users are not given root access to the operating system due to security reasons. Many sensitive partitions like /system are read only for the device owners. To get root access to the device , one can use security flaws in android but this should be done carefully as the important files may get corrupt. Open Source community uses these flaws to enhance the device capabilities. These can also be used to install malicious software or virus.

Reference: Wikipedia

Saturday, 6 October 2012

Android Application Development - Getting Started

All Android Applications are developed in Java Language. You must know Java in order to be able to develop any android application. 

You need to have the set up of android development environment. You would require the following:

The   Android SDK  is composed of modular packages that you can download separately using the Android  SDK Manager Whenever there is a new release for SDK tools or platforms , you can use the  SDK Manager to quickly download them to your environment. 

Once your environment is ready, you can start creating your apps. Lets develop our first project "Hello World" and learn how to create and run a project/application in Eclipse.

What is Android - An Introduction

Android is a Linux-based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers, developed by Google in conjunction with the Open Handset Alliance.

Just like we have Windows operating system for PCs, Android is an operating system for smartphones and tablets which soon will be extended to other products like cars, TVs etc.

Why Android is so popular?


You go to any mobile shop today and you will find majority of smartphones with Android operating system.

Some of the major reasons for its huge popularity are:

1. Android is Free

Android is and will always be free to use. That is one of the biggest reason for its huge popularity and adoption among the phone manufacturers. Why pay a royalty to any company for using the OS when you can get Android for free? This also made possible for the phone manufacturers to come out with smartphones at low cost and still having high end features. You could see many companies like Micromax, Lava , Spice and many others offering low cost smart phones.

Would you buy a normal feature phone when you can get a smartphone at roughly the same price? Probably, NOT. This has in turn benefitted normal feature phone buyers as well because their prices have come down drastically. A WIN WIN situation for all.

2. Android is Open Source and Open for customization 

You get the full source code of Android without paying any royalty to Google. This enables the phone manufacturers to customize the OS according to their requirements. With many brains working on the system, newer and newer ideas were incorporated, which in turn helped in making Android a preferred choice.

3. Huge number of Mobile Applications

Android Market created an opportunity for millions of application developers around the globe to show their skills and come up with newer applications for Android phones. Its users therefore have a wide variety of applications to choose from and can customise their phones for a personal experience. It has attracted millions of developers around the world as they can now earn by selling their applications on Android Market.

4. Excellent tools for developers

Google has provided complete toolset for developing applications for free. Applications are usually developed in the Java language using the Android Software Development Kit, but other development tools are available, including a Native Development Kit for applications or extensions in C or C++.

Market Share of Android

Here are IDC's figures for worldwide smartphone unit sales and market share in the second quarter of 2012, by operating system.

— Android (Google Inc.) — 104.8 million units, 68.1 percent share (46.9 percent a year earlier)
— iOS (Apple Inc.'s iPhone) — 26.0 million units, 16.9 percent share (18.8 percent a year earlier)
— BlackBerry (Research in Motion Ltd.) — 7.4 million units, 4.8 percent share (11.5 percent a year earlier)
— Symbian (mostly used by Nokia Corp.) — 6.8 million units, 4.4 percent share (16.9 percent a year earlier)
— Windows (Microsoft Corp.) — 5.4 million units, 3.5 percent share (2.3 percent a year earlier)
— Linux — 3.5 million units, 2.3 percent share (3.0 percent a year earlier)
— Others — 0.1 million units, 0.1 percent share (0.5 percent a year earlier) 


Future of Android

 As per market research, Android is here to stay atleast till 2015. With android set to come in TVs, cars and other appliances, there is a huge scope for android usage. 

Cars - Till now cars are provided with multimedia facilities such as GPS, Bluetooth etc. But in future Android, QNX, Linux operating systems will provide additional facilities in cars. Operating system will control all the things such as  nearby garages, air condition, seats suspension, climate control etc. It will also give the information such as the location, details of the accidents that happened nearby and traffic. It will also be possible to know the details of the hotels in the route the car is travelling.

TVs - Televisions are already coming with operating system. Google television is one of the examples. Net can be browsed with Chrome browser that is installed. Applications and multimedia content in Chrome can be utilized as in system. Lenovo and Onida have designed televisions that run with android operating system. All the television channels available in net can be watched. With the new features that will be available. Televisions can be connected to the mobile.

Our life will change drastically in future when most of the products like Car, TV, Refrigerator, Mirror, Ovens will go SMART.
Application developers can start thinking on ideas they would like their home products to have. Lets share our ideas and thoughts about future andriod applications.