Android SQLite introduction, Insert, Update, Delete records with SQLite database

SQLite, is a powerful SQL database library.

Two Mechanism gives Data Structured Data Persistent

1)      SQLite

2)      Content Providers

SQLite is a opensource, lightweight, single tier relational DBMS.

Each column in database is not strongly typed. So, type checking isn’t necessary when assigning or

extracting values from each column within a row.

Row and Resultset Mapping in SQLite:

“ContentValues” represents rows in a table, “Content Values” object represents a single table row.

Query in android will return as a “Cursor” objects. Cursors are pointers to the result set within the underlying data.

Following are some of functions in “Cursor” class, which helps navigation within result set.

  • moveToFirst Moves the cursor to the first row in the query result
  •  moveToNext Moves the cursor to the next row
  •  moveToPrevious Moves the cursor to the previous row
  •  getCount Returns the number of rows in the result set
  •  getColumnName Returns the name of the specified column index
  •  getColumnNames Returns a string array of all the column names in the current Cursor
  •  moveToPosition Moves the Cursor to the specified row
  •  getPosition Returns the current Cursor position

Following code snippet, imports some of namespaces (or packages) you need to import before starting database work in android application.

 

import android.content.Context;

import android.database.*;

import android.database.sqlite.*;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

Opening and Creating Database

Use “openOrCreateDatabase” method from the application Context class to

private static final String DATABASE_NAME = “myTestDatabase.db”;

private static final String DATABASE_TABLE = “mainTestTable”;

private static final String DATABASE_CREATE =

“create table ” + DATABASE_TABLE + ” ( _id integer primary key autoincrement,” +

“column_one text not null);”;

SQLiteDatabase myDatabase;

private void createDatabase() {

myDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);

myDatabase.execSQL(DATABASE_CREATE);

}

Once you have created database,

Use the “query” method to execute a query on database; following parameters need to specify in order to execute your query.

  • An optional Boolean that specifies if the result set should contain only unique values.
  • The name of the table to query.
  • An array of strings (column names), that lists the columns to include in the result set.
  •  A ‘‘where’’ clause that defines the rows to be returned. You can include ‘?’ wildcards that will

be replaced by the values passed in through the selection argument parameter.

  •  An array of selection argument strings that will replace the ‘?’s in the where clause.
  •  A ‘‘group by’’ clause that defines how the resulting rows will be grouped.
  •  A ‘‘having’’ filter that defines which row groups to include if you specified a group by clause.
  •  A string that describes the order of the returned rows.
  •  An optional string that defines a limit for the number of returned rows.

SQLiteDatabase class has insert, update and delete methods to perform basic database operations.

Insert Method

To add record in database, use ContentValues object and use its put methods to provide a value for

each column.

ContentValues newValues = new ContentValues();

// Assign values for each row.

newValues.put(COLUMN_NAME, newValue);

[ … Repeat for each column … ]

Now, insert the new row by passing the Content Values object into the insert method called

on the target database — along with the table name

// Insert the row into your table

myDatabase.insert(DATABASE_TABLE, null, newValues);

Update Method

In order to update a row, you have to use update method, with ‘where’ parameter that specified condition for which row(s) data to be update, and table name and new values.

Following code snippet represents idea on update method:

// Define the updated row content.

ContentValues updatedValues = new ContentValues();

// Assign values for each row.

newValues.put(COLUMN_NAME, newValue);

[ … Repeat for each column … ]

String where = KEY_ID + “=” + rowId;

// Update the row with the specified index with the new values.

myDatabase.update(DATABASE_TABLE, newValues, where, null);

Delete Method

To delete a row simply call delete on a database, specifying the table name and a where clause that

returns the rows you want to delete.

String where = KEY_ID + “=” + rowId;

myDatabase.delete(DATABASE_TABLE, where, null);

Beginners guide for Android Development-Setting Environment-Sample Application

You can take a look at android basics, and can start android application development.

Introduction to Android – First step towards Android application development

PREREQUISITE

