我正在開發APP1和APP2兩個Android應用程序。並且我在APP1的「UserLogin」表中輸入值,並且想要在APP2中獲取「UserLogin」表的數據。但是當我提取值時它給我錯誤UserLogin表不存在。我如何能在APP 2如何從Android數據庫的現有表中獲取數據
獲取數據,這裏是我的數據庫輔助類
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String LOG = "DatabaseHelper";
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "MessengerDb";
// Table Names
private static final String TABLE_USER_LOGIN= "UserLogin";
private static final String TABLE_CHAT_MESSAGE_THREAD = "ChatMessageThread";
private static final String TABLE_CHAT_MESSAGE = "ChatMessage";
//Login table column name
private static final String KEY_ID = "id";
private static final String KEY_LoginUserInfoId = "LoginUSerInfoId";
private static final String KEY_LoginUserFullName = "LoginUserFullName";
private static final String KEY_LoginUserAvatarURL = "LoginUserAvatarURL";
private static final String KEY_LoginStatus = "LoginStatus";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL(CREATE_TABLE_USER_LOGIN);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("Database updated", "Database Updated");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER_LOGIN);
// create new tables
// onCreate(db);
}
//-----------------Get login user--------------//
public List<UserLogin> getLoginUser() {
List<UserLogin> loginUser = new ArrayList<UserLogin>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USER_LOGIN, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
UserLogin uL = new UserLogin();
uL.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_ID))));
uL.setLoginUserInfoId(cursor.getString(cursor.getColumnIndex(KEY_LoginUserInfoId)));
uL.setLoginUserFullName(cursor.getString(cursor.getColumnIndex(KEY_LoginUserFullName)));
uL.setLoginUserAvatarURL(cursor.getString(cursor.getColumnIndex(KEY_LoginUserAvatarURL)));
uL.setLoginStatus(cursor.getString(cursor.getColumnIndex(KEY_LoginStatus)));
// Adding contact to list
loginUser.add(uL);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return loginUser;
}
}
你爲什麼評論這個? //db.execSQL(CREATE_TABLE_USER_LOGIN); – Amsheer
在哪裏創建'TABLE_USER_LOGIN'表? –
APP1和APP2是兩個獨立的應用程序嗎?也就是說,每個都有自己的軟件包名稱並單獨安裝?如果是這樣,你不能只從另一個應用程序的數據庫訪問(想象安全問題!)。您必須通過['ContentProvider'](http://developer.android.com/guide/topics/providers/content-providers.html)向APP1公開APP2中的數據。 –