0

Somebody help, I dont know what is wrong with this code, looks fine to me... This is my code 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;
        }

    }
}

This is the error:

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, 

I am trying to create database as well as insert but I get the error above. I dont understand this since i have done this a number of times...

1 Answer 1

1

The problem is very clear from your code. You are creating table chapter with four columns but are specifying only 3 values on the insert.

Your create table is

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

And your insert (which is not shown but seems to be from the error) is

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

Because your _id is autoincrement, you don't need to specify it - but now because you are not specifying all the columns, you need to explicitly specify which columns you are inserting, therefore your insert statement should be:

INSERT INTO chapters 
    (chapter_number, chapter_name, chapter_title)
VALUES (
    'I',
    'CHAPTER ONE',
    'SOVEREIGNTY OF THE PEOPLE AND SUPREMACY OF THIS CONSTITUTION');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.