Android SQLite插入语句错误

时间:2015-03-29 16:58:06

标签: android-sqlite

在我的数据库中,我修改了我的表格。之前它有2列,不包括id。现在它有3列,不包括id。因此,每当我添加元组时,它都会显示错误。 对于添加元组,我称之为addContact函数

      private static final int DATABASE_VERSION = 25;


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

    // Contacts table name
    private static final String TABLE_CONTACTS1 = "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";
    private static final String KEY_UNIQUE = "unique";

    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_CONTACTS1 + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_UNIQUE + " INTEGER," + 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_CONTACTS1);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_UNIQUE, contact.getUnique());
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
         //giving error at following line
        db.insert(TABLE_CONTACTS1, null, values);
        db.close(); // Closing database connection
    }

//错误是插入名称时错误= 21:58 unique = 1717 phone_number = null3     android.database.sqlite.SQLiteException:near" unique&#34 ;:语法错误(代码1):,编译时:INSERT INTO contacts(name,unique,phone_number)VALUES(?,?,?)

1 个答案:

答案 0 :(得分:1)

UNIQUE是SQL中的关键字,不能用作标识符。

将列名重命名为isunique或类似名称,然后卸载您的应用,以便实际运行修改后的onCreate()