HarmonyOS 鸿蒙Next Database新建的数据库使用insert方法时报错java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp

HarmonyOS 鸿蒙Next Database新建的数据库使用insert方法时报错java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp 照着教程里的demo自己写了一个对象映射关系型数据库,然后运行的时候报错,分别在insert和query方法中报错:java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp.database.ProductInfoHelper。我写的代码如下,请大佬帮我找找问题在哪?

  • DataAbility代码
public class ProdInfoDataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0x00102, "Demo");
    private static final String DATABASE_NAME = "ProdInfoDatabase.db";
    private static final String DATABASE_NAME_ALIAS = "ProdInfoDatabase";
    private static OrmContext ormContext = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "ProdInfoDataAbility onStart");
        //创建数据库
        DatabaseHelper helper = new DatabaseHelper(this);
        ormContext = helper.getOrmContext(DATABASE_NAME_ALIAS, DATABASE_NAME, ProdInfoDatabase.class);
        //初始化数据库表
        ProductInfo productInfo = new ProductInfo(0, "小米8", "8+256G", 1999);
        //插入初始数据
        ormContext.insert(productInfo);
        ormContext.flush();
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        //判空,防止引用空指针异常
        if (ormContext == null) {
            HiLog.error(LABEL_LOG, "failed to query, ormContext is null!");
            return null;
        }
        //查询谓词
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, ProductInfo.class);
        ResultSet resultSet = ormContext.query(ormPredicates, columns);
        if (resultSet == null) {
            HiLog.info(LABEL_LOG, "resultRet is null!");
        }
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        //判空
        if (ormContext == null) {
            HiLog.info(LABEL_LOG, "ProdInfoDataAbility insert");
            return -1;
        }
        //如果操作的表不是ProductInfo,则返回-1
        if (!uri.getDecodedPathList().get(1).equals("ProductInfo")) {
            HiLog.info(LABEL_LOG, "error table operated");
            return -1;
        }
        //初始化插入对象
        ProductInfo productInfo = new ProductInfo();
        productInfo.setId(value.getInteger("id"));
        productInfo.setProdName(value.getString("prodName"));
        productInfo.setPrice(value.getInteger("price"));
        productInfo.setProdIntroduction(value.getString("prodIntroduction"));
        productInfo.setImage_Index(value.getString("image_Index"));
        productInfo.setImage_1(value.getString("image_1"));
        productInfo.setImage_2(value.getString("image_2"));
        productInfo.setImage_3(value.getString("image_3"));
        productInfo.setImage_4(value.getString("image_4"));
        productInfo.setImage_5(value.getString("image_5"));
        // 插入数据库
        boolean isSuccessed;
        isSuccessed = ormContext.insert(productInfo);
        if (!isSuccessed) {
            HiLog.error(LABEL_LOG, "failed to insert");
            return -1;
        }
        isSuccessed = ormContext.flush();
        if (!isSuccessed) {
            HiLog.error(LABEL_LOG, "failed to insert flush");
            return -1;
        }
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        //返回的id,为数据表自增主键id
        int id = Math.toIntExact(productInfo.getRowId());
        HiLog.debug(LABEL_LOG, "success to insert id=" + id);
        return id;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL_LOG, "failed to delete, ormContext is null");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, ProductInfo.class);
        int result = ormContext.delete(ormPredicates);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        // result>0 表示删除成功,result<=0 表示删除失败
        if (result > 0) {
            HiLog.debug(LABEL_LOG, "ProdInfoDataAbility  success to delete value=" + result);
        } else {
            HiLog.debug(LABEL_LOG, "ProInfoDataAbility  failed to delete value=" + result);
        }
        return result;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL_LOG, "failed to update, ormContext is null");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, ProductInfo.class);
        int result = ormContext.update(ormPredicates, value);
        // result>0 表示更新成功,result<=0 表示更新失败
        if (result > 0) {
            HiLog.info(LABEL_LOG, "ProInfoDataAbility success to update value:" + result);
        } else {
            HiLog.info(LABEL_LOG, "ProInfoDataAbility failed to update value:" + result);
        }
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        return result;
    }
}
  • 数据库代码
@Entity(tableName = "productInfo")
public class ProductInfo extends OrmObject {
    public ProductInfo() {
    }

    public ProductInfo(Integer id, String prodName, String prodIntroduction, Integer price) {
        this.id = id;
        this.prodName = prodName;
        this.prodIntroduction = prodIntroduction;
        this.price = price;
    }

    @PrimaryKey(autoGenerate = true)
    private Integer id;

    @Column(unique = true, notNull = true)
    private String prodName;

