简体   繁体   English

没有调用Android SQLite onUpgrade

[英]Android SQLite onUpgrade not called

I have my database created in event onCreate , in which I have a lot of tables, but I need add 1 more table, and I can't lose any data, So I need to use the event onUpgrade , So I hope you guys help me because I don't know how to use it. 我在事件onCreate创建了我的数据库,其中我有很多表,但我需要再添加1个表,我不能丢失任何数据,所以我需要使用onUpgrade事件,所以我希望你们帮助我们我,因为我不知道如何使用它。

Example : 示例:

public void onCreate(SQLiteDatabase db) {
    sql = "CREATE TABLE IF NOT EXISTS funcionarios"
            +"(codigo INTEGER PRIMARY KEY, funcionario TEXT, apelido TEXT , functionTEXT, cartao TEXT , foto TEXT , tipo_foto TEXT);";  
    db.execSQL(sql);
}

what i need is 我需要的是什么

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion < 2){
        db.execSQL("CREATE TABLE IF NOT EXISTS calibrar_aceleracao"+
                 "(limiteMaximo INTEGER, limiteMinimo INTEGER);");
    }
}

but it doesn't work. 但它不起作用。

thanks. 谢谢。

The method onUpgrade is called when your version database is incremented. 当您的版本数据库递增时,将调用onUpgrade方法。 Verify in your class where you define your database version and increment this value. 在您的类中验证您定义数据库版本的位置并增加此值。

Run application. 运行应用程序 Your method onUpgrade is called. 调用onUpgrade方法。

This is not the way onUpgrade works .This is a method which will be called when you release some new version of your application and make it available for download in google play(and which may be requiring some updations to the database of the application already installed on users' devices').For your problem's solution Just add the query of CREATE TABLE IF NOT EXISTS in your onCreate() as you did for the creation of the other table in your onCreate() method already 这不是onUpgrade工作方式 。这是一种方法,当你发布一些新版本的应用程序并在google play中下载它时可以调用它(这可能需要对已经安装的应用程序的数据库进行一些更新)在用户的设备上)。对于您的问题的解决方案只需在onCreate()中添加CREATE TABLE IF NOT EXISTS的查询,就像在onCreate()方法中创建另一个表一样

public void onCreate(SQLiteDatabase db) {
    sql = "CREATE TABLE IF NOT EXISTS funcionarios"
            +"(codigo INTEGER PRIMARY KEY, funcionario TEXT, apelido TEXT , functionTEXT, cartao TEXT , foto TEXT , tipo_foto TEXT);"; 
 ///HERE YOUR Create Table QUERY and call db.execSQL

    db.execSQL(sql);
}

For onUpgrade to get called you must increase the database version that you supply to the SqliteOpenHelper implementation constructor. 要调用onUpgrade ,必须增加提供给SqliteOpenHelper实现构造函数的数据库版本。 Use a field in your class to store the same and increment it when you change your database schema. 使用类中的字段存储相同的字段,并在更改数据库模式时将其递增。

You do not need to change you applications version to update your database - not saying it is incorrect but there is a more efficient way of doing it. 不需要改变你的应用程序版本更新数据库-不是说这是不正确,但有这样做的更有效的方式。 And that is through the use of the super's constructor of your helper it would look something like the following: 这是通过使用你的助手的超级构造函数,它看起来像下面这样:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    public MyDatabaseHelper(Context context) {
        super(context, "My.db", null, 1 /* This is the version of the database*/);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        sql = "CREATE TABLE IF NOT EXISTS funcionarios (codigo INTEGER PRIMARY KEY, funcionario TEXT, apelido TEXT , functionTEXT, cartao TEXT , foto TEXT , tipo_foto TEXT);";  
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        if(oldVersion < 2){
             db.execSQL("CREATE TABLE IF NOT EXISTS calibrar_aceleracao (limiteMaximo INTEGER, limiteMinimo INTEGER);");
        }
    }
}

The method is onUpgrade is not being called probably because there are errors in the sql code, for example in the onCreate: 该方法是onUpgrade未被调用可能是因为sql代码中存在错误,例如在onCreate中:

functionTEXT

is missing a space before TEXT. 在TEXT之前缺少一个空格。

Also after, 之后,

calibrar_aceleracao"+ "(limiteMaximo

is missing a space before the bracket. 在括号前缺少一个空格。

I had the same problem, I was caching the SQLiteException so I didn't see the error was there. 我遇到了同样的问题,我正在缓存SQLiteException,所以我没有看到错误存在。

Put some logcat at the beginning and at the end of the method body and you'll see where the error is. 将一些logcat放在方法体的开头和末尾,你会看到错误的位置。

EDIT: another thing, why did you put that if? 编辑:另一件事,你为什么这么说?

if (oldVersion < 2)

It's not necessary at all. 根本没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM