Xamarin SQLite-Pcl-Net DateTIme in Query - xamarin

I am using Xamarin Prism along with Sqlite-pcl-net Nuget .
While I write Query to obtain the elements of table between a range of dates as follows
public Task<List<JournalModel>> GetRecieptListAsync(DateTime startdate,DateTime enddate)
{
return database.QueryAsync<JournalModel>("SELECT * FROM [JournalModel] WHERE ( [PromisedDate] BETWEEN " + startdate + " AND " + enddate + ")");
}
StartD and EndD are variables of DateTime Datatype of Format:
StartD MM/dd/yyyy 00:00:00
EndD MM/dd/yyyy 23:59:59
After executing this Query I get a
SQLite.SQLiteException: near "12": syntax error
Which means at the hour of StartD. I hope You know it takes 12:00:00Am at StartD in the Database. Comment if I have to make more clearifications.

In SQLite-pcl-net the default format for Datetime is Ticks.
You can change startdate to startdate.Ticks and enddate to enddate.Ticks.
return database.QueryAsync<JournalModel>("SELECT * FROM [JournalModel] WHERE ( [PromisedDate] BETWEEN " + startdate.Ticks + " AND " + enddate.Ticks + ")");

Related

Java 8 - SQL Timestamp to Instant with properly formatted time

I've read through the available q and a on SO, but nothing I have found answers my question of how to format my time in 12hour format.
Following is my code that runs a query on a MySQL database and returns results, checking to see if an appointment is within 15 minutes of login so an alert can pop.
public void apptCheck(int userId) throws SQLException {
// this method checks for an appointment occurring within 15 minutes of login
Statement apptStatement = DBQuery.getStatement();
String apptQuery = "Select apt.start, cs.customerName from DBName.appointment apt "
+ "JOIN DBName.customer cs ON cs.customerId = apt.customerId WHERE "
+ "userId = " + userId + " AND start >= NOW() AND start < NOW() + interval 16 minute";
apptStatement.execute(apptQuery);
ResultSet apptRs = apptStatement.getResultSet();
while(apptRs.next()) {
Timestamp apptTime = apptRs.getTimestamp("start");
ResourceBundle languageRB = ResourceBundle.getBundle("wgucms/RB", Locale.getDefault());
Alert apptCheck = new Alert(AlertType.INFORMATION);
apptCheck.setHeaderText(null);
apptCheck.setContentText(languageRB.getString("apptSoon") + " " + apptTime.toInstant().atZone(ZoneId.systemDefault()));
apptCheck.showAndWait();
}
My result is:
I want the time to display 3:00, not the 19:00 - 06:00. How can I make that happen?
ZonedDateTime zonedDateTime=ZonedDateTime.of(apptTime.toLocalDateTime(),ZoneId.systemDefault());
You can use ZonedDateTime and format the time as you want.
docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html ZonedDateTime has a lot of features you can see all here and you can get the hour, minute, day etc.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String formattedString = zonedDateTime.format(formatter);
if you only want time in 12hour format you can use this
I found the solution which will perform the UTC to local time conversion and then format the time so that the resulting alert is in 12 hour time format without the date or time zone info. Here is the full code:
while(apptRs.next()) {
Timestamp apptTime = apptRs.getTimestamp("start");
// perform time conversion from UTC to User Local Time
ZoneId zidApptTime = ZoneId.systemDefault();
ZonedDateTime newZDTApptTime = apptTime.toLocalDateTime().atZone(ZoneId.of("UTC"));
ZonedDateTime convertedApptTime = newZDTApptTime.withZoneSameInstant(zidApptTime);
ResourceBundle languageRB = ResourceBundle.getBundle("wgucms/RB", Locale.getDefault());
Alert apptCheck = new Alert(AlertType.INFORMATION);
apptCheck.setHeaderText(null);
// set the Alert text and format in 12 hour format
apptCheck.setContentText(languageRB.getString("apptSoon") +
convertedApptTime.toInstant().atZone(ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("h:mm a")) + ".");
apptCheck.showAndWait();
}

Update datetime and a string in Memo field using Java and UCanAccess

I want to update datetime and a string in Memo field using UCanAccess. For example - 5/27/2015 System : some string, this is all I have to update in memo field.
What I have tried - I am converting date to a string using following code:
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
and then using update query of ucanaccess like this:
st.executeUpdate(" update tblCaseInventory set fldContactNotes = " + reportDate + "' System : CAR Report '" + " where fldCaseNumber = " + rs1.getInt("fldCaseNumber"));
but I am getting an error:
Unexpected token : System.
How to achieve this?
You should use a PreparedStatement to run a parameterized query, something like this:
String sql =
"UPDATE tblCaseInventory " +
"SET fldContactNotes=? " +
"WHERE fldCaseNumber=? ";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
java.util.Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
ps.setString(1, reportDate + " System : CAR Report");
ps.setInt(2, rs1.getInt("fldCaseNumber"));
ps.executeUpdate();
}

