清除ANDROID中的应用数据

时间:2013-02-05 08:51:01

标签: 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.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);
}
}

2 个答案:

答案 0 :(得分:1)

数据库是应用程序数据的一部分。您的数据库不再起作用,因为它不再存在。它将与所有resp应用程序数据一起删除。应用程序的每个SQLite数据库都表示为单个文件,清除应用程序数据时将删除此数据库文件。

this blog post您可以找到数据库文件的位置以及如何使用它。您可以在设备上浏览文件系统并在那里找到它。

将调试点放在SQLiteOpenHelper的onCreate()方法中,看看它是否被执行。

答案 1 :(得分:0)

我没有尝试过,但你似乎有一个DBUser类,它包含了私有的DatabaseHelper类。

当数据库不存在时,Android会自动调用onCreate方法,但我认为它隐藏在你的情况下。尝试将DatabaseHelper移到DBUser类之外。