在ANDROID中清除应用程序数据
为什么每当我清除我的应用程序数据时,我以前工作的在ANDROID中清除应用程序数据,android,database,sqlite,Android,Database,Sqlite,为什么每当我清除我的应用程序数据时,我以前工作的SQLite数据库就不再工作了? 以下是我的数据库代码: package tsu.ccs.capstone; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.databas
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;
}
}
这是我的第一项活动:
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);
}
}
数据库是应用程序数据的一部分。你的数据库不再工作了,因为它已经不存在了。它与所有resp应用程序数据一起被删除。应用程序的每个SQLite数据库都表示为单个文件,清除应用程序数据时将删除此数据库文件 我可以找到数据库文件的位置以及如何使用它。您可以浏览设备上的文件系统并在那里找到它
将调试点放在SQLiteOpenHelper的onCreate()方法中,看看它是否被执行。我没有尝试过,但您似乎有一个包含私有DatabaseHelper类的DBUser类
当数据库不存在时,Android会自动调用onCreate方法,但我假设它隐藏在您的案例中。尝试将DatabaseHelper移到DBUser类之外。我认为当您清除应用程序数据时,应用程序的所有内部内存都将被清除。.是的。没错。但当我在清除应用程序数据后运行应用程序时,我的DB表似乎不存在/创建。我想知道为什么EAR应用程序数据也会清除应用程序创建的数据库。毕竟,数据库本质上是一段数据。在这种情况下,应该调用
onCreate
方法。是吗?在这种情况下你犯了什么错误?张贴日志。