0

您好,我正在尝试为我的应用程序构建一个 sqlite 数据库,但它返回一个错误“(1)表文章没有名为作者的列”

我了解问题的性质,但我现在几个小时都无法发现错误..

 package com.example.newyork;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper{

    //Database Initialize
    private static final int DATABASE_VERSION = 6;
    private static final String DATABASE_NAME = "NewYorkApplication";

    //Table 'Articles' name definition
    private static final String TABLE_ARTICLES = "articles";

    //Table 'Articles' Column Names
    private static final String KEY_ID = "key";
    private static final String KEY_TITLE = "title";
    private static final String KEY_TEXT = "text";
    private static final String KEY_AUTHOR = "author";
    private static final String KEY_DATE = "date";


    public DatabaseHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //Creating Tables 
    @Override
    public void onCreate(SQLiteDatabase db){
        String CREATE_ARTICLES_TABLE = "CREATE TABLE " + TABLE_ARTICLES + "("
                + KEY_ID + " INTEGER PRIMARY KEY ,"
                + KEY_TITLE + " TEXT,"
                + KEY_TEXT + " TEXT," 
                + KEY_AUTHOR + "TEXT,"
                + KEY_DATE + "TEXT"+  ")";
        db.execSQL(CREATE_ARTICLES_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLES);

        // Create tables again
        onCreate(db);
    }

    //Adding Article
    public void addArticle(Article article){

        //Get Database and initialize contentValues
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        //Put new values into Values
        values.put(KEY_TITLE, article.getTitle());
        values.put(KEY_TEXT, article.getText());
        values.put(KEY_DATE, article.getDate());
        values.put(KEY_AUTHOR, article.getAuthor());    

        //Insert table row
        db.insert(TABLE_ARTICLES, null, values);
        db.close();
    }

    public Article getArticle(int id) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_ARTICLES, new String[] { KEY_ID,
                KEY_TITLE, KEY_AUTHOR,KEY_TEXT,KEY_DATE }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Article article = new Article(cursor.getString(1),cursor.getString(2),cursor.getString(4),cursor.getString(3));
        // return contact
        return article;
    }

}
4

3 回答 3

0

确保在列名和类型之间放置一个空格:

+ KEY_AUTHOR + "TEXT,"
+ KEY_DATE + "TEXT"+ 

应该:

+ KEY_AUTHOR + " TEXT,"
+ KEY_DATE + " TEXT"+ 

否则,您的陈述将类似于:

CREATE TABLE articles(key INTEGER PRIMARY KEY , title TEXT, text TEXT, authorTEXT,dateTEXT);

此外,如果您最近一直在积极地改变东西,请确保您实际上正在使用您认为的数据库版本。

于 2013-07-25T08:23:41.440 回答
0

您忘记了 KEY_AUTHOR 和 KEY_DATE 的“TEXT”的 T 之前的空格

于 2013-07-25T08:24:35.307 回答
0

我认为您的问题是您在列类型之前缺少空格,请尝试以下操作:

String CREATE_ARTICLES_TABLE = "CREATE TABLE " + TABLE_ARTICLES + "("
                + KEY_ID + " INTEGER PRIMARY KEY ,"
                + KEY_TITLE + " TEXT,"
                + KEY_TEXT + " TEXT," 
                + KEY_AUTHOR + " TEXT,"
                + KEY_DATE + " TEXT"+  ")";
        db.execSQL(CREATE_ARTICLES_TABLE);
于 2013-07-25T08:27:00.057 回答