我无法向 SQLite 插入数据,也无法看到我的表。我尝试在 SQLite 的 DB 浏览器中查看该表,但是我看不到插入的任何内容,也看不到我创建的行。
数据库助手:
公共类 DatabaseHelper 扩展 SQLiteOpenHelper {
// Database Version
public static final int DATABASE_VERSION = 1;
// Database Name
public static final String DATABASE_NAME = "traineeInfo";
// Contacts table name
public static final String TABLE_NAME = "trainee";
// Trainee Table Columns names
public static final String COL_ID = "ID";
public static final String COL_USERNAME = "USERNAME";
public static final String COL_NAME = "NAME";
public static final String COL_PASS = "PASSWORD";
public static final String COL_EMAIL = "EMAIL";
SQLiteDatabase db;
//DataBase Helper
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//onCreat
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "create table contacts (id integer primary key not null ," +
" username text not null, name text not null, email text not null,password text not null );";
db.execSQL(CREATE_CONTACTS_TABLE);
this.db = db;
}
//onUpgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Creating tables again
this.onCreate(db);
}
//Adding new trainee
public void addTrainee(Trainee trainee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
int count = getTraineeCount();
values.put(COL_ID, count);
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
}
跃然一笑
PIPIONE
相关分类