Birt report parameter passing

Hi i have to pass parameter to a report...with a concat-date ....but its showing error...
like this...am having in list box with 2011-2012,2012-2013...here is my oracle query
DATE FOMART IN DATABASE:25-Mar-2012
oracle query
select tran_date,
ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2)APC
from OUTLET_PAYMODE_REPORT_FACT
Before Open:
var sqld= new String();
yearParam = params["Year"].value;
yearParam = yearParam.split("-");
startYear = yearParam[0];
endYear = yearParam[1];
this.queryText =this.queryText +" where TRAN_DATE between '" + startYear + "01-Mar-' and '" + endYear + "30-Mar-'"+
"group by tran_date"
but am getting error like this.....
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-01861: literal does not match format string
I think, your sql string should be
" where TRAN_DATE between '01-Mar-" + startYear +
"' and '30-Mar-" + endYear + "' " +
"group by tran_date"

JDBC - How do I accumulate data from ResultSet

How would I accumulate data from a ResultSet? What I'm trying to do is get the price from the ResultSet and it only gets the first value, but what I want is every price for e.g 25.0, 10.00, and 15.00 I have stored them in a Double datatype, but the Result Set only gets the last value when I have a while(rs.next() and if I have if(rs.next()) it will get 25.00 but I want all the prices to be added. How would I do this exactly? Thanks!
if(rs2.next()) {
jTextArea3.append("\n");
jTextArea3.append("\n");
jTextArea3.append("Part Type: " + rs2.getString("PartType"));
jTextArea3.append("\n");
jTextArea3.append("Description: " + rs2.getString("Description"));
jTextArea3.append("\n");
jTextArea3.append("Price: " + rs2.getString("Price"));
partsCost = rs2.getDouble("Price");
System.out.println(partsCost);
}
Maybe something like this is what you mean?
double total = 0;
while(rs.next()) {
jTextArea3.append("\n");
jTextArea3.append("\n");
jTextArea3.append("Part Type: " + rs2.getString("PartType"));
jTextArea3.append("\n");
jTextArea3.append("Description: " + rs2.getString("Description"));
jTextArea3.append("\n");
jTextArea3.append("Price: " + rs2.getString("Price"));
partsCost = rs2.getDouble("Price");
System.out.println(partsCost);
total += partsCost;
}
System.out.println(total);
You don't show the SQL query. If you only care about the total of partsCosts, and not the other details, you would need to change your query to have an aggregate function such as, "select sum(partsCost)... "

Issue using Oracle date function with Spring's NamedParamenterJdbcTemplate

I'm having an issue trying to get my SQL query which works fine in SQL Developer (Oracles free database tool) to also work using Spring's NamedParameterJdbcTemplate class.
My query is:
String sql = " SELECT COUNT(*) FROM ( " +
" SELECT FE.USR_ID, MAX(FE.DATE_FIRST_SUB) AS SUB_DATE " +
" FROM FC, FE " +
" WHERE FC_STATUS = 'MEMBER' " +
" AND FC.FC_SPC_ID = :spcId " +
" AND FE.FE_USR_ID = FC.FC_USR_ID " +
" AND FE.DATE_FIRST_SUB IS NOT NULL " +
" GROUP BY FE_USR_ID " +
" ) " +
" WHERE SUB_DATE BETWEEN TO_DATE('01-JUN-2011', 'DD-MON-YYYY') AND TO_DATE('01-JUL-2011', 'DD-MON-YYYY') ";
It has something to do with my dates, formatting perhaps? When I don't use the WHERE clause in the outer select it works, when it's included 0 is returned from the count - as I mentioned running the SQL directly returns expected results.
Any advice?
Thanks.
Oh my, I was in fact looking at the wrong database!!

Resources