JDBC - SQLITE Select to variable

吃可爱长大的小学妹 提交于 2021-02-05 11:13:45

问题


I am trying to run a query / select statement and save it in a variable. I know how to get something specific from a specific column but not from counting rows.

This is working as I getting MYID specifically.

ResultSet MYIDrs = stmtCFG.executeQuery( "SELECT rowid, MYID from MYINDEX order by rowid desc limit 1;" );
MYID = MYIDrs.getString("MYID");

Now I am trying to count the rows that works in SQLite client but not in the jdbc as I can't figure out what to request.

this is what I have but is not resulting in what I am expecting.

ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString(?????);

problem or question is: What do I put in the ????? as I already tried everything.

I am expecting to see a number.


回答1:


I am really sorry I found what I was looking for by assigning a name TOTAL

This is my code and it works...

ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) AS TOTAL from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString("TOTAL");



回答2:


You use wrong data type. COUNT(*) returns Integer type, Not String.

You can do like this without assigning a label for COUNT(*)

int FILE_COUNT = FILE_COUNTrs.getInt(1); // 1: is the column index of COUNT(*)


来源:https://stackoverflow.com/questions/40286512/jdbc-sqlite-select-to-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!