Jump to content

Android Studio Wifi and SQL problems

Go to solution Solved by colonel_mortis,

db.delete deletes all entries from the table, but it doesn't drop the table itself. This is what it sounds like you need, but your code calls onCreate, which should actually create the table. If your onCreate query doesn't specify CREATE TABLE IF NOT EXISTS then the app will crash when you run it. If you remove the onCreate call it should work though.

brief overview of what I want my app to do, it takes in the previous configured wifi information and saves them to a table. I have everything working apart from these two problems:

Problem 1:
So whenever I try and check if the wifi is enabled on the device, the app will crash.

Here is the code for the Wifi problem:

public void checkWifiState() {
       if(!wifi.isWifiEnabled())    {
           Toast.makeText(getApplicationContext(),
                   "Wifi is disabled, please enable it", Toast.LENGTH_SHORT).show();
           wifi.setWifiEnabled(true);
       }
   }

EDIT: found out that wifi.setWifiEnabled(true); is what is breaking the app for Problem 1.

Problem 2:

I want to drop my table and recreate it, whenever the user trys to press the button to check previous connections. The reason for this is becuase I don't want duplicate data in the database.
here is the code for the SQLite problem:
this is in a seperate java file called DatabaseHelper that extends SQLiteOpenHelper

 public void dropTableAndCreateNew() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME,null,null);
        onCreate(db);
    }

I have tried setting the whereClause to "1" on the delete method. It still crashed when I tried to run it on my phone.

Link to comment
Share on other sites

Link to post
Share on other sites

db.delete deletes all entries from the table, but it doesn't drop the table itself. This is what it sounds like you need, but your code calls onCreate, which should actually create the table. If your onCreate query doesn't specify CREATE TABLE IF NOT EXISTS then the app will crash when you run it. If you remove the onCreate call it should work though.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the advice this fixed my problem with the SQL

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×