您好,我正在尝试为我的应用程序构建一个 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;
}
}