Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:40:25+00:00 2026-06-04T07:40:25+00:00

I want to execute a SQL query in a method which permit to erase

  • 0

I want to execute a SQL query in a method which permit to erase all data in my table. My solution work but I don’t believe that very clean and the jury will appreciate.

Right now, I call a method with date in parameter but I don’t need to use it. It’s the only solution that I have found. When I try to put a void, logically I can’t use this method which uses return.

public long Effacer(String date) {
    return ourDatabase.delete(DATABASE_TABLE, null, null);
}

Well for you what the way is the more clean for drop my table and recreate it. I had create a method une DbHelper but i don’t know after how i can call her. I believe that’s the way the more clean but i’m just a beginner so i’m not sure. What are you thinking ?

(i have let any of my tests in the code)
Here is my code :

HotOrNot.java

public class HotOrNot
{
    public static final String  KEY_ID              = "id_operation";
    public static final String  KEY_MONTANT         = "montant";
    public static final String  KEY_DESCRIPTION     = "description";
    public static final String  KEY_DATE            = "date";

    private static final String DATABASE_NAME       = "MyBudget";
    private static final String DATABASE_TABLE      = "Operations";
    private static final int    DATABASE_VERSION    = 1;

    private DbHelper            ourHelper;
    private final Context       ourContext;
    private static SQLiteDatabase       ourDatabase;

    public static class DbHelper extends SQLiteOpenHelper
    {
        // Constructeur
        public DbHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
                    + " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
                    + KEY_DESCRIPTION + " TEXT NOT NULL);");
        }

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

        public void onDelete()
        {
            ourDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
            ourDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
                    + " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
                    + KEY_DESCRIPTION + " TEXT NOT NULL);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            // TODO Auto-generated method stub

        }

    }
    // constructeur
    public HotOrNot(Context c)
    {
        ourContext = c;
    }

    public HotOrNot open() throws SQLException
    {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        /*ourDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
        ourDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
                + " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
                + KEY_DESCRIPTION + " TEXT NOT NULL);");
                */
        //ourDatabase = ourHelper.

        //ourDatabase = ourHelper.onUpgrade(ourDatabase);*/
        return this;
    }

    public void close()
    {
        ourHelper.close();
    }

    public long createEntry(String date, String montant, String description)
    {
        ContentValues cv = new ContentValues();
        cv.put(KEY_DATE, date);
        cv.put(KEY_MONTANT, montant);
        cv.put(KEY_DESCRIPTION, description);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getData()
    {
        String[] columns = new String[]
        { KEY_DATE, KEY_MONTANT, KEY_DESCRIPTION };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";

        int iDate = c.getColumnIndex(KEY_DATE);
        int iMontant = c.getColumnIndex(KEY_MONTANT);
        int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        {
            result = result + c.getString(iDate) + " " + c.getString(iMontant)
                    + " " + c.getString(iDescription) + "\n";
        }

        return result;
    }

    public String getSolde()
    {
        // Création de notre requete et alias
        String query = "SUM(id_operation) AS " + KEY_ID;
        String[] otherColumns = new String[]{ query };
        Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
        cursorBidon.moveToFirst();
        int iOperation = cursorBidon.getColumnIndex(KEY_ID);
        String monSolde = "";
        monSolde = monSolde + cursorBidon.getString(iOperation);
        return monSolde;
    }

    /*
    public void Erase(SQLiteDatabase db)
    {
        db.execSQL("DROP TABLE " + DATABASE_TABLE);
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
                + " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
                + KEY_DESCRIPTION + " TEXT NOT NULL);");
    }*/

    public long Erase(String date)
    {
        return ourDatabase.delete(DATABASE_TABLE, null, null);
    }

}

SQLiteExample.java (only part when we click on delete button)

        case R.id.bSQLDelete:
            boolean samarche = true;
            try
            {
                String date = sqlDate.getText().toString();
                HotOrNot entryyy = new HotOrNot(SQLiteExample.this);
                entryyy.open();
                //entryyy.Effacer(date);
                entryyy.close();
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:40:27+00:00Added an answer on June 4, 2026 at 7:40 am

    I don’t know why some people have judged me negatively, we cannot say i have do nothing efforts, we can see any of my test in comment. Moreover i’ve a solution which works for my problem, if i had not searched i would never found this way. The problem was just the way to do it and the fact i would like anything of clean, so we can say my post was only created by interest in computing.

    Well, because i’m not like people which critize without help i give you the solution because personally i think to the other.
    This is solution has been found by one people in my company. The solution for call the method onDelete in the class DbHelper is :

    DbHelper dbHelper = new DbHelper(arg0.getContext());
    dbHelper.onDelete();
    

    With Regards,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute a SQL query in mysql something like the below: SELECT
I am using Symfony 1.4 and Propel(ORM), i want to execute SQL query (
I have a sql query which takes longer than 30seconds to execute. I'm aware
All, I need to write a data set from a large SQL table to
Using SQL 2005: Taking too much time to execute I want to filter the
I want to execute a function on all the objects within a List of
I want to execute a script every 30mins, but i want to use it
i want to execute the query in master database it self IF OBJECT_ID(N'DB1.dbo.T_table1', N'U')
Here's the method i want to write: public static IEnumerable<String> GetTableNames(this IQueryable<T> query) {
It looks like #temptables created using dynamic SQL via the EXECUTE string method have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.