这是我的数据库适配器类。我用它来连接和查询数据库。
package com.Coursework.AppointmentRecorder;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String APP_TITLE = "title";
public static final String APP_DATE = "date";
public static final String APP_TIME = "time";
public static final String APP_DETAILS = "details";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "NewUser";
private static final String DATABASE_TABLE = "Appointment";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"CREATE TABLE Appointment (_id integer primary key autoincrement, "
+ "title text not null, date text not null, time text not null, "
+ "details text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close() {
DBHelper.close();
}
//---insert a title into the database---
public long insertAppointment(String title,String Date, String time, String details) {
ContentValues initialValues = new ContentValues();
initialValues.put(APP_TITLE, title);
initialValues.put(APP_DATE, Date);
initialValues.put(APP_TIME, time);
initialValues.put(APP_DETAILS, details);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllTitles() {
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
APP_TITLE,
APP_TIME,
APP_DETAILS
},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long date) throws SQLException {
Cursor mCursor = db.query(false, DATABASE_TABLE, new String[] {
KEY_ROWID,
APP_TITLE,
APP_TIME,
APP_DETAILS
},
APP_DATE + "=" + date,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
do {
} while (mCursor.moveToNext());
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long rowId, String isbn, String title, String publisher) {
ContentValues args = new ContentValues();
args.put(APP_TITLE, isbn);
args.put(APP_TIME, title);
args.put(APP_DETAILS, publisher);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
我遇到的问题是,当我在另一个类中创建一个条目时,通过首先打开数据库,我称之为:
final DBAdapter db = new DBAdapter(this);
然后我尝试执行这个命令,但目录一直说没有这样的表并给我一个 SQL 异常。
db.open();
long id = db.insertAppointment(Title.getText().toString(),
String.valueOf(SDate),
Jack.getText().toString(),
details.getText().toString()
);
alertDialog.setTitle("Appoinment Manager");
alertDialog.setMessage("The Appointment Has Been Created.");
alertDialog.show();
db.close();
有人可以告诉我错误在哪里以及为什么没有创建表吗?