2012-06-21 22:46:27 - database] Automatic Target Mode: using device '0123456789ABCDEF'
[2012-06-21 22:46:27 - database] Uploading database.apk onto device '0123456789ABCDEF'
[2012-06-21 22:46:27 - database] Installing database.apk...
[2012-06-21 22:46:30 - database] Success!
[2012-06-21 22:46:30 - database] \database\bin\database.apk installed on device
[2012-06-21 22:46:30 - database] Done!
DatabaseControl.java
- Code: Select all
package example.simpleDatabase.tutorial;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
public class DatabaseControl {
private static final String KEY_ROWID="_id";
private static final String KEY_ITEMTYPE="itemType";
private static final String KEY_QUANTITY="quantity";
private static final String DATABASE_TABLE="inventory";
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public DatabaseControl(Context context){
this.context=context;
}
public DatabaseControl open() throws SQLiteException{
dbHelper=new DatabaseHelper(context);
database =dbHelper.getWritableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public long addItem(String type ,int quantity)
{
ContentValues setUpVals =createContentValues(type,quantity);
return database.insert(DATABASE_TABLE, null, setUpVals);
}
public ContentValues createContentValues(String type, int quantity) {
// TODO Auto-generated method stub
ContentValues values =new ContentValues();
values.put(KEY_ITEMTYPE, type);
values.put(KEY_QUANTITY, type);
return values;
}
public boolean updataItem(long id,String type,int quantity){
ContentValues updateVals =createContentValues(type,quantity);
return database.update(DATABASE_TABLE,updateVals, KEY_ROWID +"="+id,null) >0;
}
public long fetchItemIdByType(String type){
Cursor dbCursor;
long id=0;
try{
dbCursor=database.query(true,DATABASE_TABLE,new String[] {KEY_ROWID},KEY_ITEMTYPE +"='" + type+"'",null,null,null,null,null);
dbCursor.moveToFirst();
id=dbCursor.getLong(dbCursor.getColumnIndex(KEY_ROWID));
}catch(SQLiteException e){
id=-1;
}
return id;
}
public String fetchAllItems(){
String allData= "";
Cursor dbCursor;
try {
dbCursor=database.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_ITEMTYPE, KEY_QUANTITY}, null, null, null,null,null);
int iRow=dbCursor.getColumnIndex(KEY_ROWID);
int iType=dbCursor.getColumnIndex(KEY_ITEMTYPE);
int iQuantity=dbCursor.getColumnIndex(KEY_QUANTITY);
for(dbCursor.moveToFirst(); !dbCursor.isAfterLast();dbCursor.moveToNext()){
allData=allData +" "+dbCursor.getString(iRow)+ "\t" + dbCursor.getString(iType) + "\t\t\t" + dbCursor.getString(iQuantity) + "\n" ;
}
} catch (Exception e) {
// TODO Auto-generated catch block
allData= "";
}
return allData;
}
public boolean deteleItem(long id){
return database.delete(DATABASE_TABLE, KEY_ROWID + "=" +id ,null ) > 0;
}
}
DatabaseHelper.java
- Code: Select all
package example.simpleDatabase.tutorial;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
public class DatabaseHelper extends SQLiteOpenHelper {
/** Called when the activity is first created. */
private static final String DATABASE_NAME="itemInventorydb";
private static final int DATABASE_VERSION=1;
private static final String DATABASE_CREATE="CREATE TABLE inventory ("+
"_id integer PRIMARY KEY AUTOINCREMENT," +
"itemType text NOT NULL,"+
"quanlity integer NOT NULL" +
");";
public DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS inventory");
onCreate(db);
}
}
DatabaseManageActivity.java
- Code: Select all
package example.simpleDatabase.tutorial;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class DatabaseManageActicity extends Activity{
private EditText typeInput;
private EditText qtyInput;
private DatabaseControl dbControl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbControl=new DatabaseControl(this);
typeInput=(EditText) findViewById(R.id.itemType);
qtyInput=(EditText) findViewById(R.id.quantity);
}
public void onClick(View arg){
String typeData=typeInput.getText().toString().toLowerCase();
String qtyData=qtyInput.getText().toString();
Dialog notice=new Dialog(this);
TextView msgBody=new TextView(this);
msgBody.setTextSize(20);
long tempVal=0;
switch(arg.getId()){
case R.id.addItem:
try{
int qtyDataAsNum=Integer.parseInt(qtyData);
dbControl.open();
if((tempVal=dbControl.fetchItemIdByType(typeData)) != -1){
if(dbControl.updataItem(tempVal,typeData,qtyDataAsNum)){
notice.setTitle("Item update");
msgBody.setText("item alreadyt exist,was update instead");
}
else{
notice.setTitle("update failded");
msgBody.setText("Item already existed ,update attempted but fail");
}
}
else{
long rowId=0;
rowId=dbControl.addItem(typeData,qtyDataAsNum);
notice.setTitle("item inserted !");
msgBody.setText("Item inserted at "+rowId);
}
dbControl.close();
}catch(SQLiteException e) {
e.printStackTrace();
notice.setTitle("insert Failed");
msgBody.setText("SQL ERROR");
}
catch(NumberFormatException e) {
e.printStackTrace();
notice.setTitle("insert Failed");
msgBody.setText("Quantity mut be an integer value ");
}
notice.setContentView(msgBody);
notice.show();
break;
case R.id.updateItem:
try {
int qtyDataAsNum=Integer.parseInt(qtyData);
dbControl.open();
if((tempVal=dbControl.fetchItemIdByType(typeData)) !=-1){
if(dbControl.updataItem(tempVal,typeData,qtyDataAsNum)){
notice.setTitle("Item update");
msgBody.setText("item has been update");
}
else{
notice.setTitle("update failded");
msgBody.setText("fail update attempt.No record Found");
}
}else{
notice.setTitle("update failded");
msgBody.setText("Item does not exist");
}
dbControl.close();
}catch(SQLiteException e){
e.printStackTrace();
notice.setTitle("Update failed ");
msgBody.setText("SQL Error");
}
catch(NumberFormatException e){
e.printStackTrace();
notice.setTitle("Update failed ");
msgBody.setText("Quanlity must be an integer value");
}
notice.setContentView(msgBody);
notice.show();
break;
case R.id.deleteItem:
try{
dbControl.open();
if((tempVal=dbControl.fetchItemIdByType(typeData)) !=-1){
if(dbControl.deteleItem(tempVal)){
notice.setTitle("Item deleted");
msgBody.setText("item has been deleted");
}
else{
notice.setTitle("delete failded");
msgBody.setText("delete attempt.No record Found");
}
}else{
notice.setTitle("Delete failded");
msgBody.setText("Item does not exist");
}
dbControl.close();
}catch(SQLiteException e){
e.printStackTrace();
notice.setTitle("Delete failed ");
msgBody.setText("SQL Error");
}
notice.setContentView(msgBody);
notice.show();
break;
case R.id.viewData:
Intent i=new Intent("example.simpleDatabase.tutorial");
startActivity(i);
break;
default:
}
}
}
DatabaseViewer.java
- Code: Select all
package example.simpleDatabase.tutorial;
import android.app.Activity;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.TextView;
public class DatabaseViewer extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dataview);
TextView content =(TextView)findViewById(R.id.dataOutput);
DatabaseControl control=new DatabaseControl(this);
String result="_id\t itemType\t quantity \n";
try{
control.open();
result=result + "" + control.fetchAllItems();
control.close();
}catch(SQLiteException e){
e.printStackTrace();
}
content.setText(result);
}
}
AndroidManifest.xml
- Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.simpleDatabase.tutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".DatabaseManageActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCER" />
</intent-filter>
</activity>
<activity android:name=".DatabaseViewer" android:label="@string/app_name" >
<intent-filter>
<action android:name="example.simpleDatabase.tutorial" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
layout :main.xml
- Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item name" />
<EditText
android:id="@+id/itemType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity" />
<EditText
android:id="@+id/quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/addItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item" />
<Button
android:id="@+id/updateItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Item" />
<Button
android:id="@+id/deleteItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Item" />
<Button
android:id="@+id/viewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Database" />
</LinearLayout>
layout:dataview.xml
- Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/dataOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
/>
</LinearLayout>
anyone know wat is the problem ? hope someone can help me...

