想要插入2个参数时会出现错误,当它只有一个参数时它会没问题

时间:2016-03-26 16:15:58

标签: android sqlite

嘿伙计们可以告诉我为什么我的方法addAlarm在我只添加单个" alarm"当我想为我的闹钟添加一些名字时,我收到一条错误,上面写着no such column: alarmname (code 1): , while compiling: INSERT or replace INTO alarms (alarm,alarmname) VALUES(1459006407264,alarmname)我想添加一个名为" alarmname"的闹钟。怎么解决这个问题? 这就是我创建表的方式:

public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
                + KEY_ID + " INTEGER PRIMARY KEY , " + KEY_ALARM + " TEXT, "
                +  KEY_NAME + " TEXT" + ")";
        db.execSQL("create table alarms ("
                + "id integer primary key,"
                + "alarm text,"
                + "name text," // added a ','
                + "alarmname text" + ");");
    }

我的addAlarm方法:

void addAlarm(Alarm alarm) {
        SQLiteDatabase db = this.getWritableDatabase();

        String sql =
                "INSERT or replace INTO alarms (alarm,alarmname) VALUES("+ alarm.getAlarm() + ","+ alarm.getName() + ")" ;
        db.execSQL(sql);
        db.close(); // Closing database connection
    }

1 个答案:

答案 0 :(得分:0)

警报的名称是字符串文字,因此应该用单引号(' s)包围:

String sql =
    "INSERT or replace " +
    "INTO alarms (alarm,alarmname) " +
    "VALUES("+ alarm.getAlarm() +  ",'" + alarm.getName() + "')";
    // Here -------------------------^ And here -------------^