Android Studio SQLite数据库创建不起作用?

时间:2018-12-10 18:32:54

标签: java android database sqlite

在Android Studio和/或Java编程方面,我不是经验丰富的用户。我正在开发一个项目,该项目需要应用程序使用SQLite数据库。

按照一些指南,我创建了一个Java类,以便本地管理我将要使用的特定数据库,其编码如下:

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;
}

}

当然,我必须稍微调整变量,以使此类适合我要使用的数据库。我使用的指南是意大利语,因此变量的名称是意大利语,对此感到抱歉。

我决定通过在MainActivity中创建一个按钮来测试所有这些操作,该按钮将启动一个名为“ ProvaDB”(TestDB)的全新活动,该活动将执行以下操作:

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();

}

应该创建数据库并添加几个用户,然后用长长的Toast文本列出他们。当我尝试运行该应用程序时,单击按钮转到“ ProvaDB”后,该应用程序崩溃。在查看日志时,我注意到数据库丢失了,这很奇怪。如前所述,我不是一个有经验的用户,所以我不明白这是怎么回事。有什么想法吗?

部分日志:

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)

非常感谢您。

1 个答案:

答案 0 :(得分:2)

也许在您对各种名称进行更改时,您忘记了这一点:

DATABASE_TABELLA = "users";

将其更改为:

DATABASE_TABELLA = "clienti";

并检查代码的其他部分,您可能已经硬编码了此类名称并将其更改为新名称。