    @Column(notNull = true)
    private String prodIntroduction;

    @Column(notNull = true)
    private Integer price;

    private URI image_Index;
    private URI image_1;
    private URI image_2;
    private URI image_3;
    private URI image_4;
    private URI image_5;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProdName() {
        return prodName;
    }

    public void setProdName(String prodName) {
        this.prodName = prodName;
    }

    public String getProdIntroduction() {
        return prodIntroduction;
    }

    public void setProdIntroduction(String prodIntroduction) {
        this.prodIntroduction = prodIntroduction;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public URI getImage_Index() {
        return image_Index;
    }

    public void setImage_Index(String image_Index) {
        this.image_Index = URI.create(image_Index);
    }

    public URI getImage_1() {
        return image_1;
    }

    public void setImage_1(String image_1) {
        this.image_1 = URI.create(image_1);
    }

    public URI getImage_2() {
        return image_2;
    }

    public void setImage_2(String image_2) {
        this.image_2 = URI.create(image_2);
    }

    public URI getImage_3() {
        return image_3;
    }

    public void setImage_3(String image_3) {
        this.image_3 = URI.create(image_3);
    }

    public URI getImage_4() {
        return image_4;
    }

    public void setImage_4(String image_4) {
        this.image_4 = URI.create(image_4);
    }

    public URI getImage_5() {
        return image_5;
    }

    public void setImage_5(String image_5) {
        this.image_5 = URI.create(image_5);
    }

    @Override
    public String toString() {
        return "ProductInfo{" +
                "id=" + id +
                ", prodName='" + prodName + '\'' +
                ", prodIntroduction='" + prodIntroduction + '\'' +
                ", price=" + price +
                ", image_Index=" + image_Index +
                ", image_1=" + image_1 +
                ", image_2=" + image_2 +
                ", image_3=" + image_3 +
                ", image_4=" + image_4 +
                ", image_5=" + image_5 +
                '}';
    }
}
  • 数据库代码
@Database(entities = {ProdInfoDatabase.class}, version = 1)
public abstract class ProdInfoDatabase extends OrmDatabase {}
  • 使用数据库的函数
private void initIndexImage() throws DataAbilityRemoteException{
    DataAbilityHelper helper = DataAbilityHelper.creator(this);
    //初始化轮播图
    //定义查询条件
    DataAbilityPredicates query_Image_Index = new DataAbilityPredicates();
    query_Image_Index.between("id", 0,1 );
    String[] columns = new String[]{"id"};
    Uri uri = Uri.parse(uriString);
    //查询轮播图数据
    try {
        ResultSet resultSet = helper.query(uri, columns, query_Image_Index);
        ArrayList<Uri> Index_array = new ArrayList<>();
        resultSet.goToFirstRow();
        do {
            // 在此处理ResultSet中的记录;
            HiLog.info(hiLogLabel,
                    "uri_1=" + resultSet.getString(0));
        } while (resultSet.goToNextRow());
    } catch (DataAbilityRemoteException e) {
        e.printStackTrace();
    }
}

更多关于HarmonyOS 鸿蒙Next Database新建的数据库使用insert方法时报错java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

此帖终结,经过排查,是我写数据库类的时候引用错了表类。

更多关于HarmonyOS 鸿蒙Next Database新建的数据库使用insert方法时报错java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


欢迎开发小伙伴们进来帮帮楼主

针对您提到的HarmonyOS(鸿蒙)系统中Next Database新建的数据库在使用insert方法时报错java.lang.IllegalStateException: Execute getHelper failed : com.example.productdisplayapp的问题,这里提供一个可能的解决方案方向,但请注意,由于鸿蒙系统的具体API和框架细节可能有所不同,以下信息仅供参考:

此错误通常表明在执行数据库操作时,数据库帮助类(Helper)的获取或初始化失败了。可能的原因包括:

  1. 数据库Helper类未正确配置或实现:检查com.example.productdisplayapp包下的数据库Helper类,确保它正确继承了鸿蒙系统提供的数据库Helper基类,并实现了所有必要的抽象方法。

  2. 上下文(Context)问题:确保在调用数据库操作时,传递的上下文对象是有效的且未被销毁。错误的上下文引用可能导致无法正确初始化数据库Helper。

  3. 多线程访问冲突:如果数据库操作是在非UI线程中进行的,确保使用了正确的线程同步机制,避免多线程同时访问数据库导致的冲突。

  4. 资源限制:检查设备是否因资源限制(如内存不足)而无法正确加载或执行数据库Helper。

如果问题依旧没法解决请联系官网客服, 官网地址是 https://www.itying.com/category-93-b0.html

回到顶部