繁体   English   中英

创建和插入时出现SQLITE错误

[英]SQLITE ERROR while creating and inserting

有人帮忙,我不知道这段代码出了什么问题,对我来说很好...这是我的代码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, 

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

从您的代码中,问题很明显。 您正在创建具有四列的表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');

因为您的_idautoincrement ,所以不需要指定它-但现在由于您没有指定所有列,因此需要显式指定要插入的列,因此insert语句应该为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM