Code Monkey home page Code Monkey logo

Comments (5)

nerdyc avatar nerdyc commented on June 14, 2024

for-loop support was removed to make handling errors more compatible with swift 2 errors. Instead of a for-loop, you use the next() method:

var results = try db.prepareStatement("SELECT * FROM people")
while try results.next() {
  var row = results.dictionaryValue
  // or whatever you want to do with the row
}

Since it's common to simply transform a row into some object value, you can also use the selectFrom method on the Database object, or the select method from the compiled statement. Pass these methods a block, and they will return an array of all treated rows. No need to call next() yourself.

from squeal.

Persilos avatar Persilos commented on June 14, 2024

Thank you for your response.
I have a second question: if in example you posted, i want to get values of dictionary in a String array
So i do:
var arrayValues = Array(row.values)
But values are Bindable. I search documentation to know how cast it in String (all values from my database are String) from Bindable but i dont find it.

from squeal.

 avatar commented on June 14, 2024

@Persilos, Bindable is just a protocol, so the underlying value may be any value that implements the Bindable protocol. If you know for sure that all the Bindable values are strings, you can do:
var arrayValues = row.values.map({ $0 as String })
That will simply cast all the values to strings. If you don't know for sure that all values are strings, you can do:
var arrayValues = row.values.map({ $0 as? String })
In that case you are dealing with optional values and you need keep that in mind as you use them.

These functions are not specific to Squeal, but because Squeal returns Bindable values, you need handle using the returned data correctly.

Hope that helps.

from squeal.

nerdyc avatar nerdyc commented on June 14, 2024

There are also methods that take blocks, prefixed with select, that are likely more efficient. And the Statement object supports subscripting by column name or index too.

Something like:

let emails = try db.selectFrom("people") { $0["email"] as String }

// equivalent with a statement
let results = try db.prepareStatement("SELECT * FROM people")
let moreEmails = try results.select { $0["email"] as String }

from squeal.

Persilos avatar Persilos commented on June 14, 2024

Thanks :)

from squeal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.