4

I try to drop table programmatically nothing error show in logcat, but there has no any result :

Here my Database Helper class :

public class database1 extends SQLiteOpenHelper
{
    private static final String DB_Name="database";
    private static final int DB_Version=1;
    private static final String tbl_arrival1="arrival1";
    private static final String STRING_CREATE1 = "CREATE TABLE IF NOT EXISTS "+tbl_arrival1
            +" (_f_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            +"f_date TEXT,"
            +"o_rder NUMERIC,"
            +"rem_com TEXT);";
    public database1 (Context ctx)
    {
        super(ctx,DB_Name,null,DB_Version);
    }
    @Override
    public void onCreate(SQLiteDatabase database) {
        databaseQuery.onCreate(database);
      }
    public static class databaseQuery 
    {
        public static void onCreate(SQLiteDatabase database)
        {   
            database.execSQL("DROP TABLE IF EXISTS "+tbl_arrival1);
            database.execSQL(STRING_CREATE1);
        }
    }
}

I use this database helper in content provider :

database1 DBHelper ;
public Uri insert(Uri uri, ContentValues values) 
    {
        long row_id;
        SQLiteDatabase sqlDB = DBHelper.getWritableDatabase();
        row_id=sqlDB.insert(tbl_arrival1, null, values);
        if(row_id>0)
        {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, row_id);
            getContext().getContentResolver().notifyChange(_uri, null);
            return _uri;
        }
        throw new SQLException("Failed to insert into "+uri);
    }

The problem is that, the query drop table, is not working!

All suggestion and answer would be highly appreciated...

thanks

Edward Sullen
  • 157
  • 1
  • 5
  • 18

3 Answers3

3

I know I'm bit late but this question was relevant for me. In my case I tried to drop many tables in single a query, i. e.

drop table if exists table_name1;
drop table if exists table_name2;
drop table if exists table_name3;

so android's implementation of execSQL() executes only single sql statement

theroom101
  • 599
  • 1
  • 9
  • 23
  • My hero! I've been searching for a reason my queries wouldn't execute, finally, someone with an answer. You'd think they'd let execSQL throw an exception or something for more than one query. – JJ Du Plessis Aug 07 '19 at 08:49
2

onCreate will work only once at time of creation once the database is created you should make use of onUpgrade() to drop table if any changes has been made to old version

EDIT http://www.vogella.com/articles/AndroidSQLite/article.html

Just Variable
  • 892
  • 10
  • 19
  • can you give me tutorial link about using onUpgrade() ! I've never try with it.. thanks – Edward Sullen Oct 31 '12 at 12:30
  • does not work for me, I tried to drop tables in onUpgrade(), but there was no result, Tables stayed with old data. I checked it by placing break point right after sqlExec() method, then copied database file to my computer and open it in sqlite viewer program. Most of all, I tried to execute same sql queries on same database, and it worked ! – theroom101 Sep 19 '16 at 11:28
1

Your code seems to be fine. You are calling DROP TABLE and then creating again subsequently. How you checked the DROP TABLE is not working?

Guna
  • 121
  • 8