creating data base openhelper in android is most importent part in android appolication devlopment and when you write the code for an application then you need to create a data base open helper for your application this should be helpful for your app.
write the following code into the main class on your application:
package com.open.handler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SqliteOPenhelperDemoActivity extends Activity implements
OnClickListener {
Button login;
EditText user, pass;
String username, passlock;
DBHandler mydb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (Button) findViewById(R.id.button1);
pass = (EditText) findViewById(R.id.editText2);
user = (EditText) findViewById(R.id.editText1);
login.setOnClickListener(this);
System.out.println("before gng to database");
mydb = new DBHandler(this);
System.out.println("am bck frm database");
}
public void onClick(View v) {
// TODO Auto-generated method stub
username = user.getText().toString();
passlock = pass.getText().toString();
System.out.println("insert values before gng to database");
mydb.insertting_values(username, passlock);
System.out.println("am back fnm database");
startActivity(new Intent(this,Retrievedata.class));
}
}
create batabase Handler for create database:
package com.open.handler;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {
static String name_db = "HelperDataBase";
static int version = 1;
SQLiteDatabase db;
public DBHandler(Context context) {
super(context, name_db, null, version);
// TODO Auto-generated constructor stub
// creating the db
System.out.println("in constructor");
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("in oncreate mtd");
// TODO Auto-generated method stub
// creating table
db.execSQL("create table if not exists Users(Username Text,Password Text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insertting_values(String username, String passlock) {
// TODO Auto-generated method stub
System.out.println("in insert method");
db.execSQL("insert into Users values('" + username + "','" + passlock
+ "')");
}
public ArrayList<String> retrievefunction() {
// TODO Auto-generated method stub
ArrayList<String> alist = new ArrayList<String>();
System.out.println("in retrieve function");
Cursor c = db.rawQuery("select * from Users", null);
c.moveToFirst();
do {
String name = c.getString(c.getColumnIndex("Username"));
String pass = c.getString(c.getColumnIndex("Password"));
alist.add(name);
alist.add(pass);
System.out.println(" adding ");
} while (c.moveToNext());
return alist;
}
}
retreviewing the data from the database:
package com.open.handler;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Retrievedata extends ListActivity{
DBHandler mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mydb=new DBHandler(this);
ArrayList<String> ad=mydb.retrievefunction();
System.out.println("cmg frm data values.....");
ListView lv=getListView();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ad));
}
}
write the following code into the main class on your application:
package com.open.handler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SqliteOPenhelperDemoActivity extends Activity implements
OnClickListener {
Button login;
EditText user, pass;
String username, passlock;
DBHandler mydb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (Button) findViewById(R.id.button1);
pass = (EditText) findViewById(R.id.editText2);
user = (EditText) findViewById(R.id.editText1);
login.setOnClickListener(this);
System.out.println("before gng to database");
mydb = new DBHandler(this);
System.out.println("am bck frm database");
}
public void onClick(View v) {
// TODO Auto-generated method stub
username = user.getText().toString();
passlock = pass.getText().toString();
System.out.println("insert values before gng to database");
mydb.insertting_values(username, passlock);
System.out.println("am back fnm database");
startActivity(new Intent(this,Retrievedata.class));
}
}
create batabase Handler for create database:
package com.open.handler;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {
static String name_db = "HelperDataBase";
static int version = 1;
SQLiteDatabase db;
public DBHandler(Context context) {
super(context, name_db, null, version);
// TODO Auto-generated constructor stub
// creating the db
System.out.println("in constructor");
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("in oncreate mtd");
// TODO Auto-generated method stub
// creating table
db.execSQL("create table if not exists Users(Username Text,Password Text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insertting_values(String username, String passlock) {
// TODO Auto-generated method stub
System.out.println("in insert method");
db.execSQL("insert into Users values('" + username + "','" + passlock
+ "')");
}
public ArrayList<String> retrievefunction() {
// TODO Auto-generated method stub
ArrayList<String> alist = new ArrayList<String>();
System.out.println("in retrieve function");
Cursor c = db.rawQuery("select * from Users", null);
c.moveToFirst();
do {
String name = c.getString(c.getColumnIndex("Username"));
String pass = c.getString(c.getColumnIndex("Password"));
alist.add(name);
alist.add(pass);
System.out.println(" adding ");
} while (c.moveToNext());
return alist;
}
}
retreviewing the data from the database:
package com.open.handler;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Retrievedata extends ListActivity{
DBHandler mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mydb=new DBHandler(this);
ArrayList<String> ad=mydb.retrievefunction();
System.out.println("cmg frm data values.....");
ListView lv=getListView();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ad));
}
}
No comments:
Post a Comment