我的java项目有问题。我有两个文本文件要用jdbc驱动程序插入postgresql。这两个文件属于以下类型:
1)snp(rsid,chr,布尔值)
2)位点(rsid,mrna,基因,类)
我得买两张这种桌子:
1)Snp(id serial,rsid varchar,chr varchar,has_sig boolean)
2)位点(id序列,mana_acc varchar,gene varchar,class varchar,snp_id integer)
其中,位点的snp_id是对snp(id)的外键引用。
我可以用于表之间连接的字段是rsid,它对两个文件都是通用的。
我是这样创建表的:

st = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS snp ("
                + "id SERIAL PRIMARY KEY, "
                + "rsid varchar(100), "
                + "chr varchar(100), "
                + "has_sig varchar(100))");
        st.executeUpdate();
        st.close();

        st = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS locus ("
                + "id SERIAL PRIMARY KEY, "
                + "rsid varchar(100), "
                + "mrna_acc varchar(100), "
                + "gene varchar(100), "
                + "class varchar(100), "
                + "snp_id integer REFERENCES snp(id) on delete cascade on update cascade)");
        st.executeUpdate();
        st.close();

然后我使用select查询而不是snp_id字段将文件放入表中:
FileSnp fs = new FileSnp("/Users/valentinafratini/Documents/Progetto Tesi/FactoryMethodDb/snp.csv");
        fs.readFile();
        while (fs.line!=null) {
            fs.line = fs.reader.readLine();

            if (fs.line!=null && fs.line.length()>0) {
                    fs.obj = fs.line.split("\\s+");
                    fs.readSingleObj();

                    st = connection.prepareStatement("INSERT INTO snp ("
                        + "id, "
                        + "rsid, "
                        + "chr, "
                        + "has_sig) "
                        + "VALUES (DEFAULT, ?, ?, ?)");
                    st.setString(1, fs.rsid);
                    st.setString(2, fs.chr);
                    st.setString(3, fs.has_sig);
                    st.executeUpdate();
                    st.close();

FileLocus fl = new FileLocus("/Users/valentinafratini/Documents/Progetto Tesi/FactoryMethodDb/locus.csv");
        fl.readFile();
        while (fl.line!=null) {
            fl.line = fl.reader.readLine();

            if (fl.line!=null && fl.line.length()>0) {
                    fl.obj = fl.line.split("\\s+");
                    fl.readSingleObj();

                    st = connection.prepareStatement("INSERT INTO locus ("
                        + "id, "
                        + "rsid, "
                        + "mrna_acc, "
                        + "gene, "
                        + "class,"
                        + "snp_id) "
                        + "VALUES (DEFAULT, ?, ?, ?, ?, (SELECT id FROM snp s WHERE rsid = s.rsid))");
                    st.setString(1, fl.rsid);
                    st.setString(2, fl.mrna_acc);
                    st.setString(3, fl.gene);
                    st.setString(4, fl.classe);
                    st.executeUpdate();
                    st.close();

但当我填写时,我有以下错误:
错误:用作表达式的子查询返回了多行
你能帮助我吗?
非常感谢你。

最佳答案

似乎rsid在表snp中不是唯一的。导致子查询

SELECT id FROM snp s WHERE rsid = s.rsid

返回多行,这是错误的原因。
我建议您使用INSERT ... RETURNING获取输入到id中的行的snp
INSERT INTO snp (id, ...)
VALUES (DEFAULT, ...)
RETURNING id;

然后将返回值绑定到以下查询中的占位符:
INSERT INTO locus (..., snp_id)
VALUES (..., ?);

10-07 21:20