为了账号安全,请及时绑定邮箱和手机立即绑定

我看不到数据,也看不到我的表创建的sqlite

我看不到数据,也看不到我的表创建的sqlite

POPMUISE 2021-10-20 15:55:54
我无法向 SQLite 插入数据,也无法看到我的表。我尝试在 SQLite 的 DB 浏览器中查看该表,但是我看不到插入的任何内容,也看不到我创建的行。数据库助手:公共类 DatabaseHelper 扩展 SQLiteOpenHelper {// Database Versionpublic static final int DATABASE_VERSION = 1;// Database Namepublic static final String DATABASE_NAME = "traineeInfo";// Contacts table namepublic static final String TABLE_NAME = "trainee";// Trainee Table Columns namespublic 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 Helperpublic DatabaseHelper(Context context) {    super(context, DATABASE_NAME, null, DATABASE_VERSION);}//onCreat@Overridepublic 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@Overridepublic 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 traineepublic 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}
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

您正在尝试向不存在的表添加条目,因为您没有在类的onCreate方法中创建正确的表DatabaseHelper(联系人!= 受训者)。


所以改变这个:


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

}

到:


@Override

public void onCreate(SQLiteDatabase db) {

       String createTraineeTable = "create table trainee (id integer primary key not null ," +

            " username text not null, name text not null, email text not null,password text not null );";


    db.execSQL(createTraineeTable);

    this.db = db;

}

此外,我建议您格式化字符串并使用您定义的常量来防止这样的错误。例如:


String createTraineeTable = String.format("create table %s (%s integer primary key not null, %s text not null, %s text not null, %s text not null, %s text not null", TABLE_NAME , COL_ID, COL_USERNAME, COL_NAME, COL_PASS, COL_EMAIL);



查看完整回答
反对 回复 2021-10-20
?
PIPIONE

TA贡献1829条经验 获得超9个赞

问题是创建表


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

}


查看完整回答
反对 回复 2021-10-20
  • 2 回答
  • 0 关注
  • 321 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信