Returned RealmResult have empty values - java

I'm trying to get query results from realm:
RealmResults<Line> results = realm.where(Line.class).lessThan("id",5).findAll();
Line[] lines = results.toArray(new Line[results.size()]);
for(int i=0;i<lines.length;i++)
Log.e("erralert", String.valueOf(lines[i].id) + "-" + lines[i].text + "-" + lines[i].localId);
the results are like:
0-null-0
0-null-0
0-null-0
0-null-0
But actually, I'm sure that the data in the database are correct and changing query .less than("id",5) shows that it has actually found 4 items (those with id less than 5). changing 5 to 10, returns 9 results, but the taken results are always empty.
The following code also have the same result:
RealmResults<Line> lines = realm.where(Line.class).lessThan("id",20).findAll();
for(int i=0;i<lines.size();i++)
Log.e("erralert", String.valueOf(lines.get(i).id) + "-" + lines.get(i).text + "-" + lines.get(i).localId);
P.S.: id field is Primary Key, so obviously we can't have records like that.

Related

Accessing text in PowerShell object. Why doesn't it seem to be stored in an array?

I have taken state plane coordinates from a SQL Server table and populated an object with 100 concatenated strings that will serve as my input into a java application.
$key = Invoke-Sqlcmd -Query "SELECT DISTINCT CONCAT('spc,'<spcZone>,',',
<northing>,',',<easting>, ',',<units>,',',<inDatum>,',',<outDatum>) as
conversionString FROM <source table>;" -ServerInstance "<server>" -Database
"<database>"
The result will be 100 strings that look like this in an array:
spc,2402,173099.419,503626.812,m,NAD83(2011),NAD83(2011)
I then use NOAA's gtk java application and run through all 100 observations to be converted:
$result = FOREACH ($k in $key.conversionString)
{
java -Dparms="$k" -jar H:\gtk\jtransform_thin.jar
}
The returned output for one observations looks like this:
{
"ID":"1489004917960",
"nadconVersion":"5.0",
"srcLat":"40.0000000000",
"srcLatDms":"N400000.000000",
"srcLon":"-80.0000000000",
"srcLonDms":"W0800000.000000",
"destLat":"40.0000000000",
"destLatDms":"N400000.000000",
"destLon":"-80.0000000000",
"destLonDms":"W0800000.000000",
"sigLat":"0.000000",
"sigLon":"0.000000",
"srcEht":"100.000",
"destEht":"100.000",
"sigEht":"0.000",
"srcDatum":"NAD83(1986)",
"destDatum":"NAD83(1986)",
"spcZone":"PA S-3702",
"spcNorthing_m":76470.584,
"spcEasting_m":407886.482,
"spcNorthing_usft":250887.241,
"spcEasting_usft":1338207.566,
"spcNorthing_ift":250887.743,
"spcEasting_ift":1338210.243,
"spcConvergence":"-01 27 35.224524",
"spcScaleFactor":0.99999024,
"spcCombinedFactor":0.99997455,
"utmZone":"UTM Zone 17",
"utmNorthing":4428236.065,
"utmEasting":585360.462,
"utmConvergence":"00 38 34.174932",
"utmScaleFactor":0.9996897,
"utmCombinedFactor":0.99967402,
"x":849623.061,
"y":-4818451.818,
"z":4078049.851,
"usng":"17TNE8536028236"
}
The problem I'm encountering is accessing the stored $result object's returned fields. If I type $result. It will return all text from all 100 observations. If I type $result[1] I only get the ID of the first observation. If I type $result.ID I do not have anything returned.
This is where I am trying to get to:
$add = foreach ($r in $result)
{
"INSERT INTO SPCtoLatLong VALUES ('" + $r.spcZone + "','" +
$r.spcNorthing_usft + "','" + $r.spcEasting_usft + "','" + $r.srcLat + "','"
+ $r.srcLon + "','" + $r.srcDatum + "','" + $r.destDatum + "')" + $nl
}
I only have 2 weeks experience in PowerShell, what am I doing wrong? Thank you for any help.
Stdout from external applications is returned as a string array (one line = one enum). You would need to parse the return data into an object, or, if you're on PowerShell 5+, you can utilize the ConvertFrom-String to create templates for these objects and pass it output to it to be converted.
But! Based on how well-formed your data is, I would do the following:
$Expected = $Result | ConvertFrom-Json
PS C:\> $Expected.GetType()
PSCustomObject
I figured out to solve this problem you should add this to the java application execution component.
$result = FOREACH ($k in $key.conversionString)
{
java -Dparms="$k" -jar H:\gtk\jtransform_thin.jar | ConvertFrom-Json
}

