3 Replies - 150 Views - Last Post: 08 July 2019 - 08:04 PM

#1 agpz2397   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-July 19

How to fix Insert button in Android Studio Application

Posted 08 July 2019 - 01:37 AM

I am trying to build an application using SQLite using the Model-View-Controller architecture. My app has a Container class, a Score class, and a MainActivity class. The only "error" that shows in Android Studio is that setPoint (in the Score class) is not used. The application runs but when I click the Insert, Delete, or Update buttons it says the table already exists when it does not.

I have tried switching my code around and fixing any errors that pop up but nothing fixes those specific buttons.

Container class
package edu.csustan.cs4950.sqlwithmenu;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

//import java.lang.reflect.Array;
import java.util.ArrayList;

public class Container extends SQLiteOpenHelper {
    private static final String DB_NAME = "myDB";
    private static final int DB_VERSION = 1;
    private static final String TABLE = "score";
    private static final String ID = "id";
    private static final String NAME = "name";
    private static final String POINT = "point";

    public Container(Context context) {

        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {


    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

    public String update(int id, float point) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "update " + TABLE +
                " set " + POINT + " = " + point +
                " where id = " + id;
        Log.i("Container", query);
        db.execSQL(query);

        return "updated";
    }

    public String insert(String name, float point) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "insert into " + TABLE +
                " values (null,'" + name + "', " + point + ")";
        Log.i("Container", query);
        db.execSQL(query);

        return "inserted";
    }

    public String delete(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "delete from " + TABLE +
                " where id = " + id;
        Log.i("Container", query);
        db.execSQL(query);


        return "deleted";
    }

    public boolean existTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "select distinct tbl_name from sqlite_master" +
                " where tbl_name ='" + TABLE + "'";
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.getCount() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public String createTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "create table if not exists " + TABLE +
                " ( " + ID + " integer primary key autoincrement, " +
                NAME + " text, " +
                POINT + " real)";
        Log.i("Container", query);
        db.execSQL(query);

        return "created table";
    }


    public String deleteTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "drop table if exists " + TABLE;
        Log.i("Container", query);
        db.execSQL(query);

        return "deleted table";
    }


    public Score select(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "select * from " + TABLE +
                " where id =" + id;
        Log.i("Container", query);
        Cursor cursor = db.rawQuery(query, null);
        Score score = null;
        if (cursor.moveToFirst()) {
            int scoreId = cursor.getInt(0);
            String name = cursor.getString(1);
            float point = cursor.getFloat(2);
            score = new Score(scoreId, name, point);
        }
        return score;
    }

    public Score selectByIDLargerThanPoint(int id, float point) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "select * from " + TABLE +
                " where id = " + id + " and " +
                " point > " + point;
        Log.i("Container", query);
        Cursor cursor = db.rawQuery(query, null);
        Score score = null;
        if (cursor.moveToFirst()) {
            int scoreId = cursor.getInt(0);
            String name = cursor.getString(1);
            float columnPoint = cursor.getFloat(2);
            score = new Score(scoreId, name, columnPoint);
        }
        return score;
    }

    public ArrayList<Score> selectLargerThanPoint(float point) {
        SQLiteDatabase db = this.getWritableDatabase();
        ArrayList<Score> scores = new ArrayList<Score>();

        String query = "select * from " + TABLE +
                " where point > " + point;
        Log.i("Container", query);
        Cursor cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            float columnPoint = cursor.getFloat(2);

            scores.add(new Score(id, name, columnPoint));
        }
        return scores;
    }

    public ArrayList<Score> selectAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        ArrayList<Score> scores = new ArrayList<Score>();

        String query = "select * from " + TABLE;
        Log.i("Container", query);
        Cursor cursor = db.rawQuery(query, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            float point = cursor.getFloat(2);


            scores.add(new Score(id, name, point));

        }
        return scores;

    }     





Score class

  package edu.csustan.cs4950.sqlwithmenu;

public class Score {
    private int id;
    private String name;
    private float point;

    public Score(int id, String name, float point) {
        this.id = id;
        this.name = name;

        this.point = point;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPoint() {
        return point;
    }

    public void setPoint(float point) {

        this.point = point;
    }
    public String toString() {

        return id + " " + name + " " + point;
    }
}  
 [/code ]


MainActivity class

[code]
package edu.csustan.cs4950.sqlwithmenu;

public class Score {
    private int id;
    private String name;
    private float point;

    public Score(int id, String name, float point) {
        this.id = id;
        this.name = name;

        this.point = point;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPoint() {
        return point;
    }

    public void setPoint(float point) {

        this.point = point;
    }
    public String toString() {

        return id + " " + name + " " + point;
    }
}





The expected result is being able to Insert names and points for the query table.

Is This A Good Question/Topic? 0
  • +

Replies To: How to fix Insert button in Android Studio Application

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 49
  • View blog
  • Posts: 679
  • Joined: 23-September 15

Re: How to fix Insert button in Android Studio Application

Posted 08 July 2019 - 02:58 AM

Quote

The only "error" that shows in Android Studio is that setPoint (in the Score class) is not used.

This is not the cause that resulting your app crashed.It's just a warning.

Quote

The application runs but when I click the Insert, Delete, or Update buttons it says the table already exists when it does not.

Did you re-install your application after you made some changes on Container class?

This post has been edited by wseng92: 08 July 2019 - 03:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 agpz2397   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-July 19

Re: How to fix Insert button in Android Studio Application

Posted 08 July 2019 - 06:27 PM

Quote

Did you re-install your application after you made some changes on Container class?



Yes, I would re-install the application after making any changes.
Was This Post Helpful? 0
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 49
  • View blog
  • Posts: 679
  • Joined: 23-September 15

Re: How to fix Insert button in Android Studio Application

Posted 08 July 2019 - 08:04 PM

Yeah.

This post has been edited by wseng92: 17 July 2019 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1