AUTOINCREMENT不工作 [英] AUTOINCREMENT not working

查看:188
本文介绍了AUTOINCREMENT不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建1 predefine行中有一个表。 所以,我加入中的onCreate()行;

I want to create a table with 1 predefine row in it. So, I am adding the row in onCreate();

以下是我的code:

private static final String CREATE_CATEGORY_TABLE = "CREATE TABLE CATEGORYTABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT, category_name VARCHAR(255));";

private static final String DROP_CATEGORY_TABLE = "DROP TABLE IF EXISTS CATEGORYTABLE";

private static final String INSERT_PREDEFINED_CATEGORY = "INSERT INTO CATEGORYTABLE VALUES('Category 1');";

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        try {
            db.execSQL(DROP_CATEGORY_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            db.execSQL(CREATE_CATEGORY_TABLE);
            db.execSQL(INSERT_PREDEFINED_CATEGORY); <-- error on this line 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

错误消息:

sqlite returned: error code = 1, msg = table CATEGORYTABLE has 2 columns but 1 values were supplied

Failure 1 (table CATEGORYTABLE has 2 columns but 1 values were supplied) on 0x287cb8 when preparing 'INSERT INTO CATEGORYTABLE VALUES('Home');'.

这是要在表格中插入pdefined行中的$ P $的正确方法? 请帮帮忙!

Is this the correct way to insert a predefined row in a table? Please help!

推荐答案

该表有两列,所以你要么需要提供2个值,或指定要插入的列名。更改

The table has 2 columns so you will either need to provide 2 values, or specify the column names you want to insert to. Change

INSERT INTO CATEGORYTABLE VALUES('Category 1')

INSERT INTO CATEGORYTABLE VALUES(NULL, 'Category 1')

INSERT INTO CATEGORYTABLE(category_name) VALUES('Category 1')

有关它的价值,这也是一个不好的做法,以赶在的onCreate异常() onUpgrade() 。如果有一个问题,回调应该不会正常返回。

For what it's worth, it's also a bad practice to catch exceptions in onCreate() or onUpgrade(). If there's a problem, the callback should not return normally.

这篇关于AUTOINCREMENT不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