不在onUpgrade中调用SQLiteOpenHelper的onCreate()方法,也不使用getReadableDatabase()

时间:2016-12-02 11:56:15

标签: android sqlite android-sqlite

这是我的数据库助手类的一部分

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Permission;

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Insert a new permission.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to insert permission for.
   * @param value User or group e-mail address, domain name or {@code null}
                  "default" type.
   * @param type The value "user", "group", "domain" or "default".
   * @param role The value "owner", "writer" or "reader".
   * @return The inserted permission if successful, {@code null} otherwise.
   */
  private static Permission insertPermission(Drive service, String fileId,
      String value, String type, String role) {
    Permission newPermission = new Permission();

    newPermission.setValue(value);
    newPermission.setType(type);
    newPermission.setRole(role);
    try {
      return service.permissions().insert(fileId, newPermission).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
    return null;
  }

  // ...

}

当我更改数据库版本时,只调用OnUpgrade(),在其中调用Oncreate()不起作用。 我尝试过使用getReadableDatabase()和getWritabledatabase(),但它也不起作用 我尝试卸载应用程序,清除数据但没有任何工作。 请帮忙

3 个答案:

答案 0 :(得分:1)

在创建数据库帮助程序类的实例时,第一次在sqlite onCreate中调用一次。和新版本更旧版本时的onUpgrade方法调用然后是旧版本(newVersion> oldVersion);

答案 1 :(得分:1)

如果数据库已经创建,则调用onCreate()将不起作用。请查看此答案以获取更多详细信息:stackoverflow link

答案 2 :(得分:0)

当您第一次在模拟器或Android手机中启动Android应用程序时,您的数据库将被创建并且您的onCreate()方法将被调用。

当您更改DB_VERSION以使其大于旧版本时,将调用您的onUpgrade()方法。

如果你正在测试你的Android应用程序,并且你想在安装时调用onCreate()方法,你必须从模拟器或android手机上卸载它。卸载后,Android app的安装将​​调用onCreate()方法。

但请记住,如果你卸载了应用程序,那么如果你的DB_VERSION比旧版本更大并不重要,它只会调用onCreate()。

相关问题