これは私のデータベース アダプタ クラスです。これを使用して、データベースに接続してクエリを実行します。
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();
間違いがどこにあり、テーブルが作成されない理由を教えてもらえますか?