Can you tell me error in my codes?

0 sharjeel ashraf · April 19, 2015
MYDBHandler.java

package com.example.fasda.sqllite;

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

public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "product.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "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_PRODUCTNAME + " TEXT " +
                "};";
          db.execSQL(query);

    }

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

    }

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

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

   }

    //Print out the database as a string
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWriteableDatabase();
        String query = "SELECT + FROM " + TABLE_PRODUCTS + " WHERE 1";

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

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname"))!= null){
               dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\a";
            }
        }
        db.close();
        return dbString;
    }

}

activity_Main.xml



   

   

   

   


MainActivity.java

package com.example.fasda.sqllite;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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(){
        Products product = new Products(buckysInput.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

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

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



}

Products.java

package com.example.fasda.sqllite;

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;
    }

}



I am getting three Errors on
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\*" + productName + "*\;" );


and 


 dbString += "\a";


Thanks.

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · April 20, 2015
I know that you should not be using braces in the string you are using to create your table. 

Additionally, you have backslashes followed by semicolons. Please don't use backslashes carelessly like this. If you want the backslash character, type \\. The first backslash usually acts as an indicator for the next character to be treated specially.

I would also debug my code incrementally. First, just focus on getting the tables created. Next, focus on retrieving data from the tables and build your code gradually.

Honestly, I'm not sure exactly what to pick as a starting point. 
0 Nicholas Eason · April 20, 2015
Well, I may not know anything about SQL, but I can sure tell you that in Java 

dbString += "\a";



that wont work.
You probably mean:
dbString.append("\a");


Your error could also be coming from your empty activity_Main xml though, you may want to get that checked out.
  • 1

Java / Android Development

107,307 followers
About

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

Links
Moderators
Bucky Roberts Administrator