Hi to All, i created one demo in android regarding the database. But while executing it facing some problem. So can any one please help me. Here is the code that i developed.
package com.android;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.*;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class DataBaseWork extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String MY_DATABASE_NAME="TradeMarket";
String MY_DATABASE_TABLE="shares";
String data="DataBase";
SQLiteDatabase myDB=null;
try{
myDB=this.openOrCreateDatabase(MY_DATABASE_NAME,MODE_PRIVATE, null);
myDB.execSQL("CREATE TABLE IF NOT EXIST" + MY_DATABASE_TABLE + "(LastName VARCHAR,FirstName VARCHAR,country VARCHAR,age INT(3));");
/* Code to insert data in to DATABSE*/
myDB.execSQL("INSERT INTO" + MY_DATABASE_TABLE + "(LastName,FirstName,country,age)"+ "VALUES('JOSHI','ANUP','INDIA',24);");
myDB.execSQL("INSERT INTO" + MY_DATABASE_TABLE + "(LastName,FirstName,country,age)"+ "VALUES('JALKOTKAR','SACHIN','FRANCE',25);");
/*CODE TO FETCH DATA FROM DATABASE */
Cursor c=myDB.rawQuery("SELECT * FROM" + MY_DATABASE_TABLE ,null);
int LastNameIndex=c.getColumnIndex("LastName");
int FirstNameIndex=c.getColumnIndex("FirstName");
int CountryIndex=c.getColumnIndex("country");
int AgeIndex=c.getColumnIndex("age");
c.moveToFirst();
if(c!=null){
do{
String FirstName=c.getString(FirstNameIndex);
String LastName=c.getString(LastNameIndex);
String country=c.getString(CountryIndex);
int Age=c.getInt(AgeIndex);
data=data +FirstName + "/" + LastName + "/" + country + "/" + Age + "\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();
}
}//end of onCreate method.
}// end of DataBaseWork.