android SQLite寄存器不会保存数据

时间:2017-06-08 03:25:55

标签: java android database sqlite

我正在尝试制作简单的android SQLite寄存器,但当我尝试保存数据时,应用程序崩溃

数据库代码:

Image

public class RegistrationDB extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "registration.db";
    private static final String TABLE_NAME = "profile";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";


    private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT, " + ")";

    private String DROP_TAPLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    public RegistrationDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL(DROP_TAPLE);
        onCreate(sqLiteDatabase);

    }

    public long addUser(String email, String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COLUMN_EMAIL, email); // Email
        values.put(COLUMN_USERNAME, username); // username
        values.put(COLUMN_PASSWORD, password); // password

        // Inserting Row
//        long id = db.insert(TABLE_NAME, null, values);
        db.insert(TABLE_NAME, null, values);
        db.close(); // Closing database connection
        return 0;
    }

}

我很抱歉,但是当我尝试在此处键入代码时出现错误,这就是我使用图像的原因

2 个答案:

答案 0 :(得分:2)

我认为您对create table的查询存在问题:

  

COLUMN_PASSWORD +“TEXT,”+“)”;

如果查询结束,TEXT之后不应该有逗号“,”。 删除该逗号。 试试它是否有效。

答案 1 :(得分:-1)

更改

private static final String COLUMN_ID = "id";

private static final String COLUMN_ID = "_id";

并且还要改变..

private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT, " + ")";

private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT)";

完成此操作后,清除应用数据,或卸载并重新安装,然后重新运行。