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

  • Home
  • SEARCH
  • 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 6618305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:48:53+00:00 2026-05-25T20:48:53+00:00

While implementing the save-/loadfunction of my database, I dropped the tables to see if

  • 0

While implementing the save-/loadfunction of my database, I dropped the tables to see if my app can handle the creation of the tables if necessary – well it can’t

So in the onCreate of my testactivity, I create an instance of DB which creates in it’s constructor an instance of DBOpenHelper.
Basically when the DBOpenHelper instance calls getWritableDatabase() DBOpenHelper’s onCreate should be executed, creating the tables in my database.
I think the problem is, that getWritableDatabase() doesn’t get called, because the Database itself already exists – think I’ve read something like this method absorbed the function of openOrCreateDatabase.

How can I make sure, that the tables are recreated if not existing without having to delete and recreate the database everytime?

DBOpenhelper:

public class DBOpenHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static String DATABASE_NAME = "RTDB";
public static final String DB_TABLE_NAME1 = "playertable";
public static final String DB_TABLE_NAME2 = "itemtable";
public static String TAG = "openhelper";
private static final String DB_CREATE_TABLE_PT = "CREATE TABLE IF NOT EXISTS "
        + DB_TABLE_NAME1
        + " ("
        + "ID INT(2) PRIMARY KEY AUTOINCREMENT,"
        + "Name VARCHAR(30) ,"
        + "HP INT(3) ,"
        + "Satisfaction INT(3) ,"
        + "Hygiene INT(1) , " + "IsAlive INT(1) " + " )";

private static final String DB_CREATE_TABLE_IT = "CREATE TABLE IF NOT EXISTS "
        + DB_TABLE_NAME2
        + " ("
        + "Money INT(3) ,"
        + "Gas INT(3) ,"
        + "Food INT(3) ,"
        + "Toiletries INT(3) ,"
        + "Spareparts INT(3) ,"
        + "Meds INT(3) ,"
        + "Tents INT(3) ,"
        + "Ration INT(1) ,"
        + "Trabbihp INT(3) ," + "Trabbispeed INT(2)" + " )";

public DBOpenHelper(Context context, String databaseName) {
    super(context, databaseName, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d("TAG", "PT :" + DB_CREATE_TABLE_PT);
    db.execSQL(DB_CREATE_TABLE_PT);
    Log.d(TAG, "PT create" + DB_CREATE_TABLE_PT);
    db.execSQL(DB_CREATE_TABLE_IT);
    Log.d(TAG, "IT create" + DB_CREATE_TABLE_IT);
}

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

}

DB:

