SQLite delete 返回多个记录我想限制只删除找到的第一条记录
SQLite delete returning more than one record I want to limit to just deleting the the first record found
我正在使用 SQLite 3.15 和 Java 8.
String createTableQueryPK = "CREATE TABLE IF NOT EXISTS fileBlock (id not null,filename not null, PRIMARY KEY(id,filename))";
我的删除功能
public void deleteRecord(String filename) {
String deleteQuery = "delete from fileBlock where filename=(select * from fileBlock where filename=? order by id limit 1 offset 1)";
//"delete from fileBlock where filename= (select id, filename from fileBlock where filename=? order by id LIMIT 1);";
try {
if(c != null) {
PreparedStatement prep = c.prepareStatement(deleteQuery);
//prep.setInt(1, id);
prep.setString(1, filename);
prep.execute();
System.out.println("record deleted ");
}
} catch(Exception e) {
System.out.println("Error " + e.getMessage());
得到以下错误:Error [SQLITE_ERROR] SQL error or missing database (sub-select returns 2 columns - expected 1)
我已经尝试过以下查询任何想法:
delete from fileBlock where filename in (select Distinct filename from fileBlock where filename='test9.txt')
这将删除所有记录
该应用程序的想法是来自不同来源的许多文件具有相同的文件名或相同的用户尝试加载相同的文件
应用程序将如何工作 将文件名提交给删除功能,如果有,则取出第一条记录并将其删除。
是的,我知道不是一个好的设计
问题是:
如何删除结果集中找到的第一条记录并保留所有其他记录
将此查询更改为 return 文件名而不是 *
delete from fileBlock where filename=(select * from fileBlock where filename=? order by id limit 1 offset 1)
到
delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)
如果您只想删除与给定文件名匹配的单个 'random' 行,则必须对该行使用一些独特的内容,例如 rowid
.
想法是这样的(根据需要调整到您的数据库架构):
create table t(filename);
insert into t values('One'),('One'),('One'),('One'),
('Two'),('Two'),('Two'),('Two'),
('Three'),('Three'),('Three');
select count(*) from t; --initially there are 11 rows in this example
delete from t where rowid = (select rowid from t where filename = 'Two' limit 1);
select count(*) from t; -- now, only 10 rows indicating only one row was deleted
(即使没有 limit 1
它也会工作,因为 =
只匹配返回的第一个值,但更准确地说。)
如果您只想删除一行,哪一行都无所谓。
添加限制 1 以删除查询
示例:
delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)
limit 1;
或
如果需要,您可以不使用 select 语句直接执行删除查询。
delete from fileBlock where filename=? limit 1;
我的解决方案基于以下答案
public void deleteRecord(String filename) {
String deleteQuery = "delete from fileBlock where rowid = (select rowid from fileBlock where filename = ? limit 1);";
String deleteQuery1 = "delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)limit 1;";
//"delete from fileBlock where filename= (select id, filename from fileBlock where filename=? order by id LIMIT 1);";
try {
if(c != null) {
PreparedStatement prep = c.prepareStatement(deleteQuery1);
//prep.setInt(1, id);
prep.setString(1, filename);
prep.execute();
System.out.println("record deleted ");
}
} catch(Exception e) {
System.out.println("Error " + e.getMessage());
}
}
上面的查询都有效
谢谢
我正在使用 SQLite 3.15 和 Java 8.
String createTableQueryPK = "CREATE TABLE IF NOT EXISTS fileBlock (id not null,filename not null, PRIMARY KEY(id,filename))";
我的删除功能
public void deleteRecord(String filename) {
String deleteQuery = "delete from fileBlock where filename=(select * from fileBlock where filename=? order by id limit 1 offset 1)";
//"delete from fileBlock where filename= (select id, filename from fileBlock where filename=? order by id LIMIT 1);";
try {
if(c != null) {
PreparedStatement prep = c.prepareStatement(deleteQuery);
//prep.setInt(1, id);
prep.setString(1, filename);
prep.execute();
System.out.println("record deleted ");
}
} catch(Exception e) {
System.out.println("Error " + e.getMessage());
得到以下错误:Error [SQLITE_ERROR] SQL error or missing database (sub-select returns 2 columns - expected 1) 我已经尝试过以下查询任何想法:
delete from fileBlock where filename in (select Distinct filename from fileBlock where filename='test9.txt')
这将删除所有记录
该应用程序的想法是来自不同来源的许多文件具有相同的文件名或相同的用户尝试加载相同的文件
应用程序将如何工作 将文件名提交给删除功能,如果有,则取出第一条记录并将其删除。 是的,我知道不是一个好的设计
问题是: 如何删除结果集中找到的第一条记录并保留所有其他记录
将此查询更改为 return 文件名而不是 *
delete from fileBlock where filename=(select * from fileBlock where filename=? order by id limit 1 offset 1)
到
delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)
如果您只想删除与给定文件名匹配的单个 'random' 行,则必须对该行使用一些独特的内容,例如 rowid
.
想法是这样的(根据需要调整到您的数据库架构):
create table t(filename);
insert into t values('One'),('One'),('One'),('One'),
('Two'),('Two'),('Two'),('Two'),
('Three'),('Three'),('Three');
select count(*) from t; --initially there are 11 rows in this example
delete from t where rowid = (select rowid from t where filename = 'Two' limit 1);
select count(*) from t; -- now, only 10 rows indicating only one row was deleted
(即使没有 limit 1
它也会工作,因为 =
只匹配返回的第一个值,但更准确地说。)
如果您只想删除一行,哪一行都无所谓。
添加限制 1 以删除查询 示例:
delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)
limit 1;
或 如果需要,您可以不使用 select 语句直接执行删除查询。
delete from fileBlock where filename=? limit 1;
我的解决方案基于以下答案
public void deleteRecord(String filename) {
String deleteQuery = "delete from fileBlock where rowid = (select rowid from fileBlock where filename = ? limit 1);";
String deleteQuery1 = "delete from fileBlock where filename=(select filename from fileBlock where filename=? order by id limit 1 offset 1)limit 1;";
//"delete from fileBlock where filename= (select id, filename from fileBlock where filename=? order by id LIMIT 1);";
try {
if(c != null) {
PreparedStatement prep = c.prepareStatement(deleteQuery1);
//prep.setInt(1, id);
prep.setString(1, filename);
prep.execute();
System.out.println("record deleted ");
}
} catch(Exception e) {
System.out.println("Error " + e.getMessage());
}
}
上面的查询都有效 谢谢