android SQLite表有4列,但提供了5个值

时间:2015-12-12 13:27:39

标签: android sqlite

title = intent.getStringExtra("TITLE");
    author = intent.getStringExtra("AUTHOR");
    rating = intent.getFloatExtra("RATING", (float) 0.0);
    quotes = intent.getStringExtra("QUOTES");

    myDBHelper = new MyDBHelper(getApplicationContext());

    SQLiteDatabase db = myDBHelper.getWritableDatabase();

    String sql = String.format("INSERT INTO book VALUES(null,'%s','%s',%f,'%s')",title,author,rating,quotes);

是的,我插入了4个vales,但错误信息显示"提供了5个值"

这是我的DB类

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "CREATE TABLE book ( _id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT , author TEXT , rating REAL , quotes TEXT);";
        db.execSQL(sql);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql = "DROP TABLE book IF EXISTS book;";
        db.execSQL(sql);

        onCreate(db);

    }

我在logcat

上收到此消息
12-12 22:22:57.740 9757-9757/? E/AndroidRuntime: ... table book has 4 columns but 5 values were supplied: , while compiling: INSERT INTO book VALUES(null,'null','null',0.000000,'null')

为什么?

1 个答案:

答案 0 :(得分:0)

我看到有5个数据值。你可以像这样插入

    ContentValues values = new ContentValues();
    values.put("title", intent.getStringExtra("TITLE"));
    values.put("author", intent.getStringExtra("AUTHOR"));
    values.put("rating", intent.getFloatExtra("RATING", (float) 0.0));
    values.put("quotes", intent.getStringExtra("QUOTES"));
    // Inserting Row
    db.insert("book", null, values);

它应该有用。