0

有人帮忙,我不知道这段代码有什么问题,对我来说看起来不错...这是我的代码 sqlite

package com.ilab.klr3;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;

public class SQLiteDbhelper extends SQLiteOpenHelper {
    private static final int DB_VERSION=1;
    public static final String DB_NAME="klr";
    public static final String CHAPTER_NUMBER="chapter_number";
    public static final String CHAPTER_NAME="chapter_name";
    public static final String CHAPTER_TITLE="chapter_title";
    public static final String PART_DESC="part_description";
    public static final String PART_NUMBER="part_number";
    public static final String PART_TITLE="part_title";
    public static final String ARTICLE_ID="article_id";
    public static final String ARTICLE_CONTENT="article_content";
    public static final String ARTICLE_NUMBER="article_numer";

    public SQLiteDbhelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_chapters=String.format("CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT, " +
                "%s TEXT, %s TEXT);",KLRConstants.TABLE_CHAPTER,KLRConstants.CHAPTER_ID, CHAPTER_NUMBER,CHAPTER_NAME,CHAPTER_TITLE);
        String create_parts=String.format("CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT, " +
                "%s TEXT, %s TEXT);",KLRConstants.TABLE_PARTS,KLRConstants.PART_ID,PART_DESC,PART_NUMBER,PART_TITLE);
        String create_articles=String.format("CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT, " +
                "%s TEXT, %s TEXT);",KLRConstants.TABLE_ARTICLES,KLRConstants.ARTICLE_ID,ARTICLE_ID,ARTICLE_CONTENT,ARTICLE_NUMBER);
        db.execSQL(create_articles);
        db.execSQL(create_chapters);
        db.execSQL(create_parts);
        (new InsertSampleData(db)).execute();
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    class InsertSampleData extends AsyncTask<Void, Void, Void>{
        SQLiteDatabase database=null;
        public InsertSampleData(SQLiteDatabase database){
            this.database=database;
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            Chapter chapter1 = new Chapter("I","CHAPTER ONE","SOVEREIGNTY OF THE PEOPLE AND SUPREMACY OF THIS CONSTITUTION");
            Chapter chapter2 = new Chapter("II","CHAPTER TWO","THE REPUBLIC"); 
            Chapter chapter3 = new Chapter("III","CHAPTER THREE","CITIZENSHIP"); 
            Chapter chapter4 = new Chapter("IV","CHAPTER FOUR","BILL OF RIGHTS"); 
            Chapter chapter5 = new Chapter("V","CHAPTER FIVE","LAND AND ENVIRONMENT"); 
            Chapter chapter6 = new Chapter("VI","CHAPTER SIX","LEADERSHIP AND INTEGRITY"); 

            KLRDBAdapter klradapter=new KLRDBAdapter(database);
            klradapter.inserChapter(chapter1);
            klradapter.inserChapter(chapter2);
            klradapter.inserChapter(chapter3);
            klradapter.inserChapter(chapter4);
            klradapter.inserChapter(chapter5);
            klradapter.inserChapter(chapter6);

            return null;
        }

    }
}

这是错误:

03-26 00:40:11.170: I/SqliteDatabaseCpp(12343): sqlite returned: error code = 1, msg = table chapter has 4 columns but 3 values were supplied, db=/data/data/com.ilab.klr3/databases/klr
03-26 00:40:11.185: E/KLRDBAdapter(12343): Could not create or Open the database
03-26 00:40:11.220: I/SqliteDatabaseCpp(12343): sqlite returned: error code = 1, msg = table chapter has 4 columns but 3 values were supplied, 

我正在尝试创建数据库以及插入,但出现上述错误。我不明白这一点,因为我已经这样做了很多次......

4

1 回答 1

1

从您的代码中可以清楚地看出问题所在。您正在创建chapter具有四列的表,但在插入时仅指定了 3 个值。

create table的是

CREATE TABLE IF NOT EXISTS chapters (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    chapter_number TEXT,
    chapter_name TEXT,
    chapter_title TEXT);

而您的插入(未显示但似乎来自错误)是

INSERT INTO chapters VALUES (
    'I',
    'CHAPTER ONE',
    'SOVEREIGNTY OF THE PEOPLE AND SUPREMACY OF THIS CONSTITUTION');

因为您的_idis autoincrement,您不需要指定它 - 但现在因为您没有指定所有列,您需要明确指定要插入的列,因此您的insert语句应该是:

INSERT INTO chapters 
    (chapter_number, chapter_name, chapter_title)
VALUES (
    'I',
    'CHAPTER ONE',
    'SOVEREIGNTY OF THE PEOPLE AND SUPREMACY OF THIS CONSTITUTION');
于 2013-03-26T10:06:37.000 回答