【发布时间】:2016-04-21 14:46:40
【问题描述】:
我正在学习一本面向初学者的 Android 书籍,并正在尝试制作一个迷你应用程序来学习使用 SQLite 数据库。我已经构建了一个 DataManager 类,它创建一个数据库并处理 CRUD 操作:
public class DataManager {
// this is the actual database
private SQLiteDatabase db;
// public static final strings for each row/table we can refer to throughout the app
public static final String TABLE_ROW_ID = "_id";
public static final String TABLE_ROW_NAME = "name";
public static final String TABLE_ROW_AGE = "age";
// private finals for each row/table we need to refer to just inside this class
private static final String DB_NAME = "address_book_db";
private static final int DB_VERSION = 1;
private static final String TABLE_N_AND_A = "names_and_addresses";
public DataManager(Context context){
// create an instance of our internal CustomSQLiteOpenHelper class
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
db = helper.getWritableDatabase();
}
// insert a record
public void insert(String name, String age){
// build the query string
String query = "INSERT INTO " + TABLE_N_AND_A + " (" +
TABLE_ROW_NAME + ", " +
TABLE_ROW_AGE + ") " +
"VALUES (" +
"'" + name + "'" + ", " +
"'" + age + "'" +
");";
// log it for reference
Log.i("insert() = ", query);
// execute the query
db.execSQL(query);
}
// delete a record
public void delete(String name){
String query = "DELETE FROM " + TABLE_N_AND_A +
" WHERE " + TABLE_ROW_NAME +
" = '" + name + "';";
Log.i("delete() = ", query);
db.execSQL(query);
}
public Cursor selectAll() {
Cursor c = db.rawQuery("SELECT *" + " from " + TABLE_N_AND_A, null);
return c;
}
// search
public Cursor searchName(String name){
String query = "SELECT " +
TABLE_ROW_ID + ", " +
TABLE_ROW_NAME +
", " + TABLE_ROW_AGE +
" from " +
TABLE_N_AND_A + " WHERE " +
TABLE_ROW_NAME + " = '" + name + "';";
Log.i("searchName = ", query);
// execute the search and assign the results to a cursor, c
Cursor c = db.rawQuery(query, null);
return c;
}
// this class is created when our DataManager is initialised
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
public CustomSQLiteOpenHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
// this method only runs the first time the database is created
@Override
public void onCreate(SQLiteDatabase db){
Log.i("info", "DATABASE CREATED");
// create a table
String newTableQueryString = "create table "
+ TABLE_N_AND_A + " ("
+ TABLE_ROW_ID
+ " integer primary key autoincrement not null,"
+ TABLE_ROW_NAME
+ "text not null,"
+ TABLE_ROW_AGE
+ "text not null);";
db.execSQL(newTableQueryString);
}
// this method only runs when we increment the db version
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
}
但是,当我运行我的应用程序并尝试插入数据时,我收到一个错误:
android.database.sqlite.SQLiteException: table names_and_addresses has no column named name (code 1): , while compiling: INSERT INTO names_and_addresses (name, age) VALUES ('adfsd', '23423');
我不知道有什么方法可以检查数据库是如何设置的,或者如何删除数据库以便重新创建它。有什么想法吗?
【问题讨论】:
标签: android sqlite android-sqlite