简体   繁体   English

使用类的方法将数据插入数据库

[英]Insert data into database with a method of a class

I am trying to create a method to insert data to the database but it does not work. 我正在尝试创建一种将数据插入数据库的方法,但是它不起作用。 I am trying to insert data by using add_person . 我正在尝试使用add_person插入数据。 Here is my code. 这是我的代码。 The transparent table name is ZPERSON_20 . 透明表名称为ZPERSON_20

CLASS lcl_person DEFINITION.
  PUBLIC SECTION.
    METHODS:
      add_person
        IMPORTING 
          im_id   TYPE zperson_20-person_id
          im_name TYPE zperson_20-person_name
          im_add  TYPE zperson_20-person_address
          im_type TYPE zperson_20-person_type.

  PRIVATE SECTION.
    DATA:
     c_id   TYPE zperson_20-person_id,
     c_name TYPE zperson_20-person_name,
     c_add  TYPE zperson_20-person_address,
     c_type TYPE zperson_20-person_type.

ENDCLASS.

CLASS lcl_person IMPLEMENTATION.
  METHOD add_person.
    DATA: it_emp TYPE STANDARD TABLE OF zperson_20.
    DATA: wa_emp LIKE LINE OF it_emp.

    wa_emp-person_id   = c_id.
    wa_emp-person_name = c_name.
    wa_emp-person_add  = c_add.
    wa_emp-person_type = c_type.

    INSERT INTO zperson_20 VALUES wa_emp.
  ENDMETHOD.                
ENDCLASS.

PARAMETERS:
  v_id   TYPE zperson_20-person_id,
  v_name TYPE zperson_20-person_name,
  v_add  TYPE zperson_20-person_address,
  v_type TYPE zperson_20-person_type.

DATA: lv_ref_person TYPE REF TO lcl_person.

START-OF-SELECTION.
  CREATE OBJECT lv_ref_person.
  CALL METHOD lv_ref_person->add_person(
    im_id   = v_id
    im_name = v_name
    im_add  = v_add
    im_type = v_type
  ).

I would say your issue is inside the add_person method, as you are reading the values from the member variables, instead of the method parameters. 我会说您的问题在add_person方法内部,因为您正在从成员变量而不是方法参数中读取值。 Those member variables will probably be empty in this case, leading to inserting errors due to an empty or duplicated id. 在这种情况下,这些成员变量可能为空,由于ID为空或重复而导致插入错误。

I believe that the code you are trying to achieve is the following: 我相信您要实现的代码如下:

REPORT ZRKD_BASE_WSLOADER.

CLASS lcl_person DEFINITION.
  PUBLIC SECTION.
    METHODS:
      add_person
        IMPORTING
          im_id   TYPE zperson_20-person_id
          im_name TYPE zperson_20-person_name
          im_add  TYPE zperson_20-person_address
          im_type TYPE zperson_20-person_type.

ENDCLASS.

CLASS lcl_person IMPLEMENTATION.
  METHOD add_person.    
    DATA: wa_emp TYPE zperson_20.

    //---->  Here are the modifications <-------

    wa_emp-person_id   = im_id.
    wa_emp-person_name = im_name.
    wa_emp-person_address  = im_add.
    wa_emp-person_type = im_type.

    INSERT INTO zperson_20 VALUES wa_emp.
  ENDMETHOD.
ENDCLASS.

PARAMETERS:
  v_id   TYPE zperson_20-person_id,
  v_name TYPE zperson_20-person_name,
  v_add  TYPE zperson_20-person_address,
  v_type TYPE zperson_20-person_type.

DATA: lv_ref_person TYPE REF TO lcl_person.

START-OF-SELECTION.
  CREATE OBJECT lv_ref_person.
  CALL METHOD lv_ref_person->add_person(
    im_id   = v_id
    im_name = v_name
    im_add  = v_add
    im_type = v_type
  ).

Would be enough to create a commit method, which you then call, if You are really sure to save on db, or add this parameter as a flag to add person. 如果确实确定要保存在db上,或者将该参数作为添加人员的标志添加,则足以创建一个commit方法,然后调用该方法。 If set, commit the transaction. 如果设置,则提交事务。 The instruction for this is "commit work." 该指令是“提交工作”。 and it is missing in the entire snippet. 并且在整个代码段中都丢失了。 And without it nothing moves from Your actual LUW commit buffer to db. 没有它,您的实际LUW提交缓冲区就不会移动到db。

Your method add_person is defined to take no arguments and you are passing it arguments. 您的方法add_person定义为不接受任何参数,并且您要为其传递参数。 Basically there is a disagreement in call and the definition. 通话和定义上基本上是有分歧的。

Can you post the whole code here? 您可以在此处发布整个代码吗?

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

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