public class DB {

private Context context;
private SQLiteDatabase db;
private static String TAG = "save";
private static ContentValues itemValues = new ContentValues();
private static ContentValues playerValues = new ContentValues();

public DB(Context context) {
    this.context = context;
    DBOpenHelper dbHelper = new DBOpenHelper(this.context, "RTDB");
    this.db = dbHelper.getWritableDatabase();
}

public void savePlayer(Player player[]) {

    for (int i = 0; i <= 3; i++) {

        playerValues.put("Name", player[i].getName());
        playerValues.put("HP", player[i].getHp());
        playerValues.put("Satisfaction", player[i].getsatisfaction());
        playerValues.put("Hygiene", player[i].isHygieneInt());
        playerValues.put("IsAlive", player[i].isAliveInt());

    }
    db.insert("playertable", null, playerValues);
}

// Speichern der Items
// TODO Position fehlt noch
public void saveItems() {
    itemValues.put("Money", Resource.money);
    itemValues.put("Gas", Resource.gas);
    itemValues.put("Food", Resource.food);
    itemValues.put("Toiletries", Resource.toiletries);
    itemValues.put("Spareparts", Resource.spareparts);
    itemValues.put("Meds", Resource.meds);
    itemValues.put("Tents", Resource.tents);
    itemValues.put("Ration", Resource.ration);
    itemValues.put("Trabbihp", Resource.trabbihp);
    itemValues.put("Trabbispeed", Resource.trabbispeed);

    db.insert("itemtable", null, itemValues);
}

// Hier werden die Items aus der Datenbank abgefragt, der zurueckgelieferte
// Cursor vie cursorToIntArray() in einen Int Array umgewandelt und dessen
// Inhalt in die Ressource Klasse geschrieben
public void loadItems() {
    Cursor itemCursor = db.query("itemtable", null, null, null, null, null,
            null);
    int[] itemIntArray = cursorToInt(itemCursor, 9);

    Resource.money = itemIntArray[0];
    Resource.gas = itemIntArray[1];
    Resource.food = itemIntArray[2];
    Resource.toiletries = itemIntArray[3];
    Resource.meds = itemIntArray[4];
    Resource.tents = itemIntArray[5];
    Resource.ration = itemIntArray[6];
    Resource.trabbihp = itemIntArray[7];
    Resource.trabbispeed = itemIntArray[8];
}

// Name und Restliche Int-Werte der Playerobjekte werden separat aus der
// Datenbank geholt und gesetzt
public void loadPlayer() {
    String[] namecolumn = { "Name" };
    String[] intcolumn = { "ID, Name, HP, Satisfaction, Hygiene, IsAlive" };
    String[] namesToString = new String[4];

    Cursor playerCursor = db.query("playertable", intcolumn, null, null,
            null, null, "ID");
    playerCursor.moveToPosition(-1);
    int i = 0;
    while (i <= 3) {
        playerCursor.moveToNext();
        String temp = playerCursor.getString(1);
        Resource.playerArray[i].setName(temp);
        int tempint = playerCursor.getInt(2);
        Resource.playerArray[i].setHp(tempint);
        tempint = playerCursor.getInt(3);
        Resource.playerArray[i].setsatisfaction(tempint);
        tempint = playerCursor.getInt(4);
        Resource.playerArray[i].setHygieneInt(tempint);
        tempint = playerCursor.getInt(5);
        Resource.playerArray[i].setAliveInt(tempint);
        i++;

    }
}

public void dropTables() {
    db.execSQL("DROP TABLE 'playertable';");
    db.execSQL("DROP TABLE 'itemtable';");
}

private int[] cursorToInt(Cursor cursor, int n) {
    int[] results = new int[n];
    for (int i = 0; i <= n - 1; i++) {
        results[i] = cursor.getInt(i + 1);
    }

    return results;
}

private String[] cursorToString(Cursor cursor) {
    String[] results = new String[4];
    int columnIndex = cursor.getColumnIndex("Name");
    for (int i = 0; i <= 3; i++) {
        results[i] = cursor.getString(columnIndex);
    }

    return results;
}

}

Testactivity onCreate instancing DB:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.myDB = new DB(this);
    initUI();
    testcase1();
  • 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-05-25T20:48:54+00:00Added an answer on May 25, 2026 at 8:48 pm

    Not sure your description of how getWritableDatabase functions is correct: looking at your code you appear to be doing the right thing, as I use a very similar structure in many deployed apps without any errors. It correctly handles creating/upgrading all tables, regardless of whether or not they exist or the database exists. You might want to try making DBOpenHelper a static class: that works for me. Alternatively check your logs carefully: if there’s some other exception taking place that may not appear related at first glance then that might point you towards a solution.

    EDIT: Try private static class DBOpenHelper extends SQLiteOpenHelper. What I’d also suggest is changing the database version to force the onUpgrade method to be called. In general the case where you’ve dropped tables but the file exists would need you to call into the information schema to check if the database file has the tables defined. onCreate only gets called if the file doesn’t exist, and onUpdate only gets called if you change the database version.

    EDIT: see http://www.sqlite.org/faq.html#q7 for more detail on using the information schema table to check if your table exists. You can just run a query against the SQLITE_MASTER table and then if you can’t find a particular table run the CREATE statement.

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

Sidebar

Related Questions

While implementing a tree structure over a SQL 2005 server database, the query response
While implementing the in-app billing for Android application, I came across a problem. Let
while implementing triggers in mysql5 can i pass any session value that is in
I'm developing my first iPhone app, and while implementing user preferences, I have a
While implementing In-App purchase features, do I have to serve option to restore purchases
While implementing web applications on top of MySQL database, I'm thinking if it is
While implementing a design using nested generic collections, I stumbled across those limitations apparently
While implementing XML file reading/writing in my application I saw that when I call
I'm writing a web site (C#, ASP 3.5) while implementing a simple CMS. In
I am implementing a sound effect that plays while a user is dragging a

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.