0
我正在創建一個需要數據庫的應用程序。我使用sqlite數據庫瀏覽器創建了它,這意味着我創建的應用程序將我創建的數據庫導入到手機中。如何更新爲android創建的sqlite數據庫?
以及我以前學過的東西,如果我使用瀏覽器對數據庫做了任何更改,我必須從手機/模擬器中卸載應用程序,以便進行更改。
所以問題來了。如果我將應用程序上傳到市場後發生什麼情況,並意識到我需要更新表?另一個問題是用戶必須輸入一些數據並將其保存到數據庫的表中。
那麼有沒有辦法更新我的數據庫&還保留用戶在表中輸入的數據?
我的數據庫幫助代碼如下。請幫助...非常感謝!
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
}
在記事本教程中的例子,如果你還沒有找到他們沒有有用:http://developer.android.com/resources/tutorials/notepad/index.html要知道,你的onUpgrade函數只需使用標準SQL命令將列添加到現有表中,而不是像他們的示例那樣擦除整個事物。 – FixerMark
對於等待這麼久回覆感到抱歉,因爲我在回答問題之前就提前回答了問題,以便得到答案。問題是,我不知道如何處理它。我見過記事本教程,但不知何故,我沒有看到onUpgrade方法。 – Jovi