ERROR: extra data after last expected column When trying to inserting csv file

I am trying to insert csv file in postgresql database using java
the code is
CopyManager copyManager = new CopyManager((BaseConnection) conn);
FileReader fileReader = new FileReader(filename);
copyManager.copyIn("COPY meta.fk_payment_temp(\n"+
"settlement_ref_no, order_type, fulfilment_type, seller_sku, wsn, \n" +
" order_id, order_item_id, order_date, dispatch_date, delivery_date, \n" +
" cancellation_date, settlement_date, order_status, quantity, order_item_value, \n" +
" refund, protection_fund, total_marketplace_fee, service_tax, \n" +
" settlement_value, commission_rate, commission, fee_discount, \n" +
" cancellation_fee, fixed_fee, emi_fee, total_weight, shipping_fee, \n" +
" reverse_shipping_fee, shipping_zone, token_of_apology, pick_and_pack_fee, \n" +
" storage_fee, removal_fee, invoice_id, invoice_date, invoice_amount, \n" +
" sub_category, total_offer_amount, my_offer_share, flipkart_offer_share)\n" +
" FROM STDIN with csv header delimiter ','", fileReader );
there is 41 column in csv file and as well as in table
the error detail is:
Where: COPY fk_payment_temp, line 2:
"NFT-150331087GN00107XXXXXXX,prepaid,NON-FA,BD46-157,,OD102411813209536003,166288248,"Mar
25, 2015","..."
as we seen error is in "Mar 25, 2015" but the data is
NFT-150331087GN00107XXXXXXX prepaid NON-FA BD46-157 OD102411813209536003 166288248 25-Mar-15 26-Mar-15 27-Mar-15 31-Mar-15 delivered 1 339 339 0 0 -85.26 -10.54 243.2 15 -50.86 0 0 -5 0 0.3 -29.4 0 LOCAL 0 0 0 0 IN27248 26-Mar-15 309 babydoll 0 0 0
how i can solve this ?
My experience is that this is almost always caused by malformed csv data. There really is no substitute for knowing the format, how to review material for proper escaping, etc. And CSV is not simple. There are lots of rules regarding things like embedded end of line characters and the like.
A good place to start (if you hadn't already found your answer) would be running the data through a spreadsheet and see what seems to end up on the wrong columns.

reduce() not working with lightcouch

I have wirtten a program to manage tv series and I am stuck at an issue with lightcouch and a specific database query. This is what I have so far. To setup the database views I used the following lines:
MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
"function(doc) { "
+ " emit(doc.seriesName, doc.season)"
+ "}");
get_numberOfSeasonsMR.setReduce(
"function (key, values, rereduce) {"
+ "return Math.max.apply({}, values)"
+ "}");
map.put("get_numberOfSeasons", get_numberOfSeasonsMR);
In Futon everything appears normal (see http://i.stack.imgur.com/1hgSJ.png).
However, when I try to execute the following line, I get an exception, instead of the results that appear in Futon.
int nr = client.view("design/get_numberOfSeasons").key("Arrow").queryForInt();
Exception:
org.lightcouch.NoDocumentException: Expecting exactly a single result of this view query, but was: 0
org.lightcouch.View.queryValue(View.java:246)
org.lightcouch.View.queryForInt(View.java:219)
....db.Server.getNumberOfSeasons(Server.java:237)
...
I tried to emit Strings in my map() function instead on ints, but it did not make any difference. What am I doing wrong? Or can someone post an example of a successful lightcouch map()+reduce() operation? The tutorials I found only used map() without reduce().
Thanks in advance ;)
Nothing seems wrong with your code, here is the full version:
CouchDbClient dbClient = new CouchDbClient();
DesignDocument designDocument = new DesignDocument();
designDocument.setId("_design/mydesign");
designDocument.setLanguage("javascript");
MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
"function(doc) { "
+ " emit(doc.seriesName, doc.season)"
+ "}");
get_numberOfSeasonsMR.setReduce(
"function (key, values, rereduce) {"
+ "return Math.max.apply({}, values)"
+ "}");
Map<String, MapReduce> view = new HashMap<>();
view.put("get_numberOfSeasons", get_numberOfSeasonsMR);
designDocument.setViews(view);
dbClient.design().synchronizeWithDb(designDocument);
int count = dbClient.view("mydesign/get_numberOfSeasons").key("Arrow").queryForInt();

