gpt4 book ai didi

Android Studio - SQLiteOpenHelper 问题

转载 作者:行者123 更新时间:2023-11-29 20:21:38 26 4
gpt4 key购买 nike

我有以下 Java 类

package com.sjhdevelopment.shaunharrison.myejuiceapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "EJuiceData.db";
public static final int DATABASE_VERSION = 1;


public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

public void onCreate(SQLiteDatabase database) {
database.execSQL("create table if not exists Inventory");
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
database.execSQL("ALTER TABLE Recipe ADD COLUMN NOTES TEXT");
onCreate(database);
}

}

然后在我的主要 Activity 中,我有以下内容;

public class Calculation extends AppCompatActivity {
private MyDBHelper dbHelper;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation);
try {
dbHelper = new MyDBHelper(this);
dbHelper.getWritableDatabase();

}
catch(Exception e)
{
showError("Error", e.getMessage());
}
}

但是,当我调试代码以查看是否在尝试和数据库上使用断点创建数据库 + 表时。execSQL("create....调试器实际上并没有进入 MyDBHelper 类做任何事情,因此我假设没有创建数据库和表

我在网上看过,它说当调用 getWritableDatabase/readable 时,应该调用 onCreate 方法

知道我做错了什么吗?

最佳答案

onCreate(...) 仅在第一次创建数据库时调用。如果你想改变你的数据库,你必须增加 DATABASE_VERSION 然后它会跳转到 onUpgrade(...)

关于Android Studio - SQLiteOpenHelper 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33080635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com