SQLite in Android App (in tamil)

854 views

Published on

Dr. Ramkumar Lakshminarayanan article published in Tamil Computer Magazine. The content is about using SQLite in Android Application

Published in: Mobile
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
854
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SQLite in Android App (in tamil)

  1. 1. âv‚άô† «ì†ì£ «ðv! ݇†ó£Œ´ àì¡ âv‚άô†´‹ (SQ Lite) à†ªð£FòŠð†´ (â‹ªð† ªêŒòŠð†´) àœ÷¶. âv‚Î ¬ô† å¼ æŠð¡ «ê£˜v ªñ¡ªð£¼÷£°‹. â™ô£ «ì†ì£«ðvèœ «ð£¡Á âv‚Î C¡ªì‚v ñŸÁ‹ ®ó£¡v Ý‚ê¬ù ÝîK‚°‹. âv‚Î ¬ô† ªêò™ð´õ 250 «èH G¬ùõè‹ «ð£¶ñ£ù¶. âv‚άô† ðô Mîñ£ù «ì†ì£ õ¬èè¬÷ ÝîK‚Aø¶. Text, Intergers ñŸÁ‹ Real «ì†ì£ õ¬èèO™ ñFŠ¹è¬÷ «êI‚ èô£‹. ñŸø õ¬èèO™ ñFŠ¹è¬÷ «êI‚è ô£‹. Ýù£™ ÜõŸ¬ø Text, Integers ñŸÁ‹ Real õ¬èè÷£è ñ£ŸP «êI‚è «õ‡´‹. â™ô£ ݇†ó£Œ´ ªñ£¬ð™ ®¬õvèO ½‹ âv‚άô† GÁõŠð†®¼‚°‹. «ì†ì£ «ðC¬ù ݇†ó£Œ´ ÜŠO«èêQ™ ðò¡ 𴈶‹«ð£¶ Üî¡ ªêò™ð£´ ñ†´Šðì õ£ŒŠ¹œ÷¶. ݇†ó£Œ´ ÜŠO«èêQ™ «ì†ì£«ðC¬ù ðò¡ð´ˆ¶‹«ð£¶ H¡ ùEJ™ ªêò™ð´ñ£Á ðò¡ð´ˆî «õ‡ ´‹. Þî¬ù AsyncTask ðò¡ð´ˆ¶õî¡ Íô‹ ꣈FòŠð´ˆîô£‹. ÜŠO«èê¡ ãî£õ¶ «ì†ì£«ðC¬ù à¼õ£‚Aù£™ ܶ DATA/data/App-NAME/ database/FILE NAME Enviroment.getDirectory() â¡ø ªñˆî®¬ù ðò¡ð´ˆ¶õî¡ Íô‹ APP-NAME â¡ð¶ â¡ù â¡Á ÜP‰¶ ªè£œ÷ô£‹. «ì†ì£ â¡ð¶ «ì†ì£«ðv GÁõŠð†´œ÷ Path Ý°‹. File Name â¡ð¶  à¼õ£‚A»œ÷ «ì†ì£«ðC¡ ªðò˜ Ý°‹. Enviroment.get DataDirectory() Ýù¶ ªð¼‹ð£½‹ âv® 裘®¬ù«ò (SD Card) «êI‚èŠð´‹ Þìñ£è ªîKM‚°‹. âv‚άô† «ì†ì£«ðC™ à¼õ£‚èŠ ð´‹ îèõ™è¬÷ à¼õ£‚A«ò ÜŠO«èê¡ ñ†´«ñ ðò¡ð´ˆî º®»‹. ñŸø ÜŠO «èê¡èÀ‹ ðò¡ð´ˆ¶‹ º¬øJ™ îèõ™ «êI‚è ContentProvider- J¬ù ðò¡ð´ˆî «õ‡´‹. ÜŠO«èê¡èÀ‚ A¬ì«ò îèõ™ è¬÷ ðA˜õ¬î âv‚άô† ÝîKŠð F™¬ô. ݇†ó£Œ®™ «ì†ì£«ðC¬ù ðò¡ 𴈶õ ݇†ó£Œ´ «ì†ì£«ðv ªî£°Š¹ ðò¡ð´Aø¶. âv‚άô†®Ÿè£ù A÷£vè¬÷(Class)ñ†´«ñðò¡ð´ˆ¶õ ݇†ó£Œ´ «ì†ì£«ðv âv‚άô† ªî£°ŠH¬ù ðò¡ð´ˆîô£‹. ÜŠO«èêQ™ «ì†ì£«ðC¬ù à¼õ£‚° õ‹, ñ£Ÿø‹ ªêŒõ‹ SQLiteOpen Helper êŠ A÷£v (Subclass) ðò¡ð´Aø¶. getReadable Database() ñŸÁ‹ getWritableDatabase() ªñˆî´è¬÷ SQLiteOpenHelper êŠ A÷£CQ™ ðò¡ð´ˆ ¶õ¶ Íô‹ «ì†ì£«ðC¬ù read ñŸÁ‹ write º¬øJ™ ܬñˆFì º®»‹. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/searchText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/searchButton" android:text="Search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="search"/> </LinearLayout> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/r es/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8px"> <TextView android:id="@+id/firstName" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/lastName" android:layout_marginLeft="6px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/firstName"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/firstName"/> </RelativeLayout> Gó™-1 Gó™-2
  2. 2. H¬óñK WJ¬ù ðò¡ð´ˆ¶õ ༠õ£‚èŠð´‹ «ìHO™ ä® (Id) ñFŠ¹ ðò¡ ð´ˆîŠð´Aø¶. ݇†ó£Œ®™ âv‚άô† ðò¡ð´ˆ¶õ  âv‚άô† «ì†ì£«ðv â¡ð¶ Ü®Šð¬ì A÷£v Ý°‹. âv‚άô† «ì†ì£ «ðC™ insert(), update(),delete() ñŸ Á‹ execSQL () ªñˆî´èœ ðò¡ð´A¡ øù. execSQL() Íô‹ «î¬õò£ù °õK J¬ù (Query) ªêò™ð´ˆî º®»‹. contentvalues â¡ø ÝŠªü‚† Íô‹ W ñŸÁ‹ ÜîŸè£ù ñFŠH¬ù ªè£´‚è º®»‹. W/«õ™Îv ñFŠ¹è¬÷ ޡꘆ ñŸÁ‹ÜŠ«ì†ªêò™èO™ðò¡ð´ˆî º®»‹. rawQuery() ªñˆî† Íô‹ âv ‚Îâ™ v«ì† ªñ¡† ñŸÁ‹ ÜŠO «èê¡ ªêò™ð´‹«ð£¶ ñFŠ¹è¬÷ ªè£´‚°‹ º¬øJ™ (Dynamic Data) ªè£´‚è º®»‹. rawQuery() ðò¡ð´ˆ¶õ¬î ꣡ ø£è 𣘊«ð£‹. Cursor Cursor=getReadableDatabase().rawQuerry ("select*from to do where-id=?",newstring[]{id}) Ü´ˆîî£è Query() ªñˆî† ªè£´‚°‹ «ð£¶ â¡ù ñFŠ¹è¬÷ ªè£´‚è «õ‡´‹ â¡Á 𣘊«ð£‹. stringdbname-°õK ðò¡ð´ˆ¶‹ «ìHO¡ ªðò˜. int[] column Names- â‰î è£ô‹è¬÷ (Column) ðò¡ð´ˆî «ð£A«ø£‹ â¡ðî¬ù °PŠð null ðò¡ð´ˆFù£™ â™ô£ è£ô‹ è¬÷»‹ ªðø º®»‹. String Where Clause- â¡ù îèõ™è¬÷ ªðø «õ‡´‹ â¡ðî¬ù °P‚è ðò¡ð´ Aø¶. String[]groupby - String[]having String[]orderby «ì†ì£«ðv °õKJ¬ù ðò¡ð´ˆ¶‹ º¬øJ¬ù ꣡ø£è 𣘊«ð£‹. returndatabase.Query(DATABASE-TABLE, new string[] {KEY-ROWID, KEY-CATEGORY, KEY-SUMMARY, KEY-DESCRIPTION}, null, null, null, null, null); °õK ªêò™ð†ì¾ì¡ îèõ™è¬÷ è˜ê˜ õ®M™ ªðŸÁ . è˜êK™ àœ÷ îèõ™ èO¡ â‡E‚¬è¬ò ÜPõ getCount() ªñˆî† ðò¡ð´Aø¶. Þî¬ù îMó è˜êK™ moveToFirst() ñŸÁ‹ moveTo Next() ªñˆ î†èœ ðò¡ð´ˆî º®»‹. IS AfterLast() ªñˆî† Íô‹ îèõ™ 㶋 àœ÷î£ â¡ð î¬ù ÜPò º®»‹. Lv† MÎM™ (List view) îèõ™è¬÷ ªðÁ õ C‹Hœ è˜ê˜ Ü죊 ìK¬ù (Simple Cursor Adapter) è˜êK™ ðò¡ð´ˆîô£‹. âv‚Î ¬ô† ðò¡ð´ˆF ꣡Á å¡ P¬ù 𣘊«ð£‹. îèõ™è¬÷ «ì†ì£«ðC™ «êIŠð¶ âš õ£Á â¡Á‹, «êIˆî îèõ¬ô F¬óJ™ «î´õîŸè£ù Gó™è¬÷ »‹ ñŸÁ‹ èñ£‡† ¬ô¡ Ç™ ðò¡ð´ˆF îèõ™è¬÷ 𣘬õ Jì º®»‹. ¹Fî£è Hó£ªü‚† å¡P¬ù ªî£ìƒA Üî¬ìò ªðòK¬ù EmployeeDirectory3 â¡Á‹, Build Target- J¬ù ݇†ó£Œ´ 2.2 â¡Á‹ «ð‚ «èx ªðòK¬ù Simple Employee Directory â¡ Á‹ Ý‚®M†® ªðòK¬ù employee list â¡ Á‹ ªè£´‚è «õ‡´‹. Main.xml-L™ hQò˜ «ôܾ† àì¡ â®† ªì‚v† ñŸÁ‹ ð†ì¡ ðò¡ð´ˆ¶ «õ£‹. Main.xml-Ÿè£ù F¬ó ðì‹ 1 ñŸÁ‹ Gó™ 1-™ ªè£´‚èŠð†´œ÷¶. ⮆ ªì‚v†®™ ªè£´‚°‹ ªðòK¬ù «î´‹«ð£¶ M¬ì Lv†®™ A¬ì‚°‹. Lv†®¡ ܬñŠH¬ù employee-list-item. xml â¡ø ç¬ðL™ ªè£´‚èô£‹. employee- list-item.xml-¡ Gó™ 2 «ð£¡Á Þ¼‚°‹. «ì†ì£«ðC¬ù ðò¡ð´ˆ¶õ Data baseHelper.java â¡ø A÷£v ç¬ðL¬ù ༠õ£‚°«õ£‹. Gó™ -3 Íô‹ employee-directory â¡ø «ì†ì£«ðv à¼õ£‚èŠð´ Aø¶. H¡ ù˜ -id,Firstname, lastname, title, office phone, cellphone, email ñŸÁ‹ manage Id çd™´è¬÷ ªè£‡´ Employee «ìHœ à¼õ£‚èŠð´‹. db.exeSQL(SQL); Íô‹ âv‚Î♽‚° ãŸø ªêò™ ï¬ìªðÁ‹. ContentValues Values =new ContentValues() Valu es â¡ð¶ îèõ¬ô «ìH O™ ¹°ˆ¶õ ðò¡ ð´Aø¶. values.put("firstname", Ramkumar"), values.put("lastname", "lakshminarayanan"); Ý¡†ó£Œ´ -2 Þ󣋰ñ£˜ ªô†²Iï£ó£òí¡ 10 ðì‹-1 ðì‹-2
  3. 3. package samples.employeedirectory; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "employee_directory"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { /* * Create the employee table and populate it with sample data. */ String sql = "CREATE TABLE IF NOT EXISTS employee (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "firstName TEXT, " + "lastName TEXT, " + "title TEXT, " + "officePhone TEXT, " + "cellPhone TEXT, " + "email TEXT, " + "managerId INTEGER)"; db.execSQL(sql); ContentValues values = new ContentValues(); values.put("firstName", "Ramkumar"); values.put("lastName", "Lakshminarayanan"); values.put("title", "CEO"); values.put("officePhone", "617-219-2001"); values.put("cellPhone", "617-456-7890"); values.put("email", "ramkumar@email.com"); db.insert("employee", "lastName", values); values.put("firstName", "Sidarthan"); values.put("lastName", "Swaminathan"); values.put("title", "VP Engineering"); values.put("officePhone", "617-219-3333"); values.put("cellPhone", "781-444-2222"); values.put("email", "sid@email.com"); values.put("managerId", "1"); db.insert("employee", "lastName", values); values.put("firstName", "Mathivannan"); values.put("lastName", "N"); values.put("title", "VP Sales"); values.put("officePhone", "617-219-2002"); values.put("cellPhone", "987-654-3210"); values.put("email", "mathi@email.com"); values.put("managerId", "1"); db.insert("employee", "lastName", values); values.put("firstName", "Ramarajan"); values.put("lastName", "Palaniappan"); values.put("title", "VP Marketing"); values.put("officePhone", "617-219-2003"); values.put("cellPhone", "987-654-7777"); values.put("email", "ljordan@email.com"); values.put("managerId", "2"); db.insert("employee", "lastName", values); values.put("firstName", "Christophe"); values.put("lastName", "Jayaraj"); values.put("title", "Evangelist"); values.put("officePhone", "617-219-0000"); values.put("cellPhone", "617-666-7777"); values.put("email", "christ@adobe.com"); values.put("managerId", "2"); db.insert("employee", "lastName", values); values.put("firstName", "Chinnasamy"); values.put("lastName", "Murugan"); values.put("title", "Director Engineering"); values.put("officePhone", "617-612-0987"); values.put("cellPhone", "617-123-9876"); values.put("email", "murugan@email.com"); values.put("managerId", "2"); db.insert("employee", "lastName", values); values.put("firstName", "Jaya"); values.put("lastName", "Ramani"); values.put("title", "Lead Architect"); values.put("officePhone", "617-444-1122"); values.put("cellPhone", "617-555-3344"); values.put("email", "mtaylor@email.com"); values.put("managerId", "2"); db.insert("employee", "lastName", values); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS employees"); onCreate(db); } values.put("title","CEO"); values.put("officephone","617-219-2001"); values.put("cellphone","617-456-7890"); values.put("email","ramkumar@email.com"); db.insert("employee", "lastname",values); îèõ™èœ «ìHO™ ¹°ˆîŠð´‹, Þî¬ù «ð£¡Á ñŸø îèõ™èÀ‹ ¹°ˆîŠð†´œ÷¶. ÜŠA«ó† ªñˆî† Íô‹ «ìHœ ®ó£Š ªêŒòŠð´‹. H¡ù˜ ¹Fò «ìHœ à¼õ£‚èŠ ð´‹. Employee List.Java ç¬ð™ Íô‹ «ìHO™ îèõL¬ù «î® F¬óJ™ 裇H‚è «ð£A «ø£‹. db=(newDatabaseHelper(this) getwritableDatabase(); databaseHelper ç¬ðL™ à¼õ£‚Aò «ì†ì£«ðC¬ù Þƒ° ðòùð´ˆî «ð£A «ø£‹. searchText ñŸÁ‹ employeelist -J¬ù main.xml àì¡ Þ¬íŠ¹ ãŸð´ˆî searchText=(EditText)findviewById (R.id.searchText)
  4. 4. package samples.employeedirectory; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class EmployeeList extends Activity { protected EditText searchText; protected SQLiteDatabase db; protected Cursor cursor; protected ListAdapter adapter; protected ListView employeeList; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); db = (new DatabaseHelper(this)).getWritableDatabase(); searchText = (EditText) findViewById (R.id.searchText); employeeList = (ListView) findViewById (R.id.list); } public void search(View view) { // || is the concatenation operation in SQLite cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", new String[]{"%" + searchText.getText().toString() + "%"}); adapter = new SimpleCursorAdapter( this, R.layout.employee_list_item, cursor, new String[] {"firstName", "lastName", "title"}, new int[] {R.id.firstName, R.id.lastName, R.id.title}); employeeList.setAdapter(adapter); } - ªî£ì¼‹ ðì‹-3 ðì‹-4 ðì‹-5 Gó™-4 employeelist=(Listview)findviewById(R.id. list); main.xml ç¬ðL™ ð†ì¬ù à¼õ£‚Aò «ð£¶ android.onclick="search" â¡Á ðò¡ð´ˆF Þ¼‰«î£‹. Search â¡ù ªêò™ðì «õ‡´‹ â¡ðî¬ù employeelist- ™ õ¬óòÁˆ¶ àœ «÷£‹. è˜ê˜ Íô‹ «ì†ì£«ðC™ àœ÷ «ìHœ îèõ™ ªðøŠð´‹. C‹Hœ è˜ê˜ Üì£Šì˜ Íô‹ è˜ê˜ àœ÷ îèõ¬ô ªðŸÁ employee list -¡ Ü죊 ì˜ Ýè ܬñŠð¶ Íô‹  «î´‹ îèõ ½‚è£ù M¬ì F¬óJ™ «î£¡Á‹. (ðì‹2) ݇†ó£Œ´ âv®«èJ¬ù D:androidt â¡ø ç«ð£™ìK™ GÁM àœ«÷¡. âv‚Î ¬ô†®¬ù èñ£‡† ¬ô¡ Ç™ Ýù adh Íô‹ «êI‚èŠð†ì îèõL¬ù ÜPòô£‹. emulatorshell -ä Ü‚êv ªêŒò D:androidt platform-tools â¡ø ç«ð£™ìK™ adb-s emulator-5554 shell â¡Á ªè£´‚è «õ‡´‹. ꣡P¡ Íô‹ à¼õ£‚èŠð†ì «ì†ì£ «ðv datadatasamples.employeedirectory-J™ Þ¼‚°‹. (ðì‹3) Þ‰î ¬ìó‚ìKJ™ SQlite3 employee-dire ctory â¡Á ªè£´ˆî£™ âv‚Î ¬ô†®¬ù ðò¡ð´ˆî º®»‹. ÜŠO«èê¡ Íô‹ ༠õ£‚A»œ÷ «ì†ì£«ðC¡ ªðò˜ employee- directory â¡ð‹. âv‚άô† ®¬ù ªî£ìƒ°õ SQLite3 employee-directory â¡Á ªè£´ˆ , âv‚άô† ªêò™ðì ªî£ìƒ°‹. (ðì‹ 4) SQLite>.header on SQLite>.mode Column â¡Á ªè£´ˆî H¡ ù˜ SQLite>Select*from employees; â¡Á ªè£´ˆî£™ ÜŠO«èê¡ Íô‹ «êIˆî îèõ¬ô F¬óJ™ è£íô£‹. (ðì‹ 5)

×