我在下面发布的代码是我在Android应用程序中用于数据库的代码。我的程序目前仍在我的计算机上并与模拟器一起运行。我的问题是,如果有人从谷歌播放下载我的程序,整个数据库的工作将如何运作?他们的手机会有自己的DATABASE_NAME和DATABASE_TABLE吗?在这种情况下,我不必担心任何事情。或者DATABASE_NAME和DATABASE_TABLE是否需要针对下载程序并在应用程序上使用数据库的每个用户有所不同?在这种情况下,我不确定如何为每个用户设置数据库。任何帮助表示赞赏。感谢...
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_DE = "de";
public static final String KEY_MO = "mo";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_DESTINATION, KEY_MONTH};
// DataBase info:
public static final String DATABASE_NAME = "Prog.db";
public static final String DATABASE_TABLE = "prog_table";
//IF ANY CHANGES ARE DONE HERE THE VERSION NEEDS TO BE INCREMENTED OTHERWISE THE PROGRAM WILL CRASH
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DE + " TEXT NOT NULL, " + KEY_MO + " TEXT " ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
//initializing the DatabaseHelper
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertData(String de, String mo) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DE, de);
initialValues.put(KEY_MO, mo);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
}
答案 0 :(得分:1)
他们的手机会有自己的DATABASE_NAME和DATABASE_TABLE吗?
中的是
对于下载程序并在应用程序上使用数据库的每个用户,DATABASE_NAME和DATABASE_TABLE是否需要不同?
-NO
当您的应用程序安装到手机时,它将根据您的配置创建自己的数据库。您无需担心配置本身。它会像你的模拟器那样运行良好。
提示: 您实际上可以使用手机作为模拟器。当您使用采用者将其插入计算机时,您的开发环境应检测到它。例如。蚀