我的java项目有问题。我有两个文本文件插入postgresql与jdbc驱动程序。这两个文件都属于这种类型: 1)snp(rsid,chr,boolean value) 2)基因座(rsid,mrna,基因,类)
我必须得到两个这种类型的表: 1)Snp(id serial,rsid varchar,chr varchar,has_sig boolean) 2)Locus(id serial,mana_acc varchar,gene varchar,class varchar,snp_id integer) 其中locus的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();
但是当我填写时,我有以下错误:
你能帮帮我吗? 非常感谢你。错误:用作表达式
的子查询返回多行
答案 0 :(得分:0)
似乎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 (..., ?);