String concatenation in java is not working on one system

I believe that most of you would be thinking that this is the same question you have heard multiple times (and answered ) about string concatenation in Java. But trust me, it is different. In fact, so different that I am even hesitant in posting it here. But anyways, here it is. I have some piece of code which goes like:
public void handleSuccess(String result)
{
result = result.trim();
MessageBox.alert("Information","Result after trimming: '" + result + "'");
result = result.substring(result.indexOf('\n') + 1);
MessageBox.alert("Information","Result after substring: '" + result + "'");
String returns = getReturns();
MessageBox.alert("Information","Returns: '" + returns + "'");
String action = getAction();
MessageBox.alert("Information","Action: '" + action + "'");
String finalResult = result + returns + action;
MessageBox.alert("Information","Final result: '" + finalResult + "'");
}
Now the situation here is that, all of these : getReturns(), result and getAction() return non blank values, and in fact the string finalResult contains the concatenated value after the last line is executed.
So, at Line 1, "result" contains "12/03/2013|04-AERTY|". The value of result remains same at end of line 1,2. getReturns() returns value 12.4724. So at end of line 3, finalResult contains "12/03/2013|04-AERTY|12.4724". getAction() returns "expt". So, at end of line 5, finalResult contains "12/03/2013|04-AERTY|12.4724|expt"
This is , when I debug or run the application in eclipse. As soon as build the same application on a UNIX system to generate a "war" file, and deploy the war on a tomcat server, the problem rears it's ugly head. When I run the application on the deployed war, the last line does not contain the concatenated value. So at the end of line 5, finalResult contains just "12/03/2013|04-AERTY|12.4724". I expected it to contain "12/03/2013|04-AERTY|12.4724|expt" as it does while running in eclipse.
I have tried stringbuffer, stringbuilder and the "+" operator as well, but nothing seems to work. I am not even getting an exception.
Can somebody help me in fixing this or at least enlightening me in what I might be doing wrong here?
Just to stress again, the code on eclipse(which is on a windows machine) and UNIX machine are exactly same. I have done a diff on them.
Here is what I get after putting the message-boxes:
Message-box 1: "Result after trimming: '12/03/2013|04-AERTY|'"
Message-box 2: "Result after substring: '12/03/2013|04-AERTY|'"
Message-box 3:"Returns: '12.4724'"
Message-box 4:"Action: '|expt'"
Message-box 5:"Final result: '12/03/2013|04-AERTY|12.4724|expt'"
Message-box 5 output is the one I receive when I execute code using eclipse
When running on deployed war, Message-box 1-4 have the same output as above, but Message-box 5 says: "Final result: '12/03/2013|04-AERTY|12.4724"
It's not clear where the extra "|" is meant to come from - if getAction() just returns expt, the result would be 12/03/2013|04-AERTY|12.4724|expt.
Anyway, I think it's safe to say that string concatenation will be working fine, and something else is wrong. You should add more diagnostics, logging everything:
public void handleSuccess(String result) {
result = result.trim();
log.info("Result after trimming: '" + result + "'");
result = result.substring(result.indexOf('\n') + 1);
log.info("Result after substring: '" + result + "'");
String returns = getReturns();
log.info("Returns: '" + returns + "'");
String action = getAction();
log.info("Action: '" + action + "'");
// It's not clear what this is meant to do. I suggest you remove it and
// use logging instead.
MessageBox.alert("Information", "The selected action is " + action, null);
String finalResult = result + returns + action;
log.info("Final result: '" + finalResult + "'");
I suspect you'll find that action is an empty string in the broken case.
Note that I've added quotes round each of the logged values, very deliberately. That means that if there's some unprintable character at the end of a string which causes problems, you should be able to detect that in the logging.
EDIT: As per the comment thread, when these were turned into message boxes (as it turns out this is running in GWT) it looks like there's something wrong with the early strings, as the closing ' isn't seen in diagnostics, in the broken case. The OP is going to investigate further.

MySQL Query mutates on huge instances:

I am currently calculating values to fill a database of 15 milion records. The first 7 mill went just fin,e however now my update query starts giving problems :
Now & then a random letter changes into some jibberish.
In java I generate the query doing :
String updateSql = "UPDATE VanNaar SET time = CASE ID ";
for (int i = 0; i < routes.size(); i++) {
updateSql += " WHEN " + routes.get(i).ID + " THEN " + routes.get(i).driveTime;
}
updateSql += " END, ";
updateSql += " distance = CASE ID ";
for (int i = 0; i < routes.size(); i++) {
updateSql += " WHEN " + routes.get(i).ID + " THEN " + routes.get(i).distance;
}
updateSql += " END WHERE id IN (";
for (int i = 0; i < routes.size(); i++) {
updateSql += routes.get(i).ID + ",";
}
updateSql = updateSql.substring(0, updateSql.length() - 1);
updateSql += ");";
Which works just fine, as mentioned before. Here is what Java trows at me now:
...MySQL server version for the right syntax to use near '×HEN 8284022 THEN 999.999 WHEN 8284023 THEN 3791.0 WHEN 8284024 THEN 378...
Or
...MySQL server version for the right syntax to use near 'WÈEN 7468574 THEN 2273.0 WHEN 7468575 THEN 2410.0 WHEN 7468576 THEN 2472.0 W' at line 1
Notice the weirdisch Ã^ or Ã- , a final exmpale, mind you the bold tekst:
...MySQL server version for the right syntax to use near **'Â** WHEN 7228125 THEN 48590.0 WHEN 7228126 THEN 47910.0 WHEN 7228127 THEN...
...
Update:
It seems to be getting worse..:
Unknown column '9°22331' in 'where clause'
I'm no Java expert but shouldn't you be using a StringBuilder in the first place? Possibly even use a prepared statement? You could buld the prepared statement with a stringbuilder but instead of
updateSql += " WHEN " + routes.get(i).ID + " THEN " + routes.get(i).driveTime;
everywhere you'd do something like
myStrngBldr.append(" WHEN ? THEN ?");
or
myStrngBldr.append(" WHEN #foo1 THEN #foo2");
if named parameters are supported (don't know) and later add the actual parameters:
myPrepdStmt = myConn.prepareStatement(myStrngBldr.toString());
myPrepdStmt.setInt(1, routes.get(i).ID);
myPrepdStmt.setFloat(2, routes.get(i).driveTime);
...
myPrepdStmt.executeUpdate();
This page should help you.
What is actually causing the 'strange malformed strings': I don't know. My best guess would be the you'd have to use something like .ToString() on all those ID's and other non-string values since you're concatenating a string. Maybe, somehow, the values are interpreted as charcodes (because they're not explicitly casted as string) and thus causing weird characters.
Another guess would be: are you actually building 15 million queries in-memory before sending them to the database? Or does each query get sent to the DB seperately? Maybe the fact that you're trying to store a huge-ass string in-memory causes some problems (although it shouldn't cause the problem you're describing here).
You might want to consider replacing the gigantic update with two CASE selectors and one IN with individual UPDATE statements for each row. I know that the problem is not caused by this, but it is probably a cleaner and more efficient solution. If your DB connector supports multiple statements per execution, you could do something like the following:
int batchSize = 0;
StringBuilder sb = new StringBuilder();
for (Route r: routes) {
sb.append("UPDATE VanNaar SET")
.append(" time = '").append(r.driveTime).append("',")
.append(" distance = '").append(r.distance).append("'")
.append(" WHERE ID = '").append(r.ID).append("'; ");
if (++batchSize == BATCH_SIZE) {
connector.exec(sb.toString());
sb = new StringBuilder();
batchSize = 0;
}
}
if (sb.length() > 0) {
connector.exec(sb.toString());
}
This will let StringBuilder take care of transforming whatever the underlying value type is into a String. If driveTime, distance, or ID are already strings, then you will have to escape them properly using the appropriate JDBC methods.
You'd probably be better off using prepared statements for this sort of thing as suggested by #RobIII since they would take care of the SQL injection problems automatically.

Categories