猿问

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

我无法向 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

}


POPMUISE
浏览 322回答 2
2回答

跃然一笑

您正在尝试向不存在的表添加条目,因为您没有在类的onCreate方法中创建正确的表DatabaseHelper(联系人!= 受训者)。所以改变这个:@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;}到:@Overridepublic 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);

PIPIONE

问题是创建表@Overridepublic 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;}
随时随地看视频慕课网APP

相关分类

Java
我要回答