アプリのデータを消去すると、以前は機能していたSQLite
データベースが機能しなくなるのはなぜですか? ここに私のデータベースのコードがあります:
package tsu.ccs.capstone;
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 DBUser {
public static final String KEY_ROWID = "_id";
public static String KEY_USERNAME= "username";
public static String KEY_PASSWORD = "password";
public static String KEY_NUMBER = "number";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "usersdb";
private static final String DATABASE_TABLE = "users";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table users (_id integer primary key autoincrement, "
+ "username text not null, "
+ "password text not null, "
+ "number text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBUser(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);
}
//creating the DB
@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 users");
onCreate(db);
}
}
//open DB for writing/reading mode
public void open() throws SQLException
{
db = DBHelper.getWritableDatabase();
}
//close the DB
public void close()
{
DBHelper.close();
}
//insert Initial Values to the DB
public long AddUser()
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, "admin");
initialValues.put(KEY_PASSWORD, "admin");
return db.insert(DATABASE_TABLE, null, initialValues);
}
//delete the entire rows
public long DeleteUser()
{
return db.delete(DATABASE_TABLE, "1", null);
}
public boolean CheckContent()
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE _id=1", null);
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
//update the DB
public boolean UpdateUser(long rowId, String username, String password)
{
ContentValues newCon = new ContentValues();
newCon.put(KEY_USERNAME, username);
newCon.put(KEY_PASSWORD, password);
return db.update(DATABASE_TABLE, newCon, KEY_ROWID + "=" + rowId, null) > 0;
}
// searching the DB for username and password match
public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return true;
}
//Store Recipient's Number to the DB
public boolean EditNumber(long rowId, String number)
{
ContentValues editNumber = new ContentValues();
editNumber.put(KEY_NUMBER, number);
return db.update(DATABASE_TABLE, editNumber, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor newNumber() {
String query = "SELECT * FROM " + DATABASE_TABLE + " WHERE _id=1";
System.out.println(query);
Cursor cur = db.rawQuery(query, null);
cur.moveToFirst();
return cur;
}
}
ここに私のFirstActivityがあります:
package tsu.ccs.capstone;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
public class FirstActivity extends Activity {
final String PREFS_NAME = "FirstTimeCheker";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
DBUser dbUser = new DBUser(FirstActivity.this);
dbUser.open();
if(dbUser.CheckContent()){
//do nothing
} else {
dbUser.AddUser();
}
dbUser.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_first, menu);
return true;
}
public void gotoLogin (View v) {
SharedPreferences mPrefs;
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean RegisterScreenShown = mPrefs.getBoolean(PREFS_NAME, false);
if (!RegisterScreenShown) {
Intent intentIntro = new Intent(this, RegisterActivity.class);
startActivity(intentIntro);
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(PREFS_NAME, true);
editor.commit(); // Very important to save the preference
}else{
Intent intentAlecc = new Intent(this, AleccActivity.class);
startActivity(intentAlecc);
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
}
}
public void gotoCctv (View v) {
Intent intent = new Intent(this, CctvActivity.class);
startActivity(intent);
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
}
public void editUser (View v) {
Intent intent = new Intent(this, EditUserActivity.class);
startActivity(intent);
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition (R.anim.incoming, R.anim.outgoing);
}
}