Tôi đang tạo một ứng dụng cần một cơ sở dữ liệu. Tôi tạo ra nó bằng cách sử dụng trình duyệt cơ sở dữ liệu sqlite, có nghĩa là các ứng dụng tôi tạo ra, nhập khẩu cơ sở dữ liệu tôi tạo ra vào điện thoại.Làm cách nào để nâng cấp cơ sở dữ liệu mà không xóa dữ liệu mà người dùng nhập vào cơ sở dữ liệu trước đó?
Ứng dụng tôi tạo yêu cầu người dùng nhập dữ liệu vào cơ sở dữ liệu. Khi nâng cấp cơ sở dữ liệu, tôi hy vọng giữ lại dữ liệu mà người dùng đã nhập.
cơ sở dữ liệu mã giúp đỡ của tôi là dưới đây:
public class DatabaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/test.test/databases/";
private static String DB_NAME = "TestDatabase";
private static final int DB_VERSION = 1;
private SQLiteDatabase myDatabase;
private final Context myContext;
/**
* # Constructor #
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}//constructor
/**
* # Create Database #
* Creates a empty database on the system and rewrites it with your own database.
*/
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if(dbExist)
{
//do nothing - database already exist
}//if
else
{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try
{
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}//catch
}//else
}//createDatabase
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch(SQLiteException e) {
//database does't exist yet.
}//catch
if(checkDB != null)
{
checkDB.close();
}//if
return checkDB != null ? true : false;
}//checkDatabase
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}//copyDatabase
// # open database #
public void openDatabase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}//openDatabase
@Override
public synchronized void close()
{
if(myDatabase != null)
myDatabase.close();
super.close();
}//close
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public List<String> selectData
(String tableName, String [] columns, String selection, String[] selectionArgs,
String groupBy, String having, String orderBy) {
List<String> list = new ArrayList<String>();
Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}//selectData
public void insertData (String tableName, String nullColumnHack, ContentValues values) {
try
{
myDatabase.insert(tableName, nullColumnHack, values);
} catch (Exception e) {
Log.e("Error :","unable to insert data");
}//catch
}//insertData
//edit row
public void updateData (String tableName, ContentValues values, String whereClause, String[] whereArgs) {
try
{
myDatabase.update(tableName, values, whereClause, whereArgs);
} catch (Exception e) {
Log.e("Error :","unable to update data");
}//catch
}//updateData
public void deleteRow (String tableName, String whereClause, String[] whereArgs) {
try
{
myDatabase.delete(tableName, whereClause, whereArgs);
} catch (Exception e) {
Log.e("Error :","unable to delete row");
}//catch
}//deleteRow
}
* lưu ý: cơ sở dữ liệu của tôi bao gồm nhiều hơn một bảng. Hai bảng yêu cầu đầu vào của người dùng. Những người khác thì không.
Tôi hy vọng rằng câu trả lời thực sự có thể được đưa ra, thay vì đưa ra trang web không có trong tình huống chính xác mà tôi có, vì tôi bị lẫn lộn dễ dàng.
xin lỗi ... tôi không thể hiểu truy vấn có trong cơ sở dữ liệu vì nó không nêu giá trị của nó là gì. – Jovi
Truy vấn gì? Bạn cần phải hiểu lược đồ của cơ sở dữ liệu để cập nhật nó lên phiên bản mới. –
xin lỗi, ý tôi là truy vấn được hiển thị trong ví dụ. (ví dụ: db.execSQL ("ALTER TABLE" + Tables.SESSIONS + "ADD COLUMN" + SessionsColumns.SESSION_FEEDBACK_URL + "TEXT"); phiên bản = VER_SESSION_FEEDBACK_URL;) làm định nghĩa cho ví dụ: Tables.Session chưa được đưa ra – Jovi