{"id":13537687,"url":"https://github.com/square/sqlbrite","last_synced_at":"2026-01-10T15:35:28.884Z","repository":{"id":27520530,"uuid":"31001422","full_name":"square/sqlbrite","owner":"square","description":"A lightweight wrapper around SQLiteOpenHelper which introduces reactive stream semantics to SQL operations.","archived":true,"fork":false,"pushed_at":"2020-08-17T16:20:56.000Z","size":754,"stargazers_count":4574,"open_issues_count":5,"forks_count":422,"subscribers_count":210,"default_branch":"trunk","last_synced_at":"2024-04-13T17:52:13.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://square.github.io/sqlbrite/3.x/sqlbrite/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/square.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-19T04:38:37.000Z","updated_at":"2024-03-31T14:15:47.000Z","dependencies_parsed_at":"2022-08-07T13:00:22.886Z","dependency_job_id":null,"html_url":"https://github.com/square/sqlbrite","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/square%2Fsqlbrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/square%2Fsqlbrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/square%2Fsqlbrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/square%2Fsqlbrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/square","download_url":"https://codeload.github.com/square/sqlbrite/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234555910,"owners_count":18851870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T09:01:02.130Z","updated_at":"2025-09-28T19:31:55.606Z","avatar_url":"https://github.com/square.png","language":"Java","readme":"SQL Brite\n=========\n\nA lightweight wrapper around `SupportSQLiteOpenHelper` and `ContentResolver` which introduces reactive\nstream semantics to queries.\n\n# Deprecated\n\nThis library is no longer actively developed and is considered complete.\n\nIts database features (and far, far more) are now offered by [SQLDelight](https://github.com/cashapp/sqldelight/)\nand its [upgrading guide](https://github.com/cashapp/sqldelight/blob/1.0.0/UPGRADING.md) offers some\nmigration help.\n\nFor content provider monitoring please use [Copper](https://github.com/cashapp/copper) instead.\n\n\n\nUsage\n-----\n\nCreate a `SqlBrite` instance which is an adapter for the library functionality.\n\n```java\nSqlBrite sqlBrite = new SqlBrite.Builder().build();\n```\n\nPass a `SupportSQLiteOpenHelper` instance and a `Scheduler` to create a `BriteDatabase`.\n\n```java\nBriteDatabase db = sqlBrite.wrapDatabaseHelper(openHelper, Schedulers.io());\n```\n\nA `Scheduler` is required for a few reasons, but the most important is that query notifications can\ntrigger on the thread of your choice. The query can then be run without blocking the main thread or\nthe thread which caused the trigger.\n\nThe `BriteDatabase.createQuery` method is similar to `SupportSQLiteDatabase.query` except it takes an\nadditional parameter of table(s) on which to listen for changes. Subscribe to the returned\n`Observable\u003cQuery\u003e` which will immediately notify with a `Query` to run.\n\n```java\nObservable\u003cQuery\u003e users = db.createQuery(\"users\", \"SELECT * FROM users\");\nusers.subscribe(new Consumer\u003cQuery\u003e() {\n  @Override public void accept(Query query) {\n    Cursor cursor = query.run();\n    // TODO parse data...\n  }\n});\n```\n\nUnlike a traditional `query`, updates to the specified table(s) will trigger additional\nnotifications for as long as you remain subscribed to the observable. This means that when you\ninsert, update, or delete data, any subscribed queries will update with the new data instantly.\n\n```java\nfinal AtomicInteger queries = new AtomicInteger();\nusers.subscribe(new Consumer\u003cQuery\u003e() {\n  @Override public void accept(Query query) {\n    queries.getAndIncrement();\n  }\n});\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 1\n\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"jw\", \"Jake Wharton\"));\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"mattp\", \"Matt Precious\"));\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"strong\", \"Alec Strong\"));\n\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 4\n```\n\nIn the previous example we re-used the `BriteDatabase` object \"db\" for inserts. All insert, update,\nor delete operations must go through this object in order to correctly notify subscribers.\n\nUnsubscribe from the returned `Subscription` to stop getting updates.\n\n```java\nfinal AtomicInteger queries = new AtomicInteger();\nSubscription s = users.subscribe(new Consumer\u003cQuery\u003e() {\n  @Override public void accept(Query query) {\n    queries.getAndIncrement();\n  }\n});\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 1\n\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"jw\", \"Jake Wharton\"));\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"mattp\", \"Matt Precious\"));\ns.unsubscribe();\n\ndb.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"strong\", \"Alec Strong\"));\n\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 3\n```\n\nUse transactions to prevent large changes to the data from spamming your subscribers.\n\n```java\nfinal AtomicInteger queries = new AtomicInteger();\nusers.subscribe(new Consumer\u003cQuery\u003e() {\n  @Override public void accept(Query query) {\n    queries.getAndIncrement();\n  }\n});\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 1\n\nTransaction transaction = db.newTransaction();\ntry {\n  db.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"jw\", \"Jake Wharton\"));\n  db.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"mattp\", \"Matt Precious\"));\n  db.insert(\"users\", SQLiteDatabase.CONFLICT_ABORT, createUser(\"strong\", \"Alec Strong\"));\n  transaction.markSuccessful();\n} finally {\n  transaction.end();\n}\n\nSystem.out.println(\"Queries: \" + queries.get()); // Prints 2\n```\n*Note: You can also use try-with-resources with a `Transaction` instance.*\n\nSince queries are just regular RxJava `Observable` objects, operators can also be used to\ncontrol the frequency of notifications to subscribers.\n\n```java\nusers.debounce(500, MILLISECONDS).subscribe(new Consumer\u003cQuery\u003e() {\n  @Override public void accept(Query query) {\n    // TODO...\n  }\n});\n```\n\nThe `SqlBrite` object can also wrap a `ContentResolver` for observing a query on another app's\ncontent provider.\n\n```java\nBriteContentResolver resolver = sqlBrite.wrapContentProvider(contentResolver, Schedulers.io());\nObservable\u003cQuery\u003e query = resolver.createQuery(/*...*/);\n```\n\nThe full power of RxJava's operators are available for combining, filtering, and triggering any\nnumber of queries and data changes.\n\n\n\nPhilosophy\n----------\n\nSQL Brite's only responsibility is to be a mechanism for coordinating and composing the notification\nof updates to tables such that you can update queries as soon as data changes.\n\nThis library is not an ORM. It is not a type-safe query mechanism. It won't serialize the same POJOs\nyou use for Gson. It's not going to perform database migrations for you.\n\nSome of these features are offered by [SQL Delight][sqldelight] which can be used with SQL Brite.\n\n\n\nDownload\n--------\n\n```groovy\nimplementation 'com.squareup.sqlbrite3:sqlbrite:3.2.0'\n```\n\nFor the 'kotlin' module that adds extension functions to `Observable\u003cQuery\u003e`:\n```groovy\nimplementation 'com.squareup.sqlbrite3:sqlbrite-kotlin:3.2.0'\n```\n\n\nSnapshots of the development version are available in [Sonatype's `snapshots` repository][snap].\n\n\n\nLicense\n-------\n\n    Copyright 2015 Square, Inc.\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n\n\n\n\n [snap]: https://oss.sonatype.org/content/repositories/snapshots/\n [sqldelight]: https://github.com/square/sqldelight/\n","funding_links":[],"categories":["Java","Index","Libs","Library","O/R Mapping","并发编程","Database","Bindings"],"sub_categories":["RxJava","\u003cA NAME=\"Orm\"\u003e\u003c/A\u003eOrm","一些原理分析的文章"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquare%2Fsqlbrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsquare%2Fsqlbrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquare%2Fsqlbrite/lists"}