Sunday, August 4, 2013

Android SQLite Database for Insert and view the values

No comments :
Hi,

This is an Example for SQLITE Database n Android

1.Create the Database Adapter class

2.Using the Insert and View function in Android

DBAdapter.java:

package com.example.dbdemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.EditText;

public class DBdatadapter{

static final String SCAN_NAME="scantext";

static final String TAG="msg";
static final String DATABASE_NAME="Hungry";
static final String TBL_SCAN="scantext_tbl";
static final String KEYUSERID="userid";


static final String DATABASE_CREATE="create table scantext_tbl(userid integer primary key autoincrement,scantext text not null);";


final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBdatadapter(Context ctx)
{
this.context=ctx;
DBHelper=new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);

}
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);

}
catch(Exception e)
{
e.printStackTrace();
}
}

public void onUpgrade(SQLiteDatabase db,int oldversion,int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldversion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS scantext_tbl");
onCreate(db);
}
}
public DBdatadapter open()throws SQLException
{
db=DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long textinsert(String texts)
{
ContentValues cv=new ContentValues();
cv.put(SCAN_NAME, texts);
return db.insert(TBL_SCAN, null, cv);
}


public Cursor fetchUser(String scantext)
{
Cursor myCursor=db.query(true,TBL_SCAN, new String[] {KEYUSERID}, SCAN_NAME+"='"+scantext+"'",null,null,null,null,null);
if(myCursor!=null)
{
myCursor.moveToFirst();
}
return myCursor;
}
public int selecttext()
{
Cursor c;
c=db.rawQuery("SELECT * FROM scantext_tbl", null);
return c.getCount();

}

}


MainActivtiy.java:


package com.example.dbdemo;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
DBdatadapter db;
final Context context=this;
Button insert,view;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
insert=(Button)findViewById(R.id.button1);
view=(Button)findViewById(R.id.button2);
text=(EditText)findViewById(R.id.editText1);

db=new DBdatadapter(context);
db.open();
insert.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(text.getText().length()>0)
db.textinsert(text.getText().toString());
}
});
view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "total record= "+db.selecttext(), Toast.LENGTH_LONG).show();
}
});


}



}

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="21dp"
android:layout_marginTop="20dp"
android:text="Insert" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="View" />

</RelativeLayout>

No comments :

Post a Comment