我创建了一个扩展SQLiteOpenHelper类的类,并尝试在我的Android应用启动时创建数据库并插入虚拟记录。
但是我收到错误'表库没有名为title'。
的列其他列不会抛出任何错误,并且它们都以相同的方式创建。
错误:
12-19 14:24:30.401 4801-4807/? E/art: Failed sending reply to debugger: Broken pipe
12-19 14:24:30.808 4801-4801/? E/SQLiteLog: (1) table gallery has no column named title
12-19 14:24:30.810 4801-4801/? E/SQLiteDatabase: Error inserting title=First pic artist=James Liscombe rank=3 year=1992
android.database.sqlite.SQLiteException: table gallery has no column named title (code 1): , while compiling: INSERT INTO gallery(title,artist,rank,year) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.jamesliscombe.cet325assignment.DBHandler.addPicture(DBHandler.java:59)
at com.example.jamesliscombe.cet325assignment.Home.onCreate(Home.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我创建的类扩展了SQLiteOpenHelper:
public class DBHandler extends SQLiteOpenHelper {
//DB Info
private static final int DB_VERSION = 2;
private static final String DB_NAME = "louvre";
private static final String TABLE_NAME = "gallery";
//Table column names
private static final String KEY_ID = "id";
private static final String KEY_RANK = "rank";
private static final String KEY_ARTIST = "artist";
private static final String KEY_TITLE = "title";
private static final String KEY_YEAR = "year";
public DBHandler(Context context) {
super(context, DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_GALLERY_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + "INTEGER PRIMARY KEY," + KEY_RANK + "INTEGER,"
+ KEY_ARTIST + "TEXT," + KEY_TITLE + "TEXT," + KEY_YEAR + "TEXT" + ")";
db.execSQL(CREATE_GALLERY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop older table if it exists
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//Create table again
onCreate(db);
}
//Adding a new gallery record
public void addPicture(Picture picture) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_RANK, picture.getRank());
values.put(KEY_ARTIST, picture.getArtist());
values.put(KEY_TITLE, picture.getTitle());
values.put(KEY_YEAR, picture.getYear());
//Insert row
db.insert(TABLE_NAME, null, values);
//Close db connection
db.close();
}
//Reading a single record
public Picture getPicture(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] {
KEY_ID,KEY_RANK,KEY_ARTIST,KEY_TITLE,KEY_YEAR
},KEY_ID + "=?", new String[] {
String.valueOf(id) }, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Picture contact = new Picture(Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)),cursor.getString(2),cursor.getString(3), cursor.getString(4));
return contact;
}
//Reading all records
public List<Picture> getAllPictures() {
List<Picture> galleryList = new ArrayList<Picture>();
//Select all query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//Loop through all rows and add to galleryList.
if(cursor.moveToFirst()) {
do {
Picture picture = new Picture();
picture.setId(Integer.parseInt(cursor.getString(0)));
picture.setRank(Integer.parseInt(cursor.getString(1)));
picture.setArtist(cursor.getString(2));
picture.setTitle(cursor.getString(3));
picture.setYear(cursor.getString(4));
galleryList.add(picture);
} while (cursor.moveToNext());
}
return galleryList;
}
//Get total number of records
public int getGallerySize() {
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
//Update gallery items
public int updateGallery(Picture picture) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_RANK, picture.getRank());
values.put(KEY_ARTIST, picture.getArtist());
values.put(KEY_TITLE, picture.getTitle());
values.put(KEY_YEAR, picture.getYear());
return db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(picture.getId())});
}
//Delete a gallery record
public void deletePicture(Picture picture) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + " = ?", new String[] {String.valueOf(picture.getId())});
db.close();
}
}
我在使用该课程并插入虚拟记录的地方:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Create our DB
DBHandler db = new DBHandler(this);
//Insert some test data into our db
db.addPicture(new Picture(1,3,"James","First pic","1992"));
答案 0 :(得分:1)
您的设备中已安装了一个不包含title
列的数据库,因此当您尝试使用Title
列的值添加虚拟数据时,会出现错误。
这可以通过删除旧的已创建数据库然后再次运行应用程序来修复,以便创建新数据库。
要删除现有数据库,请转到Android Device Monitor -> data -> data
,然后搜索应用的程序包名称。点击您应用的软件包名称,然后删除其中的database
文件夹,然后再次运行您的应用。