我正在尝试使用 SQLliteDatabase 创建一个基本数据库,但它一直给我这个错误。我被卡了很长时间,我想我犯的错误很简单,但我找不到。如果有人可以提供帮助,那就太好了
01-06 15:07:47.885: I/Database(1824): sqlite returned: error code = 1, msg = no such table: webpages
01-06 15:07:47.904: E/Database(1824): Error inserting id=1 phone=10001 name=Ivan
01-06 15:07:47.904: E/Database(1824): android.database.sqlite.SQLiteException: no such table: webpages: , while compiling: INSERT INTO webpages(id, phone, name) VALUES(?, ?, ?);
这是我使用的代码
public class DatabaseHandler extends SQLiteOpenHelper {
/**
* The name of the database
*/
private static final String DATABASE_NAME = "Weboff.db";
/**
* The name of the (only) table.
*/
private static final String TABLE_NAME = "webpages";
/**
* The name of the first column (ID)
*/
private static final String COL_ID = "id";
/**
* The name of the second column (NAME)
*/
private static final String COL_NAME = "name";
/**
* The name of the third column (PHONENUMBER)
*/
private static final String COL_PHONE = "phone";
/**
* A constructor which builds a DatabaseHandler object. Note that calling
* the constructor does not create a database. This does not happen until
* the first call to getReadableDatabase() or getWriteableDatabase()
*
* @param context
* In this case, a reference to LecturerActivity
*/
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
/**
* This method is called when the database is created for the first time.
* This is where the creation of tables and the initial population of the
* tables should happen.
*/
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT,"
+ COL_PHONE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
/**
* Called when the database needs to be upgraded. Only relevant when you
* have multiple versions of the database scheme in play.
*
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldNum, int newNum) {
// Drop older table if exist and create fresh
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
/**
* Use this method to add a Lecturer to the database.
*
* @param lecturer
* the Lecturer you want to add
*/
public void addLecturer(Lecturer lecturer) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_ID, lecturer.getID());
values.put(COL_NAME, lecturer.getName());
values.put(COL_PHONE, lecturer.getPhoneNumber());
db.insert(TABLE_NAME, null, values);
db.close();
}
/**
* Use this method to get all of the Lecturers in the database.
*
* @return a list of Lecturer objects, one per row
*/
public List<Lecturer> getAll() {
List<Lecturer> list = new ArrayList<Lecturer>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Lecturer lecturer = new Lecturer(cursor.getInt(0),
cursor.getString(1), cursor.getString(2));
list.add(lecturer);
} while (cursor.moveToNext());
}
return list;
}
/**
* Use this method to remove all of the Lecturers from the database. This is
* useful when experimenting. After dropping all tables, the initial state
* of the database is re-created.
*/
public void removeAll() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
/**
* This method removes one lecturer from the database.
*
* @param lecturer
* the Lecturer to remove
*/
public void deleteLecturer(Lecturer lecturer) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COL_ID + " = ?",
new String[] { String.valueOf(lecturer.getID()) });
db.close();
}
/**
* This method updates the data stored in the database for one Lecturer.
*
* @param lecturer
* the Lecturer to update
* @return the number of rows affected
*/
public int updateLecturer(Lecturer lecturer) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_NAME, lecturer.getName());
values.put(COL_PHONE, lecturer.getPhoneNumber());
return db.update(TABLE_NAME, values, COL_ID + " = ?",
new String[] { String.valueOf(lecturer.getID()) });
}
/**
* This method gets a single Lecturer from the database, using the ID field
* as a key
* ;
* @param id
* the ID of the Lecturer we want
* @return a Lecturer object
*/
public Lecturer getLecturer(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { COL_ID, COL_NAME,
COL_PHONE }, COL_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Lecturer lecturer = new Lecturer(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return lecturer;
}
}
public class Lecturer {
/**
* The ID assigned to the lecturer. Starts at 1.
*/
private int ID;
/**
* The full name of the lecturer.
*/
private String name;
/**
* The internal phone number of the lecturer. 4 digits.
*/
private String phoneNumber;
/** Builds a new Lecturer object
*
* @param ID The ID assigned to the lecturer.
* @param name The full name of the lecturer
* @param phoneNumber The internal phone number of the lecturer
*/
public Lecturer(int ID, String name, String phoneNumber){
this.ID = ID;
this.name = name;
this.phoneNumber = phoneNumber;
}
/**
* Get the full name of the lecturer
*
* @return the lecturers full name
*/
public String getName(){
return this.name;
}
/**
* Set the name of the lecturer
*
* @param name the lecturers full name
*/
public void setName(String name){
this.name = name;
}
/**
* Get the phone number of the lecturer
*
* @return the lecturers full name
*/
public String getPhoneNumber(){
return this.phoneNumber;
}
/**
* Set the phone number of the lecturer
*
* @param name the lecturers phone number
*/
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
/**
* Get the ID of the lecturer
*
* @return the lecturers ID
*/
public int getID() {
return ID;
}
/**
* Set the ID of the lecturer
*
* @param name the lecturers ID
*/
public void setID(int iD) {
ID = iD;
}
}
And in the onCreate method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
regularExpresionStrings();
dh = new DatabaseHandler(this);
dh.addLecturer(new Lecturer(1, "Ivan", "10001"));
Log.d("Database: ", "Listing all lecturers..");
List<Lecturer> list = dh.getAll();
for (Lecturer lr : list) {
String log = "ID:" + lr.getID() +" Name: " + lr.getName() + " Phone: " + lr.getPhoneNumber();
Log.d("Database: ", log);
}