2

I am creating an application, but when I go to insert some data, it doesn't work. Here is a screenshot of my application, including the error message. Here is my database class:

public class database extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "STUDENT";
    public static final String TABLE_NAME = "TRIP";
    public static final String COL1 = "ID";
    public static final String COL2 = "NAME";
    public static final String COL3 = "CURRENT";
    public static final String COL4 = "DESTINATION";
    public static final String COL5 = "BUDGET";
    public database(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CURRENT TEXT,DESTINATION TEXT,BUDGET INTEGER )");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_NAME);
        onCreate(db);
    }
    public void insertdata(String name,String current,String destination,String budget)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        db.execSQL("insert into TRIP(NAME,CURRENT,DESTINATION,BUDGET)values('" + name + "','" + current + "','" + destination + "',"+Integer.parseInt(budget)+")");
    }
}

Any suggestions on how to solve this problem?

Razor
  • 1,778
  • 4
  • 19
  • 36
  • 1
    Uninstall and reinstall your app. http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Sep 14 '16 at 19:50
  • 1
    If you have taken Current Field Later then you need to uninstall app from device first and then run again app and ii will work – Vickyexpert Sep 15 '16 at 04:10
  • 1
    I think you need to add semicolon at the end of sqlite queries like `db.execSQL("create table if not exists " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CURRENT TEXT,DESTINATION TEXT,BUDGET INTEGER );");` – pz64_ Sep 15 '16 at 05:32
  • Thanks Sir, i just reinstalled my app and it starts working – Sudhanshu Kaul Sep 27 '16 at 18:29

1 Answers1

1

Try making the insertdata function return a long. Replace the insertdata function with this one:

public long insertdata(String name, String current, String destination, String budget) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, name);
    contentValues.put(COL3, current);
    contentValues.put(COL4, destination);
    contentValues.put(COL5, budget);

    return db.insert(TABLE_NAME, null, contentValues);
}

In your activity where you're inserting the data, make sure you call this function like so:

database db = new database(this);

if (db.insertdata("name", "current", "destination", "budget") > 0)
    // do something else here

If you're still experiencing the same problems, uninstall your app and try again. If it's still not working, feel free to comment below.

Razor
  • 1,778
  • 4
  • 19
  • 36