如何更新为android创建的sqlite数据库?

如何更新为android创建的sqlite数据库?,android,database,Android,Database,我正在创建一个需要数据库的应用程序。我使用sqlite数据库浏览器创建了它,这意味着我创建的应用程序会将我创建的数据库导入手机 我以前学到的是,如果我使用浏览器对数据库做了任何更改,我必须从我的手机/模拟器中卸载应用程序,以便它进行更改 所以问题来了。如果我将应用程序上传到市场,并意识到我需要更新表格,会发生什么情况???另外,另一个问题是用户必须输入一些数据并将其保存到数据库的表中 那么有没有办法更新我的数据库&同时保留用户在表中输入的数据 我的数据库助手代码如下。请帮忙…非常感谢 publi

我正在创建一个需要数据库的应用程序。我使用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
}
public类DatabaseHelper扩展了SQLiteOpenHelper{
//应用程序数据库的Android默认系统路径。
私有静态字符串DB_PATH=“/data/data/test.test/databases/”;
私有静态字符串DB_NAME=“TestDatabase”;
私有静态最终int DB_版本=1;
私有SQLiteDatabase-myDatabase;
私有最终上下文myContext;
/**
*#构造函数#
*获取并保留传递的上下文的引用,以便访问应用程序资产和资源。
*@param上下文
*/
公共数据库助手(上下文){
super(上下文,数据库名称,空,数据库版本);
this.myContext=上下文;
}//建造师
/**
*#创建数据库#
*在系统上创建一个空数据库,并用您自己的数据库重写它。
*/
public void createDatabase()引发IOException{
布尔值dbExist=checkDatabase();
if(dbExist)
{
//不执行任何操作-数据库已存在
}//如果
其他的
{
//通过调用此方法,将在默认系统路径中创建空数据库
//所以我们可以用我们的数据库覆盖那个数据库。
这是.getReadableDatabase();
尝试
{
copyDatabase();
}捕获(IOE异常){
抛出新错误(“复制数据库时出错”);
}//抓住
}//否则
}//创建数据库
私有布尔校验数据库(){
SQLiteDatabase checkDB=null;
尝试
{
字符串myPath=DB_PATH+DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READONLY);
}catch(sqlitee异常){
//数据库还不存在。
}//抓住
if(checkDB!=null)
{ 
checkDB.close();
}//如果
return checkDB!=null?true:false;
}//检查数据库
私有void copyDatabase()引发IOException{
//打开本地数据库作为输入流
InputStream myInput=myContext.getAssets().open(DB_NAME);
//刚创建的空数据库的路径
字符串outFileName=DB_路径+DB_名称;
//打开空数据库作为输出流
OutputStream myOutput=新文件OutputStream(outFileName);
//将字节从输入文件传输到输出文件
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=myInput.read(缓冲区))>0)
{
写入(缓冲区,0,长度);
}
//关闭溪流
myOutput.flush();
myOutput.close();
myInput.close();
}//复制数据库
//#开放式数据库#
public void openDatabase()引发SQLException{
//打开数据库
字符串myPath=DB_PATH+DB_NAME;
myDatabase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READWRITE);
}//开放数据库
@凌驾
公共同步作废关闭()
{
if(myDatabase!=null)
myDatabase.close();
super.close();
}//接近
@凌驾
public void onCreate(SQLiteDatabase db){
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
}
公共列表选择数据
(字符串tableName、字符串[]列、字符串选择、字符串[]selectionArgs、,
字符串groupBy、字符串have、字符串orderBy){
列表=新的ArrayList();
Cursor Cursor=this.myDatabase.query(tableName、columns、selection、selectionArgs、groupBy、having、orderBy);
if(cursor.moveToFirst())
{
做
{
list.add(cursor.getString(0));
}
while(cursor.moveToNext());
}
if(cursor!=null&!cursor.isClosed())
{
cursor.close();
}
退货清单;
}//选择数据
公共void insertData(字符串tableName、字符串nullColumnHack、ContentValues){
尝试
{
插入(tableName,nullColumnHack,value);
}捕获(例外e){
Log.e(“错误:,“无法插入数据”);
}//抓住
}//插入数据
//编辑行
public void updateData(字符串tableName、ContentValues值、字符串where子句、字符串[]wherergs){
尝试
{
myDatabase.update(tableName、values、where子句、where rgs);
}捕获(例外e){
Log.e(“错误:,“无法更新数据”);
}//抓住
}//更新数据
public void deleteRow(字符串tableName、字符串where子句、字符串[]wherergs){
尝试
{
删除(tableName,whereClause,wherergs);
}捕获(例外e){
Log.e(“错误:,“无法删除行”);
}//抓住
}//删除行
}
就是这样:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
这是给你的


记事本教程中的示例在您还没有找到它们的情况下非常有用:请注意,您的onUpgrade函数可以使用标准SQL命令将列添加到现有表中,而不是像示例中那样擦除整个内容。很抱歉,等待了这么久才回复,因为我很早就问了这个问题,所以我想在做之前先得到一些答案。问题是,我不知道如何处理它。我看过记事本教程,但不知怎么的,我没有看到onUpgrade方法。