First of all you must have at least one Android SDK installed in your computer. You can get more idea on setting up the SDK environment, on <a href=’http://developer.android.com/sdk/index.html’>http://developer.android.com/sdk/index.html</a&gt;

You also need one of TOOL for android development, I have choose, Eclipse IDE plug-in.

If you need to install Eclipse, you can download it from this location:

http://www.eclipse.org/downloads/

For SDK installation, you can refer:

http://developer.android.com/sdk/installing.html

Or In short: If have not downloaded any SDK version then

You can launch the Android SDK and AVD Manager, From within Eclipse,

Select Window > Android SDK and AVD Manager.

Here, under, Available Packages option, you can select one of Android SDK version, and click “Install Selected” button.

 

SETTINGS in Eclipse

Following settings you have to make (in Eclipse) before running android application.

In Eclipse IDE, go to

Window- > Android SDK and AVD Manager

And, Under Virtual Devices option, click “New” button and make virtual device that will display and show your application (or widget) inside it.

Second thing is, At least one SDK target set in Eclipse environment.

In Eclipse IDE, go to

Window- > Preference

Under “Android” option, SDK Location setting is there,

Click “Browse” button and select path of android sdk folder. (From where system can find ‘platforms’ folder under which your sdk folder will be there).

For example, I have following folder hierarchy

 android-sdk-windows\platforms\android-4

So, I have to specify path up to “android-sdk-windows”.

SAMPLE ANDROID APPLICATION

Now you are ready to make new Android Application,

File-> New -> Project

Small window will appear, here select “Android Project” option, and click “Next” button.

Second window will display. Asking you application options:

Project Name: firstapp

Under Build target:  one of sdk will be selected.

Package Name: must be specified, we can give, thesmile.android.widget

Now click “Finish” button.

‘Package Explorer’ window inside IDE will show your project (firstapp).

It will contain various folders:

SRC: contains various .java files, inside which you have to write your code.(in this example there will be FirstappActivity.java file)

RES: this resource folder contains folders inside it like ‘layout’, ’values’ , ‘drawable-ldpi’ etc…

drawable-ldpi : folder is for images you wants to use in application

layout : folder contains .xml files, Layouts are same as Forms in window application, it represents screens of your application.

In our example this file will contain following code.

<?xml version=“1.0” encoding=“utf-8”?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:orientation=“vertical”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

>

<TextView

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“@string/hello”

/>

</LinearLayout>

values : folder will have strings.xml file, which stores application level various strings like application name, and small text.

Most important file in your project is, AndroidManifest.xml , which contains application level behavior settings.

There will be <activity> tag, inside <application>, this tag specifies which layout will be loaded at first when you application is launched.

If you focus on following lines,

<activity android:name=“.FirstappActivity”

android:label=“@string/app_name”>

<intent-filter>

<action android:name=“android.intent.action.MAIN” />

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

In Eclipse IDE, go to

Run -> Run

Select, ‘Android Application’ option, and your Virtual device will show home screen in few minutes.

You can go to menu in your ‘virtual device’ and see widget “Firstapp” (for our example), clicking it will show a screen with text, “Hello World, Firstapp activity.”

Android application development – How to start android app development – Help for beginner

For, Android application development,you should know how to start android app development, I will help for beginners.

Android is a software stack(specially design for mobile devices), consists of Operating system, middle-ware and applications.

Or we can say, Android is a combination of three components:

  • A free, open-source operating system for mobile devices
  • An open-source development platform for creating mobile applications
  • Devices, particularly mobile phones, that run the Android operating system and the applications
    created for it

Following are Key concepts that builds Android.

Application framework:enabling reuse and replacement of components
SQLite: is server-less, lightweight,relational, transactional SQL database engine, designed for structured data storage.
Dalvik virtual machine: optimized for mobile devices
Integrated browser: based on the open source WebKit engine
Optimized graphics: powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
Media support: for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
Hardware Support functions : GSM Telephony, Bluetooth, EDGE, 3G, and WiFi ,Camera, GPS, compass, and accelerometer (hardware dependent)
Rich development environment: including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE.

android,mobile,mobile app,app development, android development