Android Studio SQLite database creation does not work?

0

Issue

I’m not an experienced user when it comes to Android Studio and/or Java programming. I am working on a project for which is required an SQLite DB to be used by an app.

Following some guides i created a Java Class in order to locally manage the very specific DB I will be using, coded as follows:

public class gestionedb {
static final String KEY_RIGAID = "_id";
static final String KEY_EMAIL = "email";
static final String KEY_PASSWORD = "password";
static final String KEY_NICKNAME = "nickname";
static final String TAG = "gestionedb";
static final String DATABASE_NOME = "superchat5db";
static final String DATABASE_TABELLA = "users";
static final int DATABASE_VERSIONE = 1;

static final String DATABASE_CREAZIONE = "create table clienti (_id integer primary key autoincrement, " + "email text not null, password text not null, nickname text not null);";

final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public gestionedb(Context ctx){
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NOME, null, DATABASE_VERSIONE);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREAZIONE);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
    }
}

public gestionedb open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

public void close() {
    DBHelper.close();
}

public Cursor obtainallusers() {
    return db.query(DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, null, null, null, null, null);
}

public Cursor obtainuser(long rigaId) throws SQLException {
    Cursor mCursore = db.query(true, DATABASE_TABELLA, new String[] {KEY_RIGAID, KEY_EMAIL, KEY_NICKNAME}, KEY_RIGAID + "=" + rigaId, null, null, null, null, null);
    if (mCursore != null) {
        mCursore.moveToFirst();
    }
    return mCursore;
}

public long adduser(String email, String password, String nickname) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_EMAIL, email);
    initialValues.put(KEY_PASSWORD, password);
    initialValues.put(KEY_NICKNAME, nickname);
    return db.insert(DATABASE_TABELLA, null, initialValues);
}

public boolean deleteuser(long rigaId) {
    return db.delete(DATABASE_TABELLA, KEY_RIGAID + "=" + rigaId, null) > 0;
}

public boolean updatenickname(long rigaId, String nickname) {
    ContentValues args = new ContentValues();
    args.put(KEY_NICKNAME, nickname);
    return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
}

public boolean updatepsw(long rigaId, String password){
    ContentValues args = new ContentValues();
    args.put(KEY_PASSWORD, password);
    return db.update(DATABASE_TABELLA, args, KEY_RIGAID + "=" + rigaId, null) > 0;
}

}

Of course I had to tweak variables a bit in order to adapt this class to the DB I wanna use. The guide I used was in italian, therefore variables’ names are in italian, sorry about that.

I decided to test all of this by creating a button from my MainActivity that starts a brand new Activity called “ProvaDB” (TestDB), which does the following:

gestionedb db = new gestionedb(this);

    db.open();
    long id = db.adduser("prova@gmail.com", "prova123", "prova1");
    id = db.adduser("prova2@gmail.com", "prova123", "prova2");
    db.close();

    db.open();
    Cursor c = db.obtainallusers();
    if (c.moveToFirst()){
        do{
            Toast.makeText(this,
                    "id: " + c.getString(0) + "\n" +
                    "Email: " + c.getString(1) + "\n" +
                    "Nickname: " + c.getString(2),
                    Toast.LENGTH_LONG).show();
        } while (c.moveToNext());
    }
    db.close();

}

It is supposed to create the DB and add a couple of users, then list them with a long Toast text. When I try to run the app, after clicking on the button to go to “ProvaDB”, the app crashes. Looking at logs, I noticed that the DB is missing, which is weird. I can’t understand what’s wrong about this, since I’m not an experienced user, as I mentioned earlier. Any ideas?

Part of the log:

2018-12-10 19:11:05.175 17701-17701/com.example.lorenzo.superchat5 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lorenzo.superchat5, PID: 17701
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lorenzo.superchat5/com.example.lorenzo.superchat5.ProvaDB}: android.database.sqlite.SQLiteException: no such table: users (code 1): , while compiling: SELECT _id, email, nickname FROM users
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
    (no such table: users (code 1): , while compiling: SELECT _id, email, nickname FROM users)

Thank you very much.

Solution

Maybe while you were making changes to various names, you forgot this:

DATABASE_TABELLA = "users";

change it to:

DATABASE_TABELLA = "clienti";

and check other parts of your code that you may have hardcoded such names and change them to the new names.

Answered By – forpas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More