working with SQL lite (49)

0 B K · March 25, 2015
I am new to android app development (and programming)  and in tutorial 49 I am having trouble however it don't give me a error it just says application stooped working with no error being given as to why. If you can see where I am going wrong it would be amazing. 

MainActivity.java
package au.edu.utas.shopping;

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

public class MainActivity extends ActionBarActivity {

    EditText myInput;
    TextView MyText;
    MyDBHandler dbHandler;

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

    public void addButtonClicked(View view){
        Products products =new Products(myInput.getText().toString());
        dbHandler.addProduct(products);
        printDatabase();
  }

    public void deleteButtonClicked(View view){
        String inputText = myInput.getText().toString();
        dbHandler.deleteProduct(inputText);
        printDatabase();
    }

    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        MyText.setText(dbString);
        myInput.setText("");

    }

}


MyDBHandler.java

package au.edu.utas.shopping;

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 = "products.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCNAME = "productname";

    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_PRODUCNAME + " TEXT " +
                ");";
                db.execSQL(query);

    }

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

    public void addProduct(Products product)
    {
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    public void deleteProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCNAME + "=\"" + productName + "\";");

    }

    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String qurey = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
        Cursor c = db.rawQuery(qurey, null);
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname"))!=null){
                dbString += c.getString((c.getColumnIndex("productname")));
                dbString += "\n";
            }
        }
        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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myInput"
        android:layout_marginTop="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:width="300dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        android:id="@+id/addButton"
        android:layout_marginTop="42dp"
        android:onClick="addButtonClicked"
        android:layout_below="@+id/myInput"
        android:layout_alignLeft="@+id/myInput"
        android:layout_alignStart="@+id/myInput"
        android:layout_marginLeft="43dp"
        android:layout_marginStart="43dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:id="@+id/deleteButton"
        android:layout_marginRight="35dp"
        android:layout_marginEnd="35dp"
        android:onClick="deleteButtonClicked"
        android:layout_alignTop="@+id/addButton"
        android:layout_alignRight="@+id/myInput"
        android:layout_alignEnd="@+id/myInput" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/MyText"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/addButton"
        android:layout_centerHorizontal="true" />
</RelativeLayout>


Products.java
package au.edu.utas.shopping;

public class Products
{
    private int _id;
    private String _productname;

    public Products(){

    }

    public Products(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;
    }
}

Thank you for the help :D

Post a Reply

Replies

Oldest  Newest  Rating
+1 l k · March 28, 2015
try to look into MyDBHandler.java:

- in onCreate method put the ',' at the end in line
          COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
- in databaseToString method in the "while" after the "if" 
          add the "c.moveToNext();" 

The source code is: https://www.thenewboston.com/forum/topic.php?id=3767

Thank you.
  • 1

Java / Android Development

107,090 followers
About

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

Links
Moderators
Bucky Roberts Administrator