我无法将数据插入SQLite,也看不到我的表

时间:2018-09-30 10:51:23

标签: java android database sqlite

我无法将数据插入SQLite,也看不到我的表。
我尝试在DB Browser for SQLite中查看该表,但看不到插入的任何内容,也看不到我创建的行。

从DataBaseHelper中插入方法:

//Adding new trainee
public void addTrainee(Trainee trainee) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    int count = getShopsCount();

    values.put(COL_ID, 1);
    values.put(COL_USERNAME, trainee.getUsername());
    values.put(COL_NAME, trainee.getName());
    values.put(COL_PASS, trainee.getPassword());
    values.put(COL_EMAIL, trainee.getEmail());

    // Inserting Row
    db.insert(TABLE_NAME, null, values);
    db.close();// Closing database connection
}

// Getting shops Count
public int getShopsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();
    // return count
    return cursor.getCount();
}

在此处插入主要活动的注册:

public void confirminput(View v) {
    //If one of the validate retuen false the validation faild.
    if (  !validateEmail() || !validateUsername() || !validateName() || !validatePassword()) {
        Toast.makeText(getApplicationContext(), " Validation NOT OK", Toast.LENGTH_LONG).show();
        return;
    }

    //Inserting Trainee.
    Trainee trainee = new Trainee();
    trainee.setUsername(textInputUsername.getEditText().getText().toString().trim();
    trainee.setName(textInputName.getEditText().getText().toString().trim());
    trainee.setEmail(textInputEmail.getEditText().getText().toString().trim());
    trainee.setPassword(textInputPassword.getEditText().getText().toString().trim();

    //Insert Method data .
    myDb.addTrainee(trainee);


}

onCreate():

//onCreat
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_CONTACTS_TABLE = "CREATE TABLE "+ TABLE_NAME + "("
            + COL_ID + " INTEGER PRIMARY KEY," + COL_USERNAME + " TEXT,"
            + COL_NAME  + " TEXT," + COL_PASS + " TEXT," +COL_EMAIL + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
    this.db = db;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

感谢您的帮助 问题是创建了错误的表

@Override
public void onCreate(SQLiteDatabase db) {

    String createTraineeTable = "create table trainee ("+COL_ID+" integer primary key AUTOINCREMENT  ," +
            COL_USERNAME+" text not null, "+COL_NAME+" text not null, "+COL_PASS+" text not null,"+COL_EMAIL+" text not null );";

    db.execSQL(createTraineeTable);
    this.db = db;
}