解析 CSV 文件..管理主键有问题吗?

发布于 2024-10-17 10:03:09 字数 80 浏览 2 评论 0原文

我刚刚创建了一个java文件来解析csv文件并将它们保存到oracle数据库中..但我需要一个充当主键的字段ID..而且我对abt循环有点困惑..

i just created a java file to parse a csv files and saved them into an oracle database.. but i need a field ID which acts as a primary key.. and i am a bit confused abt looping..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-10-24 10:03:09

我认为您需要做的就是利用一个序列(如 Ronnis 所建议的)

CREATE SEQUENCE FIELD_ID_SEQ START WITH 1 INCREMENT BY 1 NOCYCLE NOCACHE; 
/*NOTE THE SEQUENCE, WHILE INCREMENTING, IS NOT GUARANTEED TO BE 1,2,3,4...N ->expect gaps in the #*/

现在在您保存数据的 java 应用程序中:

"INSERT INTO TABLE_OF_CSV(FIELD_ID, FIELD_COLA, FIELD_COLB) VALUES(FIELD_ID_SEQ.NEXTVAL, ?,?);"

或者


现在,如果您正在使用一个过程(或包中的过程),您可以执行此操作(注意这会将主键返回到调用应用程序)

create procedure insertIntoCSVTable(pCOLA IN TABLE_OF_CSV.FIELD_COLA%TYPE 
                                  , pCOLB IN TABLE_OF_CSV.FIELD_COLB%TYPE
                                  , pFIELD_ID OUT TABLE_OF_CSV.FIELD_ID%TYPE)
AS
BEGIN

    INSERT INTO TABLE_OF_CSV(FIELD_ID, FIELD_COLA, FIELD_COLB) 
    VALUES(FIELD_ID_SEQ.NEXTVAL, pCOLA, pCOLB)
    RETURNING FIELD_ID
    INTO pFIELD_ID
    ;

END insertIntoCSVTable;

假设您已经在 J​​ava 代码中循环(假设逐行插入),则不需要循环,


或者


您可以使用触发器将新值插入到桌子:
创建或替换

TRIGGER TABLE_OF_CSV_TRG BEFORE INSERT ON TABLE_OF_CSV 
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF :NEW.FIELD_ID IS NULL THEN
      SELECT FIELD_ID_SEQ.NEXTVAL INTO :NEW.FIELD_ID FROM DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;

I think all you need to do is utilize a sequence (as suggested by Ronnis)

as such

CREATE SEQUENCE FIELD_ID_SEQ START WITH 1 INCREMENT BY 1 NOCYCLE NOCACHE; 
/*NOTE THE SEQUENCE, WHILE INCREMENTING, IS NOT GUARANTEED TO BE 1,2,3,4...N ->expect gaps in the #*/

Now either in your java app where you are saving the data:

"INSERT INTO TABLE_OF_CSV(FIELD_ID, FIELD_COLA, FIELD_COLB) VALUES(FIELD_ID_SEQ.NEXTVAL, ?,?);"

OR


Now if you are using a procedure (or a procedure within a package) you can do this (note this returns the primary key back to the calling app)

create procedure insertIntoCSVTable(pCOLA IN TABLE_OF_CSV.FIELD_COLA%TYPE 
                                  , pCOLB IN TABLE_OF_CSV.FIELD_COLB%TYPE
                                  , pFIELD_ID OUT TABLE_OF_CSV.FIELD_ID%TYPE)
AS
BEGIN

    INSERT INTO TABLE_OF_CSV(FIELD_ID, FIELD_COLA, FIELD_COLB) 
    VALUES(FIELD_ID_SEQ.NEXTVAL, pCOLA, pCOLB)
    RETURNING FIELD_ID
    INTO pFIELD_ID
    ;

END insertIntoCSVTable;

no looping required assuming you are already looping in your java code (assuming a row-by-row insert)


OR


You may use a trigger to insert a new value into the table:
create or replace

TRIGGER TABLE_OF_CSV_TRG BEFORE INSERT ON TABLE_OF_CSV 
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF :NEW.FIELD_ID IS NULL THEN
      SELECT FIELD_ID_SEQ.NEXTVAL INTO :NEW.FIELD_ID FROM DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文