在db中插入值时出错

时间:2016-11-23 11:14:17

标签: android sqlite

我试图找到为什么我的应用程序在开始时崩溃,调试器说

“E / SQLiteLog:(1)没有这样的表:类别 E / SQLiteDatabase:插入Name = Salads时出错                   android.database.sqlite.SQLiteException:没有这样的表:Categories(代码1):,同时编译:INSERT INTO Categories(Name)VALUES(?)“

在我查看我的代码之后,看起来像表格类别并且错误不应该出现。

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

3 个答案:

答案 0 :(得分:0)

在TEXT使用此

之前,您没有提供空间
private static final String CREATE_MEALS = "CREATE TABLE "
    + Meals + "(" + KEY_ID + " INTEGER PRIMARY KEY," + mealsName
    + " TEXT unique," + desc + " TEXT," + size + " TEXT,"
    + price + " TEXT" + ")";

答案 1 :(得分:0)

private static final int DATABASE_VERSION = 1;的值更改为private static final int DATABASE_VERSION = 2;也许您修改了数据库结构并忘记以这种方式更新它。告诉我它是否有效。

答案 2 :(得分:0)

//创建数据库处理程序的对象 MyDatabaseHelper dbHelperDriver = new MyDatabaseHelper(this);

//使用对象插入类别 dbHelperDriver.addCategory( “TestCategory”);