有人帮忙,我不知道这段代码有什么问题,对我来说很好看...... 这是我的代码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,
我正在尝试创建数据库以及插入但我得到上面的错误。我不明白这一点,因为我做了很多次......
答案 0 :(得分: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');
由于您的_id
是autoincrement
,因此您无需指定它 - 但现在因为您没有指定所有列,您需要明确指定要插入的列,因此insert
声明应该是:
INSERT INTO chapters
(chapter_number, chapter_name, chapter_title)
VALUES (
'I',
'CHAPTER ONE',
'SOVEREIGNTY OF THE PEOPLE AND SUPREMACY OF THIS CONSTITUTION');