我正在通过我的应用程序创建一个 sqlite 数据库。插入和创建表的查询执行良好。但是当我检索数据时,我的应用程序因以下日志而崩溃
Caused by: android.database.sqlite.SQLiteException: no such table: FileRevisionHistory:while compiling: SELECT FileName, FileRevision FROM FileRevisionHistory WHERE FileName=?
SQL 表创建的代码是这样的
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
public class FileRevision_dbHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "revisionHistoryManager";
// Contacts table name
private static final String TABLE_REVISION_HISTORY = "FileRevisionHistory";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "FileName";
private static final String KEY_REVISION = "FileRevision";
public FileRevision_dbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_FILE_REVISION_TABLE = "CREATE TABLE " + TABLE_REVISION_HISTORY + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_REVISION + " TEXT" + ")";
db.execSQL(CREATE_FILE_REVISION_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_REVISION_HISTORY);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addFileRevision(FileRevision filerevision) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, filerevision.mFileName); // Contact Name
values.put(KEY_REVISION, filerevision.mRevision); // Contact Phone
// Inserting Row
db.insert(TABLE_REVISION_HISTORY, null, values);
db.close(); // Closing database connection
}
// Getting single contact
String getFileRevision(String inFileName) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_REVISION_HISTORY, new String[] {KEY_NAME, KEY_REVISION }, KEY_NAME + "=?",
new String[] { inFileName }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
FileRevision filerevision = new FileRevision();
filerevision.mFileName = (String)cursor.getString(cursor.getColumnIndex(KEY_NAME));
filerevision.mRevision = (String)cursor.getString(cursor.getColumnIndex(KEY_REVISION));
db.close();
// return contact
return filerevision.mRevision;
}
}
}
为什么那个表没有被创建?