2013-03-26 62 views
0

有人幫助,我不知道什麼是錯的這個代碼,看起來好像沒什麼問題... 這是我的代碼的SQLiteSQLITE錯誤而創建和插入

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, 

我想創建數據庫以及插入,但我得到上面的錯誤。我不明白這一點,因爲我已經做了很多次...

回答

1

這個問題在你的代碼中很清楚。您正在創建具有四列的表chapter,但在插入中僅指定了三個值。

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');