0

So i'm having a little trouble with SQLite in Android. I followed a guide with which i created a table and wrote a working method to insert data. After i had a lot of data in it i wanted to delete it again so i wrote a method which dropped my table. Now i have the problem that it won't create the table anymore. If i want to insert data it says "no such table".I don't really get why it won't create it anymore. Relevant code:

public class MeFragment extends Fragment implements View.OnClickListener {

private SQLiteDatabase newDB;
private DatabaseHelper dbHelper;
private String TABLE_NAME = DatabaseHelper.TABLE_NAME;


ArrayList<String> myCategoryList = new ArrayList<String>() {  

};
ListView lv1;
ArrayAdapter<String> adapter1;
EditText editText;
Button addButton;


public MeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_me, container, false);

    dbHelper = new DatabaseHelper(this.getActivity());
    preFillCategories();
    getCategories();

    editText = (EditText) view.findViewById(R.id.addCategory);
    addButton = (Button) view.findViewById(R.id.addCategoryButton);
    addButton.setOnClickListener(this);
    lv1 = (ListView) view.findViewById(R.id.listView1);
    adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, myCategoryList);
    lv1.setAdapter(adapter1);
    Utility.setDynamicHeight(lv1);
    //dbHelper.removeAllData();

    return view;
}

And my Database Helper Class:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "categories.db";
public static final String TABLE_NAME = "categories_table";
public static final String COL = "CATEGORYNAME";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME +" (CATEGORYNAME TEXT PRIMARY KEY)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.d("Hallo", "hallo");
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);

}

public boolean insertData(String CATEGORYNAME){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL, CATEGORYNAME);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1){
        return false;
    }
    else{
        return true;
    }

}

public void removeAllData(){

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DROP TABLE "+TABLE_NAME);

}

Any help would be very much appreciated

A.Moll
  • 25
  • 2
  • 7

1 Answers1

0

Can you add the removeAllData method to the dbhelper so we can see what you were doing?

Also, you're not doing anything to manage the db version number. If you're constantly using the same version number even after making changes, that could be a potential source of you problem.

Also, you should avoid calling any lifecycle methods directly (such as onCreate in your onUpgrade method). If you have code you want to invoke from multiple lifecycle methods, break that out into its own method so you don't run the risk of possibly introducing lifecycle issues on top of your current problem.

Josh Kitchens
  • 1,080
  • 11
  • 18