Opening Connections
How do I create a database in my application?
How do I create a database in my application?
First choose a proper location for the database file. Document-based applications will let the user pick a location. Apps that use the database as a global storage will prefer the Application Support directory.The sample code below creates or opens a database file inside its dedicated directory (a recommended practice). On the first run, a new empty database file is created. On subsequent runs, the database file already exists, so it just opens a connection:
How do I open a database stored as a resource of my application?
How do I open a database stored as a resource of my application?
Open a read-only connection to your resource:
How do I close a database connection?
How do I close a database connection?
Database connections are automatically closed when
DatabaseQueue or DatabasePool instances are deinitialized.If the correct execution of your program depends on precise database closing, perform an explicit call to close(). This method may fail and create zombie connections, so please check its detailed documentation.SQL and Debugging
How do I print a request as SQL?
How do I print a request as SQL?
When debugging a request that doesn’t deliver expected results, you can compile the request into a prepared statement:Or setup a tracing function:
The generated SQL may change between GRDB releases without notice: don’t have your application rely on any specific SQL output.
How do I monitor the duration of database statements execution?
How do I monitor the duration of database statements execution?
Use the
trace(options:_:) method with the .profile option:Features and Compatibility
What are experimental features?
What are experimental features?
Since GRDB 1.0, all backwards compatibility guarantees of semantic versioning apply: no breaking change will happen until the next major version of the library.There is an exception: experimental features, marked with the ”🔥 EXPERIMENTAL” badge. These are advanced features that are too young or lack user feedback. They are not stabilized yet.Experimental features are not protected by semantic versioning and may break between two minor releases of the library. Your feedback is greatly appreciated to help them become stable.
Does GRDB support library evolution and ABI stability?
Does GRDB support library evolution and ABI stability?
No, GRDB does not support library evolution and ABI stability. The only promise is API stability according to semantic versioning, with an exception for experimental features.However, GRDB can be built with the “Build Libraries for Distribution” Xcode option (
BUILD_LIBRARY_FOR_DISTRIBUTION), so you can build binary frameworks at your convenience.Associations
How do I filter records and only keep those that are associated to another record?
How do I filter records and only keep those that are associated to another record?
Use This fetches only books that have an author, discarding anonymous ones.
joining(required:) to only fetch records that have an association:How do I filter records and only keep those that are NOT associated to another record?
How do I filter records and only keep those that are NOT associated to another record?
Use a combination of optional join and filter:
How do I select only one column of an associated record?
How do I select only one column of an associated record?
Use
annotated(withOptional:) to append specific columns:ValueObservation
Why is ValueObservation not publishing value changes?
Why is ValueObservation not publishing value changes?
There may be four possible reasons:See the ValueObservation documentation for more troubleshooting tips.
- Changes not committed: The expected changes were not committed into the database
- Changes overwritten: The changes were committed but quickly overwritten
- Observation stopped: The observation was stopped or cancelled
- Wrong region tracked: The observation doesn’t track the expected database region
Need More Help?
GitHub Discussions
Ask questions and get help from the community
Swift Forums
Discuss GRDB on the Swift forums
Report Issues
Found a bug? Report it on GitHub
Documentation
Browse the complete API reference