0

I am trying to delete all the tables from an SQLite database. The problem is I can't seem to do it via the SQLite command and can't find the files on my computer. I have the following code to insert data into the database.

package com.example.mycoursetimetable;


import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBTools extends SQLiteOpenHelper {

public DBTools(Context applicationContext){

    super(applicationContext, "module.db", null, 1);

}

@Override
public void onCreate(SQLiteDatabase database) {

    String query = "CREATE TABLE modules ( moduleId INTEGER PRIMARY KEY,ModuleCode TEXT PRIMARY KEY, ModuleName TEXT,ModuleType TEXT, DayOfWeek TEXT, StartTime TEXT, EndTime TEXT,Location TEXT,AdditionalInfo Text)";

    database.execSQL(query);

}

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

    String query = "DROP TABLE IF EXISTS modules";

    database.execSQL(query);
    onCreate(database);

}

public void insertModule(HashMap<String, String> queryValues){

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put("ModuleCode", queryValues.get("ModuleCode"));
    values.put("ModuleName", queryValues.get("ModuleName"));
    values.put("ModuleType", queryValues.get("ModuleType"));
    values.put("DayOfWeek", queryValues.get("DayOfWeek"));
    values.put("StartTime", queryValues.get("StartTime"));
    values.put("EndTime", queryValues.get("EndTime"));
    values.put("Location", queryValues.get("Location"));
    values.put("AdditionalInfo", queryValues.get("AdditionalInfo"));

    database.insert("modules", null, values);

    database.close();

}

public int updateModule(HashMap<String, String> queryValues){

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();


    values.put("ModuleCode", queryValues.get("ModuleCode"));
    values.put("ModuleName", queryValues.get("ModuleName"));
    values.put("ModuleType", queryValues.get("ModuleType"));
    values.put("DayOfWeek", queryValues.get("DayOfWeek"));
    values.put("StartTime", queryValues.get("StartTime"));
    values.put("EndTime", queryValues.get("EndTime"));
    values.put("Location", queryValues.get("Location"));
    values.put("AdditionalInfo", queryValues.get("AdditionalInfo"));

    return database.update("modules", values, 
            "moduleId" + " = ?", new String[]  {queryValues.get("moduleId") });

}

public void deleteModule(String id){

    SQLiteDatabase database = this.getWritableDatabase();

    String deleteQuery = "DELETE FROM modules WHERE moduleId='" + id + "'";

    database.execSQL(deleteQuery);

}

public ArrayList<HashMap<String, String>> getAllModules(){

    ArrayList<HashMap<String, String>> moduleArrayList = new  ArrayList<HashMap<String, String>>();

    String selectQuery = "SELECT * FROM modules ORDER BY ModuleCode";

    SQLiteDatabase database = this.getWritableDatabase();

    Cursor cursor = database.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()){

        do{

            HashMap<String, String> moduleMap = new HashMap<String,  String>();

            moduleMap.put("moduleId", cursor.getString(0));
            moduleMap.put("ModuleCode", cursor.getString(1));
            moduleMap.put("ModuleName", cursor.getString(2));
            moduleMap.put("ModuleType", cursor.getString(3));
            moduleMap.put("DayOfWeek", cursor.getString(4));
            moduleMap.put("StartTime", cursor.getString(5));
            moduleMap.put("EndTime", cursor.getString(6));
            moduleMap.put("Location", cursor.getString(7));
    //      moduleMap.put("AdditionalInfo", cursor.getString(8));


            moduleArrayList.add(moduleMap);

        } while(cursor.moveToNext());

    }

    return moduleArrayList;

}

public HashMap<String, String> getModuleInfo(String id){

    HashMap<String, String> moduleMap = new HashMap<String, String>();

    SQLiteDatabase database = this.getReadableDatabase();

    String selectQuery = "SELECT * FROM modules WHERE moduleId='" + id + "'";

    Cursor cursor = database.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()){

        do{

            moduleMap.put("moduleId", cursor.getString(0));
            moduleMap.put("ModuleCode", cursor.getString(1));
            moduleMap.put("ModuleName", cursor.getString(2));
            moduleMap.put("ModuleType", cursor.getString(3));
            moduleMap.put("DayOfWeek", cursor.getString(4));
            moduleMap.put("StartTime", cursor.getString(5));
            moduleMap.put("EndTime", cursor.getString(6));
            moduleMap.put("Location", cursor.getString(7));
        //  moduleMap.put("AdditionalInfo", cursor.getString(8));


        } while(cursor.moveToNext());

    }

    return moduleMap;

}

}

From the SQLite shell, I have tried many times in various ways to select the tables in my database. It keeps saying no such table, but the data is there in my application on startup. Does any one know how I could go about getting rid of these tables??

TIA

Daniel o Keeffe
  • 578
  • 2
  • 10
  • 25
  • If you're sure you don't install the android database explicitly then it will be deleted with the application, so you could simply re-install the app. However if you want to do it during runtime, you have to make sure that it's not being used, then open it and run you `query` command – nstosic Oct 27 '13 at 20:17
  • possible duplicate of [Drop all tables command](http://stackoverflow.com/questions/525512/drop-all-tables-command) – Alfredo Cavalcanti Oct 27 '13 at 20:18
  • Your question has already been answered in: http://stackoverflow.com/a/548297/845443 – Alfredo Cavalcanti Oct 27 '13 at 20:20
  • Well i actually already tried that solution, but the problem still persists. NitroNbg, not exactly sure what that means, do you mean drop the database programatically once, and then remove that code?? I'm using an emulator to test the device at present. – Daniel o Keeffe Oct 27 '13 at 20:24

1 Answers1

1

If you really want to delete the whole database content including its structure (tables), than the easiest way is to drop the database and recreate it.

Drop with context.deleteDatabase(name) and recreate just by opening it with the Db open helper.

The deleteDatabase is very useful, e.g. when testing the create database code.

Dropping and recreating the database ensures that no internal structure or anything is left. It's a clean new start.

jboi
  • 11,324
  • 4
  • 36
  • 43
  • Thanks, but I think I asked the wrong question. context.deleteDatabase(module) gives me a syntax error near context. There is also no database file in file explorer in Eclipse. I think maybe that even though there is no database, the information assigned to the edit and textviews in hte emulator can still retain the data, though it no longer exists. Still doesen't move me an closer to solving the problem:(. I'll edit the questio#n to reflect this. – Daniel o Keeffe Oct 27 '13 at 20:56
  • Just renamed the database and started again, but since you answered the question deserves an accept. – Daniel o Keeffe Oct 27 '13 at 22:12