Thursday, 31 March 2011

Android Retriving Content from perticular loaction in sqlite databse


package com.mibook;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class datbase extends Activity {
private static String DB_PATH = "/mnt/sdcard/mibook/";
    private static String DB_NAME = "pg558.sqlite";
  

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  SQLiteDatabase myDB= null;
  SQLiteDatabase myDB2=null;
  String TableName = "MI_BOOK";
  String TableName2="MI_BOOK";
  int id=0;
  String title="";
  String subject="";
  String Data="";

  /* Create a Database.
  try {
   myDB = this.openOrCreateDatabase("", MODE_PRIVATE, null);

   /* Create a Table in the Database.
   myDB.execSQL("CREATE TABLE IF NOT EXISTS "
     + TableName
     + " (BOOK_ID INT(10),BOOK_TITLE VARCHAR, BOOK_SUBJECT VARCHAR);");
  
   */
       /*Retrieving data from another database*/
  try
  {
  String myPath = DB_PATH + DB_NAME;
   myDB2=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  // Cursor cur = myDB2.rawQuery("SELECT * FROM " + TableName2 + "where BOOK_ID=1" , null);
   Cursor cur = myDB2.rawQuery( "SELECT * FROM MI_BOOK WHERE BOOK_ID=1",null);
  cur.moveToPosition(0);
  id= cur.getInt(0);
  title=cur.getString(1);  
  subject=cur.getString(2);
  cur.close();
} catch(Exception e){}

   myDB2.close();
   System.out.println("OUTPUT FROM SQLITE FILE:"+id+" "+title+" "+subject);
  
   try
   {

  String myPath = "/mnt/sdcard/mibook/m_book";
   myDB2=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
   /* Insert data to a Table*/
   myDB2.execSQL("INSERT INTO "
     + "library"
     + " (book_id, book_title,subject)"
     + " VALUES ("+id+",'"+title+"','"+subject+"');");
   } catch(Exception e){System.out.println("error:"+e);}
  // myDB2.close();
   System.out.println(id+" "+title+" "+subject);
   //retrieve data from database
   Cursor c = myDB2.rawQuery("SELECT * FROM " + "library" , null);
   String data="";
   c.moveToFirst();
   while(c.isAfterLast()==false){
data=data+c.getString(0)+" "+c.getString(1)+" "+c.getString(2);
System.out.println(data);
c.moveToNext();
 
   }
  
   /*Retrieving database
      public String getEntry(long rowIndex){
   String value = "";
         Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_NAME, VALUE}, KEY_NAME + "=" + rowIndex, null, null, null, null);
       int columnIndex = c.getColumnIndex(VALUE);
       int rowsCount = c.getCount();
       if(rowsCount > 0){
               String value = c.getString(columnIndex);
       }

       return value;
}*/
  
 /*
   int Column1 = c.getColumnIndex("Field1");
   int Column2 = c.getColumnIndex("Field2");
   int Column3 = c.getColumnIndex("Field3");
  
   // Check if our result was valid.
   c.moveToFirst();
   if (c != null) {
    // Loop through all Results
    do {
    int id = c.getInt(Column1);
     String title = c.getString(Column2);
     String subject = c.getString(Column3);
     *
     Data =Data +id+"|"+title+"|"+subject+"\n";
    /*}while(c.moveToNext());
   }
   TextView tv = new TextView(this);
   tv.setText(Data);
   setContentView(tv);
  }
  catch(Exception e) {
   Log.e("Error", "Error", e);
  } finally {
   if (myDB != null)
    myDB.close();*/
  }
 }

thanks jack dsilva   mangalore

output:


No comments:

Post a Comment