Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Update and delete in sqlite

Posted on November 11, 2022 By

Solution 1 :

You have to call close function on your database after updation/deletion to commit the changes.

db.close()

Problem :

i have been working on this app which uses sqlite, and i want to update and delete rows when the function is called, the primary key is passed as a parameter in the function. But when the function is called no updates are seen in the table. I am unable to identify where I am going wrong and whats the error, please help.

//create table

 @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE registerJob (JID INTEGER PRIMARY KEY AUTOINCREMENT, JobProfile VARCHAR, Organisation VARCHAR, Location VARCHAR, AboutOrg VARCHAR, AboutJob VARCHAR, Incentives VARCHAR , Salary INTERGER, Number INTEGER, Category Varchar, Duration number,Twitter Varchar, Facebook Varchar, LinkedIn Varchar)");
}

//update vacancy
    public void updateVacancy(String orgName,String aboutOrg, String jp, String l, String aoj, int s, String i, int n, int jid) {
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("JobProfile", jp);
            contentValues.put("Organisation", orgName);
            contentValues.put("Location", l);
            contentValues.put("AboutOrg", aboutOrg);
            contentValues.put("AboutJob", aoj);
            contentValues.put("Incentives", i);
            contentValues.put("Salary", s);
            contentValues.put("Number", n);
            db.update("registerJob",contentValues," JID= "+jid,null);
            Toast.makeText(contexto, "Data updated", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Toast.makeText(contexto, "DATABASE HELPER: Update vacancy "+e, Toast.LENGTH_SHORT).show();

        }
    }

    //delete vacancy
    public void deleteJob(int jid) {
        try{
            SQLiteDatabase db = this.getWritableDatabase();
            db.delete("registerJob"," JID= "+jid,null);

        }catch (Exception e){
            Toast.makeText(contexto,"DATABASE HELPER deleteJob "+e,Toast.LENGTH_SHORT).show();

        }
    }

Comments

Comment posted by Gautam

what error are you getting? and how are you checking if an update is a success or fail?

Comment posted by Payal Soni

I am getting no error message, its just that the data is not getting updated or deleted

Comment posted by Payal Soni

and to check the success of update I have an activity to display the updated data

READ  [FIXED] java - Fragment inflater says cannot resolve symbol
Powered by Inline Related Posts

Comment posted by Gautam

the code is correct now you have to debug where it’s failing. It might be the schema or the data which can cause an error. Try running test command like db.execSQL(“UPDATE registerJob SET JobProfile =’testing’ WHERE id=1”) and check if this is reflected or not.

Comment posted by Alpha 1

I can’t see any issue in the code. Are you getting any exception? Can you please check if table has data before updation or deletion?

Comment posted by Payal Soni

There are no exceptions also the data is getting saved in the table.

Comment posted by Alpha 1

It’s weird. No idea why it is happening? Try calling this.close() instead of db.close(). this.close(). Is is possible for you to share the code on GitHub or through any other means?

Android Tags:android, android-studio, java

Post navigation

Previous Post: [FIXED] java – android studio 4.0 cannot load jdk class
Next Post: [FIXED] java – How can ViewParent be cast into View?

Related Posts

[FIXED] android – Use regular search Google api with Retrofit Android
[FIXED] android – Admob Native Ad Issue Android
[FIXED] android – I have two radio buttons where single selection is to be performed.But and on single row both radio buttons are getting selected Android
[FIXED] Multiline editing in Android Studio Android
[FIXED] MAUI WebView problem with rendering AngularJS Android
[FIXED] android – Flutter APK Build Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme