未创建Android SQLite数据库表 [英] Android SQLite database table not being created
问题描述
我正在学习一本针对初学者的Android书籍,并且正在尝试制作一个微型应用程序以了解有关使用SQLite数据库的信息.我建立了一个DataManager类,该类创建一个数据库并处理CRUD操作:
I'm following along in an Android for beginner's book and am trying to make a mini app to learn about using SQLite databases. I've built a DataManager class which creates a database and handles the CRUD operations:
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){
}
}
}
但是,当我运行我的应用并尝试插入数据时,出现错误:
However, when I run my app and attempt to insert data, I get an error:
android.database.sqlite.SQLiteException:表names_and_addresses没有名为name(代码1):的列,而在编译时:INSERT INTO names_and_addresses(name,age)VALUES('adfsd','23423');/code>
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');
我不知道有什么方法可以检查数据库的设置或删除数据库的方式,以便重新创建数据库.有什么想法吗?
I don't know of any way to check how the database has been setup, or how to drop the database so I can recreate it. Any ideas?
推荐答案
在文本"之前使用适当的间距,如下所示:
Use proper spacing before "text" like follows :
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);";
这篇关于未创建Android SQLite数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!