[Solved] Cannot get COUNT from query
-
- Posts: 35
- Joined: Tue Aug 03, 2010 12:04 am
[Solved] Cannot get COUNT from query
I have a Basic macro that connects to a OpenOffice.org Calc spreadsheet and imports the data using the Uno service Drive Manaer. I successully connect to the Calc spreadsheet and successfully execute the query "SELECT * FROM TABLENAME", and then cycle through all records importing the data to my database. However the following query does not produce the count of records from the spreadsheet "SELECT COUNT(*) AS ICOUNT FROM TABLENAME". The variable ICOUNT is empty. Am I doing something wrong or is COUNT not valid in this situation? I can cycle through the records and get a count of the number of records if necessary.
Last edited by jgsylvester on Wed Sep 08, 2010 3:29 am, edited 1 time in total.
Openoofice.org 3.3.0 Windows Vista (also Openoffice.org 3.3.0, Ubuntu 10.10)
Re: Cannot get COUNT from query
When I use a Base document and have as database a Calc sheet, I can use a 'count-*' query. I do not know whether you can expect that a BASIC variable 'ICOUNT' is filled by running the query. I think that you need to bind your BASIC variable to the output column. In some languages with 'embedded' SQL you normally specify '... AS :ICOUNT FROM ... (note the colon) to tell that there is a relation between language variable and query result. Not sure how that works in Base with BASIC, but have a look to variable binding.
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
W11 22H2 (build 22621), LO 7.4.2.3(x64)
-
- Posts: 35
- Joined: Tue Aug 03, 2010 12:04 am
Re: Cannot get COUNT from query
Thanks for the suggestion. I get an BASIC runtime error: An exception occurred. Type: com.sun.star.sdbc.SQLException. Syntax error. Unexpected symbol ':', expecting NAME or CHAR_LENGTH or SQL_TOKEN_POSITION or SQL_TOKEN_EXTRACT. I also tried to COUNT (HEADERNAME) but that failed, saying only COUNT (*) was valid. I have never seen CHAR_LENGTH. I will look into that - maybe there is something there.
Openoofice.org 3.3.0 Windows Vista (also Openoffice.org 3.3.0, Ubuntu 10.10)
Re: Cannot get COUNT from query
Why don't you try without the column alias in the query statement with "count(*)"? Simply cycle through your one row result set:
Code: Select all
oStmt = oCon.prepareStatement("SELECT COUNT(*) FROM TABLENAME")
oResult = oStmt.executeQuery()
oResult.next() ' usually you would have a while ... here, but as it is always exactly one row
theCount = oResult.getString(1)
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
-
- Posts: 35
- Joined: Tue Aug 03, 2010 12:04 am
Re: Cannot get COUNT from query[Solved]
It worked!! Thankyou very much for solving this problem. I really appreciate it.
Openoofice.org 3.3.0 Windows Vista (also Openoffice.org 3.3.0, Ubuntu 10.10)