简体   繁体   English

Jackcess等价于简单的SQL SELECT语句

[英]Jackcess equivalents for simple SQL SELECT statements

Can someone please explain how I might use Jackcess to implement the equivalent of the following SQL queries? 有人可以解释一下我如何使用Jackcess来实现以下SQL查询的等价物吗?

SELECT name FROM table WHERE id = '1'

SELECT name FROM table INNER JOIN table ON table.id = table2.id

Re: your first query (SELECT ... FROM tableName WHERE ...) Re:你的第一个查询(SELECT ... FROM tableName WHERE ...)

In its simplest form you could use the "Searching for a row with a specific column value" example under "Sample code" on the main Jackcess page here , ie, 最简单的形式,你可以在主Jackcess页面上使用在“示例代码”的“搜索排与特定的列值”的例子在这里 ,也就是说,

Table table = DatabaseBuilder.open(new File("my.mdb")).getTable("MyTable");
Row row = CursorBuilder.findRow(table, Collections.singletonMap("a", "foo"));
if(row != null) {
    System.out.println("Found row where 'a' == 'foo': " + row);
} else {
    System.out.println("Could not find row where 'a' == 'foo'");
}

To loop through multiple matching rows you could do something like this 要遍历多个匹配的行,您可以执行类似的操作

Table table = DatabaseBuilder.open(new File("my.mdb")).getTable("MyTable");
Cursor cursor = CursorBuilder.createCursor(table);
while (cursor.findNextRow(Collections.singletonMap("a", "foo"))) {
    Row row = cursor.getCurrentRow();
    System.out.println(String.format(
            "a='%s', SomeFieldName='%s'.", 
            row.get("a"), 
            row.get("SomeFieldName")));
}

or, as @jtahlborn suggests in a comment below, 或者,正如@jtahlborn在下面的评论中所建议的,

Table table = DatabaseBuilder.open(new File("my.mdb")).getTable("MyTable");
Cursor cursor = CursorBuilder.createCursor(table);
for (Row row : cursor.newIterable().addMatchPattern("a", "foo")) {
    System.out.println(String.format(
            "a='%s', SomeFieldName='%s'.", 
            row.get("a"), 
            row.get("SomeFieldName")));
}

Re: your second query (SELECT ... FROM table1 INNER JOIN table2 ON ...) Re:你的第二个查询(SELECT ... FROM table1 INNER JOIN table2 ON ...)

You could use one for or while loop (similar to above) to iterate through the relevant rows in one table, and use an inner for or while loop to iterate through the related rows in the other (related) table. 您可以使用一个forwhile循环(类似于上面)来遍历一个表中的相关行,并使用内部forwhile循环来遍历另一个(相关)表中的相关行。 If the two tables have an existing "Relationship" in Access (aka "foreign key constraint") Jackcess has a Joiner class that may be of assistance, discussed here . 如果两个表在Access中具有现有的“关系”(也称为“外键约束”),则Jackcess具有可能有帮助的Joiner类, 这里讨论。


If you require further assistance you will need to try writing some code for yourself and then ask a new question showing the code you are attempting to use and the specific issue you have with it. 如果您需要进一步的帮助,您需要尝试自己编写一些代码 ,然后提出一个新问题,显示您尝试使用的代码以及您遇到的具体问题

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

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