I'm building an Android app that contain SQLite DB and I was having a problem with translating SQLite saved data then I find a solution which is a method to get resource id of strings, and it worked just fine, the app was working fine then suddenly this error showed I don't know why, please help
The method, I used to get resource for string:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
The code in the adapter
textViewname.setText(context.getString(R.string.product_Name)+" " + repo.getName());
textViewquantity.setText(context.getString(R.string.last_added_qty)+" " + repo.getQuantity() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewdate.setText(context.getString(R.string.entry_date)+" " + repo.getDate()+ " /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewTotal.setText(context.getString(R.string.total_qty)+" " + repo.getTotal() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textviewlastUpdated.setText(context.getString(R.string.last_updated_date)+" " + repo.getUpdateDate() +" /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewid.setText(context.getString(R.string.id)+" " + repo.getId());
textViewOutputqty.setText(context.getString(R.string.last_out_qty)+" " + repo.getOutputqty() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewpriceofsingleitem.setText(context.getString(R.string.retail_price)+" " + repo.getPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewpriceofMultipleitem.setText(context.getString(R.string.wholesale_price)+" " + repo.getPriceofmultipleInput() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitSIngle.setText(context.getString(R.string.retail_profit)+" " + repo.getProfitSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitMultiple.setText(context.getString(R.string.wholesale_profit)+" " + repo.getProfitMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewPurchasingPrice.setText(context.getString(R.string.purchasing_price)+" " + repo.getPurchasingPricePeranItem() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalPurchasingPrice.setText(context.getString(R.string.total_purchase_price)+" " + repo.getTotalPurchasingPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitSingle.setText(context.getString(R.string.total_retail_profit)+" " + repo.getTotalProfitperSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitMultiple.setText(context.getString(R.string.total_wholesale_profit)+" "+ repo.getTotalProfitPerMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewitemsIBox.setText(context.getString(R.string.num_unitsInBox)+" "+ repo.getItemsInBox());
textViewSinglePriceOfiteminBox.setText(context.getString(R.string.retail_price_perUnit)+ " " + repo.getSinglePriceofItemInBox()+" " +context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewMultipriceOfItemInBox.setText(context.getString(R.string.wholesale_price_perUnit)+" "+repo.getMultiplePriceofitemInBox()+" "+context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewtotalItemsInBoexs.setText(context.getString(R.string.total_units)+" "+ repo.getTotalitemsInboxes() +" "+ context.getResources().getString(R.string.Pieces));
This error happens because you are trying to get resource that does not exist. It is because you store memory address which allocate to your string resource and possible it will change next time. I do not know what is your scenario but why you store it already in your strings it will never lost. I can help you to build another logic only when you tell what are you trying to achieve.
"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;
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.
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());
}
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
...