• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Dynamic content in SQLite query and error because it's not "final"  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to execute SQLite query like this:



The problem is:
error: local variables referenced from a lambda expression must be final or effectively final stmt.executeUpdate(Q);

How can I generate query command using conditionals and make it work? :-/
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not showing all the relevant code. I don't see the lambda expression that the compiler is complaining about. Apparently the stmt.executeUpdate(Q); is in a lambda expression, but you didn't post that. You're also not showing how the variable stmt is declared. Please show us the complete code, otherwise it will be a lot harder to explain exactly what's wrong.

Variables used inside a lambda expression must be effectively final. Apparently your variable stmt is not, that's why you get an error. You can try making it final: final Statement stmt = ...;

Note also: The = new String(); is not necessary at all. You're going to set gender to either "1" or "0" in the following if-statement - throwing away the empty String object you just created.
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code with lambda expression... it's an event that happens when users click save button.
Stmt has been defined before this expression in same function:


and is working great when selecting data above this event:

 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's almost never a good reason to say new String(). Take them out of 4 and 13 and try again.
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I added a function:



and did the event:



Had to add final to "Statement stmt = ...".

I don't really get it why it wouldn't work before... :-/
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:There's almost never a good reason to say new String().


@Matt: Indeed, I wonder if you need a String at all, except to provide a value for your database table (and why that would be "0" or "1", as opposed to the more usual "M" or "F", I have no idea). What about:It might not help with your database stuff, but it sure makes more sense to me than using a String as a "gender". You might also want to read the StringsAreBad page for more details.

HIH

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!