Setting Array in preparedstatement, throws a java.sql.SQLFeatureNotSupportedException - arrays

I have a List of Strings that I want to set as a parameter in a preparedstatement. This question and answer here, makes it look easy:
How to use an arraylist as a prepared statement parameter
Well, not really easy. There is still the conversion of a List, to an SQL-Array, which I found easiest to do by creating a String[] in between.
Below is my code:
PreparedStatement s = con.prepareStatement("SELECT * FROM Table WHERE Country IN ? ");
String[] countryArray = new String[countryListObject.size()];
countryArray = countryListObject.toArray(countryArray);
Array cArray = con.createArrayOf("VARCHAR", countryArray); //<--- Throws the exception
s.setArray(1, cArray);
This answer Seems to adress a similar problem, but I can't really understand how this helped solve anything. The answer is ambigous at best, stating only that:
Basically what you are wanting to do is not directly possible using a
PreparedStatement.
I've come to learn from the API Documentation that this exception is thrown if the JDBC driver does not support this method. I'm running a com.microsoft.sqlserver sqljdbc4 version 3.0. I am trying to see which versions do and don't support setArray, but I can't find the information. It is probably right in front of me, but I would really appreciate a little help on this.
How can I figure out if my JDBC's do support setArray()?

Related

peewee get_or_create and then save: error binding

Is there an easy way to update a field on a get of a get_or_create?
I have a class ItemCategory and I want to either create a new entry or get the already created entry and update a field (update_date).
What I do is:
item,created= ItemCategory.get_or_create(cat=cat_id,item=item_id)
if created == True:
print "created"
else:
item.update_date = datetime.now
item.somethingelse = 'aaa'
item.save()
This works for a while in my loop. But after 50-100 get/create it crashes:
peewee.InterfaceError: Error binding parameter 4 - probably unsupported type.
Maybe I should use upsert(), I tried but wasn't able to get anything working. Also it's not probably the best solution, since it makes a replace of the whole row instead of just a field.
I like peewee, it's very easy and fast to use, but I can't find many full examples and that's a pity
Newbie mistake
item.update_date = datetime.now()
I am not 100% sure this is the only answer though. I modified my code so many times that it might be also something else.
Regarding my question about create_or_update , I've done this:
try:
Item.create(...)
except IntegrityError:
Item.update(...)
peewee is really great, I wonder why no one ever asked for a create_or_update.

Beginners Example: Cypher not returning a node name for one node, but is for all the others

I'm trying to learn about Cypher using the really basic, beginners example at http://www.neo4j.org/console.
The first query, I attempted is:
MATCH (n)
RETURN n
which essentially says:
"Match all nodes and return them"
The result I get is this:
{name:"Neo"}
(1:Crew {name:"Morpheus"})
(2:Crew {name:"Trinity"})
(3:Crew:Matrix {name:"Cypher"})
(4:Matrix {name:"Agent Smith"})
(5:Matrix {name:"The Architect"})
This answer seems correct, as these are indeed all the nodes in the database. However, why does the result for "Neo" not include a number and the type "Crew" like all the others?
I would expect it to say (0:Crew {name:"Neo"})
In fact it should ...
I have checked ajax call, and I have found the label for this node, but neo4j console doesn't print it. I don't know why for now.
I think this is a bug of the application.
Can you report it here https://github.com/neo4j-contrib/rabbithole ?
Cheers.

BadValueError: Expected GeoPt

I am trying to pass values from a html form into the gae datastore and receive the following error:
"BadValueError: Expected GeoPt, got (51.123, -0.123)"
Apologies up front, but I'm new to this so may be going about it all wrong but would really appreciate some advice on the best way of doing this.
Thanks in advance.
Looks like you're trying to assign a tuple to the property instead of a geopt.
Your code does this:
entity.some_geo_property = (51.123, -0.123)
Where it needs to do this:
from google.appengine.ext import ndb
entity.some_geo_property = ndb.GeoPt(51.123, -0.123)
alternatively using the tuple:
entity.some_geo_property = ndb.GeoPt(*value)
Without seeing your exact code it's hard to offer a complete solution. If you provide code I'll try to update my answer.
I have never worked with GeoPt properties, but I would think that you passed a tuple of two floats.
Pass the value to ndb.GeoPt() first and store the return value in NDB. Example:
location = ndb.GeoPt(51.123, -0.123)
If you use the DB API of HRD rathern than the NDB API, use db.GeoPt() which is basically the same.

Accessing variable from other class returns null

I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?

C#, Linq data from EF troubles

I don't have much experience with programming. I am working (to learn) on a project. I am using C# 4.0 and WPF 4 with EF (SQLite). I am having problemw with LINQ.
Here is the code in question (I hope this is enough, let me know if more is needed)
private void cboSelectCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (ShippingEntities context = new ShippingEntities())
{
var companies = from company in context.Deliveries
where company.State == cboSelectCompany.SelectedItem.ToString()
select company;
txtDeliveryName.Text = companies.Name;
txtDeliveryState.Text = companies.State;
}
The last two lines don't work. Am I misunderstanding what LINQ is returning? I just get this error
Error 5 'System.Linq.IQueryable<SqliteDemo.Delivery>' does not contain a definition for 'State' and no extension method 'State' accepting a first argument of type 'System.Linq.IQueryable<SqliteDemo.Delivery>' could be found (are you missing a using directive or an assembly reference?) c:\users\dan\documents\visual studio 2010\Projects\SqliteDemo\SqliteDemo\DeliveryCompanies.xaml.cs 49 51 SqliteDemo
If anyone could give me some pointers or to a good reference I would appreciate it
You're close!
Linq is returning a Queryable set of Deliveries, which hasn't executed yet. My guess based on reading your code is that you're expecting at most one result, and then you want to put those values into a UI of some sort. In that case what you'll want to do is:
Take your IQueryable and execute it
Make sure you grab the first (or, alternatively enforce that there is only one) row
Set the appropriate values in the UI.
So, leave the line with the query there, and then change the rest to something like this:
var company = companies.FirstOrDefault();
txtDeliveryName.Text = company.Name;
txtDeliveryState.Text = company.State;
Insert null-checking as appropriate, and go check out the differences between First, FirstOrDefault, Single, and SingleOrDefault to see which one seems most correct for your particular situation.
Well, looks like companies.State doesn't compile, because companies is a list of SqliteDemo.Delivery (and not just a SqliteDemo.Delivery).
What do you want to achieve? Imagine that your LINQ query returns several results, is it possible?
Just use companies.FirstOrDefault() or companies.Single() to access first item becouse by default LINQ returns collection of items not just single item.

Resources