[source code] Android Development Tutorial - 49 to 54

+32 Bucky Roberts · January 6, 2015

MainActivity.java
package com.thenewboston.sqlitedatabaseexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    EditText buckysInput;
    TextView buckysText;
    MyDBHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buckysInput = (EditText) findViewById(R.id.buckysInput);
        buckysText = (TextView) findViewById(R.id.buckysText);
        dbHandler = new MyDBHandler(this, null, null, 1);
        printDatabase();
    }

    //Add a product to the database
    public void addButtonClicked(View view){
        Product product = new Product(buckysInput.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

    //Delete items
    public void deleteButtonClicked(View view){
        String inputText = buckysInput.getText().toString();
        dbHandler.deleteProduct(inputText);
        printDatabase();
    }

    //Print the database
    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        buckysText.setText(dbString);
        buckysInput.setText("");
    }

}


Product.java
package com.thenewboston.sqlitedatabaseexample;

//Each row in the database can be represented by an object
//Columns will represent the objects properties
public class Product {

    private int _id;
    private String _productname;

    public Product(){
    }

    public Product(String productname){
        this._productname = productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }

}


MyDBHandler.java
package com.thenewboston.sqlitedatabaseexample;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productDB.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    //We need to pass database information along to superclass
    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    //Add a new row to the database
    public void addProduct(Product product){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    //Delete a product from the database
    public void deleteProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
    }

    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

        //Cursor points to a location in your results
        Cursor c = db.rawQuery(query, null);
        //Move to the first row in your results
        c.moveToFirst();

        //Position after the last row means the end of the results
        while (!c.isAfterLast()) {
            if (c.getString(c.getColumnIndex("productname")) != null) {
                dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\n";
            }
            c.moveToNext();
        }
        db.close();
        return dbString;
    }

}


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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <EditText
        android:id="@+id/buckysInput"
        android:width="300dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Add"
        android:id="@+id/addButton"
        android:onClick="addButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38dp"
        android:layout_below="@+id/buckysInput"
        android:layout_alignLeft="@+id/buckysInput"
        android:layout_alignStart="@+id/buckysInput" />

    <Button
        android:text="Delete"
        android:id="@+id/deleteButton"
        android:onClick="deleteButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/addButton"
        android:layout_alignRight="@+id/buckysInput"
        android:layout_alignEnd="@+id/buckysInput" />

    <TextView
        android:id="@+id/buckysText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+2 Tomas Repisky · March 30, 2015
I finally found where I should look for these! :-)
+2 Garth Toland · April 1, 2015
Thank god I found this.
0 Akshat Bhardwaj · April 2, 2015
Where to look for the code of Android Development tutorials?
0 yadvendra singh · June 20, 2015
when running this code this error occurs..


terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
This application has requested the Runtime to terminate it in an unusual way.
0 enam ajmal · June 23, 2015
i have this problem i did not get mobile type interface 
let me show you
0 Dilip Sanghvi · July 15, 2015
Can someone please help me getting the source code for all the tutorials ??
Thank You
0 Adil Shaikh · August 8, 2015
This is unfair! I wrote the code myself after watching the tutorials and it gave me a bunch of errors! But here, what i see is an additional line "c.moveToNext()" which wasn't there in the videos...Please confirm the errors INBETWEEN the videos please!
+4 Rip Van Winkle · August 14, 2015
I learn much better when I type in my code myself and figure out whatever errors might be there. In any case, the code given in the tutorial videos only needed two changes (both in MyDBHandler.java):



  • In databaseToString, add c.moveToNext() to the while loop.

  • In onCreate, add a comma after AUTOINCREMENT.


   
After those two changes, it worked fine for me. (Both of those changes are already reflected in the code posted here, but since it's just a list of two things, this summary might help other people.)
0 Donald Genes · August 14, 2015
Nice one Bukky, God bless you
0 prince khan · August 16, 2015
dear bucky you are so cute and veryyyyyyyyyyyyyy thank for such a usefull android tutorial.

but in this tutorial which is about android database(sqlit)  if two same name entered then it should to show the message that this name is already exist.

Java / Android Development

124,553 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator