sqLite error when using update statement - java

I am busy using a SQLite database with a java application and after updating the database successfully I get the following error
org.sqlite.jdbc4.JDBC4PreparedStatement#4e1c6f
Below is my update code
PreparedStatement update = con.prepareStatement("UPDATE highLeaker SET " + cyc + " = "
+ dataArray[3] + " , "
+ "gasSurveyOperator" + " = "
+ dataArray[1] + " , "
+ "gasSurveySerial" + " = "
+ dataArray[2] + " , "
+ "loss" + " = "
+ dataArray[4] + " , "
+ "comment" + " = "
+ dataArray[5] + " , "
+ "cycle" + " = '"
+ cycleT + "' , "
+ "date" + " = "
+ dataArray[6]
+ " WHERE leakerID = " + dataArray[0]+";");
System.out.println(update);
update.executeUpdate();
dataRow = CSVFile.readLine(); // Read next line of data.

You printed out the object. Also, I would highly recommend using place holders instead when executing SQL queries.

Related

Sqlite update operations can I add value existing value?

"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
point + " WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
this query updates my value, but I want it to add to existing value. How can I fix this problem ?
I guess you want to add the value of point to the existing value of DatabaseFıelds.SUBJECT_PROGRESS, right? You can do it like this:
"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
it's works Thank you forpas"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;

JDBC query returning zero when using simple arithmetic operations and alias even though the data in table is not zero

I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"

Sqlite Database not updating using java

I am trying to update my new database but for some reason after executing my Prepared Statement the data remains the same. It returns no errors or any exceptions. Please have a look at my code and see if you can help me. Thanks in advance.
try {
connect();
PreparedStatement update = con.prepareStatement("UPDATE generalInfo SET "
+ "site" + " = '"
+ siteTt + "' , "
+ "area" + " = '"
+ areaTt + "' , "
+ "unit" + " = '"
+ unitTt + "' , "
+ "unitName" + " = '"
+ unitNameTt + "' , "
+ "drawing" + " = '"
+ drawingTt + "' , "
+ "system" + " = '"
+ systemTt + "' , "
+ "stream" + " = '"
+ streamTt + "' , "
+ "product" + " = '"
+ productTt + "' , "
+ "equipmentLoc" + " = '"
+ equipLocTt + "' , "
+ "specificLoc" + " = '"
+ specificLocTt + "' , "
+ "camOperator" + " = '"
+ camTechTt + "' , "
+ "camSerial" + " = '"
+ camSerialTt + "' , "
+ "gasSurveyOperator" + " = '"
+ surveyTechTt + "' , "
+ "gasSurveySerial" + " = '"
+ surveySerialTt + "' , "
+ "equipmentDesc" + " = '"
+ equipDescTt + "' , "
+ "equipmentType" + " = '"
+ equipTypeTt + "' , "
+ "equipmentSize" + " = '"
+ equipSizeTt + "' , "
+ "equipmentID" + " = '"
+ equipIDTt + "' , "
+ "[Maintenance type]" + " = '"
+ repairTt + "' , "
+ "measurementPosition" + " = '"
+ sourceTt + "'"
+ " WHERE leakerID = " + Integer.parseInt(leakerIDCombo.getSelectedItem().toString())+";");
update.closeOnCompletion();
update.executeUpdate();
con.close();
System.out.println("saved");
} catch (SQLException ex) {
Logger.getLogger(RefineryData.class.getName()).log(Level.SEVERE, null, ex);
}
never forgett to commit your changes:
con.setAutoCommit(true);
update.closeOnCompletion();
update.executeUpdate();
con.close();
or
update.closeOnCompletion();
update.executeUpdate();
con.commit();
con.close();

Using SQlite to populate expandable list duplicating data into headers

I am trying to populate my expandable list from a SQLite DB, I got the expandable list tutorial from androidhive but it was for static data.
I got it working about 80% but now my problem is that the data gets populated under all the headers and not their unique headers. I think my problem is because all header and child data is using the same arrays and need to be unique to the headers?
What I was thinking was creating arrays dynamically with variable names but I have no idea how to do that and can't think of another way of doing this since this is my first time working with expandable lists.
Here is my code. I also have a feeling the way I am doing the nested cursors isn't the right way
String sqlQ = "SELECT Distinct "
+ "strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") header " +
" FROM " + SQLdb.DICTIONARY_TABLE_NAME
+ " WHERE " + SQLdb.DB_PACKAGE + " = '"+ appPackage +"'"
+ " ORDER BY " + SQLdb.DB_TIMEOPEN + " ASC";
Cursor mCursor = dataBase.rawQuery(sqlQ, null);
Log.i("SQL",sqlQ);
if (mCursor.moveToFirst()) {
do {
listDataHeader.add(mCursor.getString(mCursor
.getColumnIndex("header")));
String sqlQ2 = "SELECT " + SQLdb.DB_NAME + ", "
+ SQLdb.DB_PACKAGE + ", "
+ SQLdb.DB_TIMEOPEN + ", "
+ SQLdb.DB_TIMECLOSED + ", "
+ SQLdb.DB_DURATION + ", "
+ "strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") header " +
" FROM " + SQLdb.DICTIONARY_TABLE_NAME
+ " WHERE " + SQLdb.DB_PACKAGE + " = '"+ appPackage +"'"
+ " AND strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") = '"+ mCursor.getString(mCursor
.getColumnIndex("header")) +"'"
+ " ORDER BY " + SQLdb.DB_TIMEOPEN + " ASC";
Cursor mCursor2 = dataBase.rawQuery(sqlQ2, null);
if (mCursor2.moveToFirst()) {
do {
listDataChildInner.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_DURATION)));
listDataChildInnerO.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMEOPEN)));
listDataChildInnerC.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMECLOSED)));
Log.i("ACT CHILDT",mCursor.getString(mCursor
.getColumnIndex("header")) + " -> " + mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMEOPEN)));
} while (mCursor2.moveToNext());
}
listDataChild.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInner);
listDataChildO.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInnerO);
listDataChildC.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInnerC);
} while (mCursor.moveToNext());
}

Writing the resultset to csv file

I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...
You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.
You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...

Categories