解决方案解决了它.
我编写了一个 Android应用程序,在我调试它几次后有一个sqlite数据库
db中的oncreate方法没有被调用(即使之前一切正常)
我将db版本号从1更改为2后,一切正常
即使我通过应用程序管理器卸载了应用程序,也删除了缓存
本地数据库信息.
我的问题如下 – 本地数据库数据是否保存在其他地方?
如果没有 – 为什么它只在我升级版本号后才有效
甚至当我删除所有与应用相关的数据时?
/** * A class to handle sqlite reads/writes of user related data to be collected */ public class UserDataManager extends sqliteOpenHelper { // Class Variables private final String TAG = UserDataManager.class.getSimpleName(); // Database Version private static final int DATABASE_VERSION = 1; // Database Name public static final String DATABASE_NAME = "tmc"; // Tables private static final String TABLE_USER = "user"; // Tables and table columns names private String CREATE_USER_TABLE; private static final String COLUMN_USER_ID = "user_id"; private static final String COLUMN_USER_MAIL = "email"; private static final String COLUMN_USER_ACTIVE = "user_active"; private static final String COLUMN_USER_NAME = "name"; private static final String COLUMN_USER_PASSWORD = "password"; private static final String COLUMN_USER_PHONE_NUMBER = "phone_number"; /** * Class constructor * * @param context * The context to run in */ public UserDataManager(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } // Creating Tables @Override public void onCreate(sqliteDatabase db) { CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + " (" + COLUMN_USER_ID + " INTEGER PRIMARY KEY NOT NULL," + COLUMN_USER_MAIL + " VARCHAR(64) NOT NULL," + COLUMN_USER_NAME + " VARCHAR(64) NOT NULL," + COLUMN_USER_PASSWORD + " VARCHAR(64) NOT NULL," + COLUMN_USER_PHONE_NUMBER + " VARCHAR(64) NOT NULL," + COLUMN_USER_ACTIVE + " INT NOT NULL);"; // create the tables db.execsql(CREATE_USER_TABLE); } // Upgrading database @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // Drop older table if existed db.execsql("DROP TABLE IF EXISTS " + TABLE_USER); // Create tables again onCreate(db); } /** * Adding a user to the database * * @param userId * The created user id * @param userName * The user name * @param userEmail * The user email * @param userPassword * The user password * @param userPhoneNumber * The user phone number * @param isActive * Set to 1 if the user is active 0 otherwise * @return True if the user added successfully false otherwise */ public boolean AddUser(int userId,String userName,String userEmail,String userPassword,String userPhoneNumber,boolean isActive) { // method variables long rowId; boolean pass = false; int active = isActive ? 1 : 0; sqliteDatabase db = null; ContentValues row = null; // try to add the user to the db try { row = new ContentValues(); db = this.getWritableDatabase(); db.delete(TABLE_USER,null); row.put(COLUMN_USER_ID,userId); row.put(COLUMN_USER_NAME,userName); row.put(COLUMN_USER_MAIL,userEmail); row.put(COLUMN_USER_PASSWORD,userPassword); row.put(COLUMN_USER_CAR_NUMBER,userPhoneNumber); row.put(COLUMN_USER_ACTIVE,active); rowId = db.insert(TABLE_USER,row); if (rowId > -1) { pass = true; } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (db != null) { // close database connection db.close(); } } return pass; } /** * Get the current registered user * * @return The id of the column of the registered user */ public int GetRegisteredUserId() { // method variables int columnIndex = -1; int userId = -1; sqliteDatabase db = null; Cursor cursor = null; // try to get the user from the database try { db = this.getReadableDatabase(); cursor = db.query(TABLE_USER,new String[] { COLUMN_USER_ID },null); if (cursor != null) { boolean moved = cursor.moveToFirst(); if (moved) { columnIndex = cursor.getColumnIndex(COLUMN_USER_ID); if (columnIndex > -1) { userId = cursor.getInt(columnIndex); } } } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (cursor != null) // release cursor cursor.close(); if (db != null) // close database connection db.close(); } return userId; } /** * Get the current user email * * @return The id of the column of the registered user */ public String GetRegisteredUserEmail() { // method variables int columnIndex = -1; String userEmail = null; sqliteDatabase db = null; Cursor cursor = null; // try to get the user from the database try { db = this.getReadableDatabase(); cursor = db.query(TABLE_USER,new String[] { COLUMN_USER_MAIL },null); if (cursor != null) { boolean moved = cursor.moveToFirst(); if (moved) { columnIndex = cursor.getColumnIndex(COLUMN_USER_MAIL); if (columnIndex > -1) { userEmail = cursor.getString(columnIndex); } } } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (cursor != null) // release cursor cursor.close(); if (db != null) // close database connection db.close(); } return userEmail; } /** * Get the current user password * * @return The password of the current logged user */ public String GetRegisteredUserPassword() { // method variables int columnIndex = -1; String userPassword = null; sqliteDatabase db = null; Cursor cursor = null; // try to get the user from the database try { db = this.getReadableDatabase(); cursor = db.query(TABLE_USER,new String[] { COLUMN_USER_PASSWORD },null); if (cursor != null) { boolean moved = cursor.moveToFirst(); if (moved) { columnIndex = cursor.getColumnIndex(COLUMN_USER_PASSWORD); if (columnIndex > -1) { userPassword = cursor.getString(columnIndex); } } } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (cursor != null) // release cursor cursor.close(); if (db != null) // close database connection db.close(); } return userPassword; } /** * Get number of rows in the user table * * @return the number of the rows in the user table (How many users are * saved in the DB) */ public int GetRowCount() { // method variables int rowsCount = 0; sqliteDatabase db = null; Cursor cursor = null; // try to get the user from the database try { db = this.getReadableDatabase(); cursor = db.query(TABLE_USER,null); if (cursor != null) { boolean moved = cursor.moveToFirst(); if (moved) { do { rowsCount++; } while (cursor.moveToNext()); } } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (cursor != null) // release cursor cursor.close(); if (db != null) // close database connection db.close(); } return rowsCount; } /** * Remove a user from the database * * @param userId * The user id */ public void logoutUser() { // method variables sqliteDatabase db = null; // try to remove a user from the database try { db = this.getWritableDatabase(); onUpgrade(db,DATABASE_VERSION,DATABASE_VERSION); } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (db != null) { // close database connection db.close(); } } } /** * Set a user to be active or not * * @param isActive * 1 if the cigarette is active 0 otherwise * @return True if the cigarette active field has changed false otherwise */ public boolean SetUserActive(boolean isActive) { // method variables int rowsAffected; int active = isActive ? 1 : 0; long userId; String userIdString; boolean pass = true; sqliteDatabase db = null; ContentValues values = null; // try to remove a device from the database try { userId = GetRegisteredUserId(); if (userId > -1) { userIdString = String.valueOf(userId); db = this.getWritableDatabase(); values = new ContentValues(); values.put(COLUMN_USER_ACTIVE,active); rowsAffected = db.update(TABLE_USER,values,COLUMN_USER_ID + " = ?",new String[] { userIdString }); if (rowsAffected != 1) { pass = false; } } } catch (sqlException exception) { Log.e(TAG,exception.getMessage()); } finally { if (db != null) { // close database connection db.close(); } } return pass; } }
笔记 –
1.请注意我的设备已植根,所以在将数据插入数据库后我将更改数据库的777权限,以便我可以从手机中取出它以查看其中的内容(即查询是否通过)
2.抛出的错误是“android.database.sqlite.sqliteException:no such table:user”
巧克力饼干将被授予任何答案… =)
解决方法@H_502_21@
为什么只有在我升级了版本号之后它才有效,即使我删除了所有与应用程序相关的数据?
>一旦开始使用getReadableDatabase(),getWriteableDatabase()或任何其他sqliteHelper类代码.第一个方法调用是onCreate(sqliteDatabase db),它在应用程序数据库路径下创建数据库
/ data / data / PACKAGE_NAME / databases / tmc(在您的情况下).
>如果在sqliteHelper中修改数据库结构,则调用的第一个方法是onUpgrage(),它检查Database_Version是否被修改.如果是,那么它将执行onUpgrade(),其中包含一系列DROP TABLE IF EXIST,然后是onCreate(),它将通过替换以前的数据库文件再次在应用程序路径下创建具有新结构的数据库.
>使用Application Manager清除缓存数据确实清除了该应用程序的数据库和缓存数据.但sqliteHelper确实检查了新旧的Database_Version.如果新的大于旧的.它调用onUpgrage()后跟onCreate().
>当您打算将数据库与Android应用程序一起使用时,它会在/ data / data / PACKAGE_NAME / databases / tmc下存储应用程序进程安全性.除非您已经拥有已安装的Android设备,否则无法访问数据库文件.
您可以创建开发人员选项或任何您喜欢的内容,以便将数据库从您的应用程序进程拉到SD卡以获取无根设备.
将数据库文件从应用程序进程路径复制到SD卡,以获取无根设备.
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/data/" + getPackageName() + "/databases/ZnameDB"; //Your DATABASE_NAME
String backupDBPath = "ZnameDB_Dev.db"; //DATABASE_COPY_NAME UNDER SDCARD
File currentDB = new File(currentDBPath);
File backupDB = new File(sd,backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src,src.size());
src.close();
dst.close();
Toast.makeText(SettingsActivity.this,"Database Transfered!",Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Log.e(TAG,e.toString());
}
>一旦开始使用getReadableDatabase(),getWriteableDatabase()或任何其他sqliteHelper类代码.第一个方法调用是onCreate(sqliteDatabase db),它在应用程序数据库路径下创建数据库
/ data / data / PACKAGE_NAME / databases / tmc(在您的情况下).
>如果在sqliteHelper中修改数据库结构,则调用的第一个方法是onUpgrage(),它检查Database_Version是否被修改.如果是,那么它将执行onUpgrade(),其中包含一系列DROP TABLE IF EXIST,然后是onCreate(),它将通过替换以前的数据库文件再次在应用程序路径下创建具有新结构的数据库.
>使用Application Manager清除缓存数据确实清除了该应用程序的数据库和缓存数据.但sqliteHelper确实检查了新旧的Database_Version.如果新的大于旧的.它调用onUpgrage()后跟onCreate().
>当您打算将数据库与Android应用程序一起使用时,它会在/ data / data / PACKAGE_NAME / databases / tmc下存储应用程序进程安全性.除非您已经拥有已安装的Android设备,否则无法访问数据库文件.
您可以创建开发人员选项或任何您喜欢的内容,以便将数据库从您的应用程序进程拉到SD卡以获取无根设备.
将数据库文件从应用程序进程路径复制到SD卡,以获取无根设备.
try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "/data/data/" + getPackageName() + "/databases/ZnameDB"; //Your DATABASE_NAME String backupDBPath = "ZnameDB_Dev.db"; //DATABASE_COPY_NAME UNDER SDCARD File currentDB = new File(currentDBPath); File backupDB = new File(sd,backupDBPath); if (currentDB.exists()) { FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src,src.size()); src.close(); dst.close(); Toast.makeText(SettingsActivity.this,"Database Transfered!",Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { Log.e(TAG,e.toString()); }