SQL Query Issue iOS

90 views Asked by At

I'm working on an app that uses SQLite database, I mostly use inloop and DB Browser for SQLite on my Mac. In the iOS app (Develop with Swift), I'm using the GRDB library for SQLite.

The problem is some of the queries are working in the DB Browser for SQLite but not working on inloop (No any error also same time not showing any result) for example below query,

select
  sum(e.amount) as Total,
  strftime("%Y%m%d", entrydate / 1000, 'unixepoch') as day,
  count(c.cid) as trxcount,
  group_concat(
    DISTINCT(
      c.first_name || ' ' || ifnull(c.middle_name, '') || ' ' || ifnull(c.last_name, '')
    )
  ) as parties
from
  LedgerEntry e
  inner join Customer c on e.customer_id = c.cid
where
  e.entrydate >= 1655271792800
  and e.entrydate <= 1655271792868
  and e.type = 0
group by
  day
order by
  day asc

The question is not about the inloop problem but whatever queries are not working in the inloop are also not working with GRDB in my app. Can someone please explain what's the main reason for this? The app is developed with Swift 5

Database connection code.

import Foundation
import GRDB


class SBC_DailyBalanceByType : Codable, FetchableRecord, PersistableRecord {
    var total : Double?
    var day : Int?
    var trxcount : Int?
    var parties : String?
}


class SB_DailyBalanceByType : NSObject {
    
    
    static let shared = SB_DailyBalanceByType()
    static var arrData = [SBC_DailyBalanceByType]()
    
    
    func GetBal(type: Int, startDate:Int, endDate:Int, completion: @escaping ([SBC_DailyBalanceByType]?) -> ()) {
        
        do {
            ///GRDB
            let dbQueue = try DatabaseQueue(path: SBGlobal.filePath)
            
            //1655271792800
            //1655271792868
            
            let query = "select sum(e.amount) as Total,strftime(\"%Y%m%d\", entrydate / 1000, 'unixepoch') as day,count(c.cid) as trxcount,group_concat(DISTINCT(c.first_name || ' ' || ifnull(c.middle_name, '') || ' ' || ifnull(c.last_name, ''))) as parties from LedgerEntry e inner join Customer c on e.customer_id = c.cid where e.entrydate >= \(1655271792800) and e.entrydate <= \(1655271792868) and e.type = \(0) group by day order by day asc"


            let players: [SBC_DailyBalanceByType] = try dbQueue.read { db in
                try SBC_DailyBalanceByType.fetchAll(db, sql: query)
            }
            
            SB_DailyBalanceByType.arrData.append(contentsOf: players)
            
            print(players)

        } catch {
            print (error)
        }
        completion(SB_DailyBalanceByType.arrData)
    }
    
}

However, I made some changes in the query for an alternative solution that works with GRDB. So I assume that some problem with strftime function.

SELECT
  SUM(e.amount) AS Total,
  SUBSTR(DATE('1970-01-01', '+' || (e.entrydate / 1000) || ' SECONDS'), 1, 10) AS day,
  COUNT(c.cid) AS trxcount,
  GROUP_CONCAT(
    COALESCE(c.first_name, '') || ' ' || COALESCE(c.middle_name, '') || ' ' || COALESCE(c.last_name, '')
  ) AS parties
FROM
  LedgerEntry e
  INNER JOIN Customer c ON e.customer_id = c.cid
WHERE
  e.entrydate BETWEEN 1655271792800
  AND 1655271792868
  AND e.type = 0
GROUP BY
  day
ORDER BY
  day ASC;
0

There are 0 answers