{"id":18001545,"url":"https://github.com/hehonghui/simpledb","last_synced_at":"2025-10-30T20:56:27.904Z","repository":{"id":146185647,"uuid":"65609801","full_name":"hehonghui/simpledb","owner":"hehonghui","description":"A Lightweight android database library","archived":false,"fork":false,"pushed_at":"2016-08-15T04:09:00.000Z","size":362,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T11:50:36.011Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hehonghui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-13T09:22:51.000Z","updated_at":"2016-12-15T02:24:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"9cad9d5b-cc20-4a07-986e-a020f8037647","html_url":"https://github.com/hehonghui/simpledb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fsimpledb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fsimpledb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fsimpledb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hehonghui%2Fsimpledb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hehonghui","download_url":"https://codeload.github.com/hehonghui/simpledb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245618565,"owners_count":20645023,"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-10-29T23:17:55.208Z","updated_at":"2025-10-30T20:56:22.839Z","avatar_url":"https://github.com/hehonghui.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simpledb 开源框架\n\n![](images/simpledb.png) \n\n一个轻量级的 Android 异步数据库框架. \n\n* 异步操作\n* 使用简单、方便\n* 便于数据库升级\n* 非ORM，关系映射自行处理\n* 精简小巧, 小于 30 kb\n\n\u003e 适合的用户 ： \n\u003e \n\u003e * 不想使用太大的数据库开源库 ( 对apk大小有要求 )\n\u003e * 不想使用ORM 框架\t\t\t\n\n## 下载\n\n[simpledb-1.0.jar 下载](output/simpledb-1.0.jar)\n\n## 一、初始化数据库\n\n```\nimport com.simple.database.Builder;\n\n/**\n* 初始化数据库\n*/\nprivate void initDatabase() {\n    new Builder(getApplicationContext())\n        .setDbName(\"demo.db\")              // 数据库名为 demo.db\n        .setDbVersion(1)                   // 数据库版本号为1\n        .setCreateSqlFile(\"db/create.sql\") // 创建数据库表的sql文件在 asserts/db/create.sql文件中\n        .setUpgradePath(\"db/migrations\")  // 数据库升级文件在 asserts/db/migrations 目录中\n        .create();\n }\n```\n\n## 二、创建数据库表的sql语句\n\n创建数据库的sql语句我们存放在 assets 目录中, 该文件可以在构建Builder时自行设置， 默认为 assets/db/create.sql文件。\n\n![](images/project-structure.png)\n\ncreate.sql 为标准的sql语句, 示例如下: \n\n```\n-- 用户表\nCREATE TABLE users (\n\t\t\t id TEXT unique ,\n\t\t\t name TEXT not null,\n\t\t\t gender INTEGER\n) ;\n\n-- 书籍表\nCREATE TABLE books (\n\t\t\t\tid TEXT NOT NULL unique,\n            \tname TEXT NOT NULL\n) ;\n\n-- 借阅表\nCREATE TABLE borrow (\n\t\t\t\tuser_id TEXT NOT NULL,\n            \tbook_id TEXT NOT NULL, \n            \tUNIQUE(user_id, book_id)\n ) ;\n\n```\n\n## 三、数据库的增删改查\n\n\n首先建立 操作数据库表的DAO对象, 例如 : \n\n```\n/**\n * books 表的DAO对象。\n */\npublic class BookDao extends AbsDAO\u003cBook\u003e {\n\n    public BookDao() {\n        super(\"books\");\n    }\n\n    @Override\n    protected ContentValues convert(Book item) {\n        ContentValues newValues = new ContentValues();\n        newValues.put(\"id\", item.id);\n        newValues.put(\"name\", item.name);\n        return newValues;\n    }\n\n    @Override\n    protected Book parseOneItem(Cursor cursor) {\n        Book item = new Book();\n        item.id = cursor.getString(0);\n        item.name = cursor.getString(1) ;\n        return item;\n    }\n}\n```\n\n增删改查接口，四个接口的参数都是 DAO的class, 例如 BookDao.class 。\n\n* insertInto(Class daoClass) : 插入数据\n* deleteFrom(Class daoClass) : 删除数据\n* updateFrom(Class daoClass) : 更新数据\n* selectFrom(Class daoClass) : 查询数据\n\n更多的接口文档请移步 : [api 文档](javadoc/index.html)\n\n上述四个接口都是 DatabaseHelper类中的静态共有函数，因此静态import之后就可以直接使用上述接口，使用示例如下: \n\n```\nimport static com.simple.database.DatabaseHelper.deleteFrom;\nimport static com.simple.database.DatabaseHelper.insertInto;\nimport static com.simple.database.DatabaseHelper.selectFrom;\nimport static com.simple.database.DatabaseHelper.updateFrom;\n\n\npublic class BookModel {\n\n    public void insertBook(Book aBook) {\n    \t  // 插入图书\n        insertInto(BookDao.class).withItem(aBook).execute();\n    }\n\n    public void deleteBook(Book aBook) {\n        // 根据Id 删除图书\n        deleteFrom(BookDao.class)\n        \t.where(\"id=?\", new String[]{aBook.id}).execute();\n    }\n\n    public void updateBook(Book aBook) {\n        // 更新数据\n        updateFrom(BookDao.class).withItem(aBook)\n              .where(\"id=?\", new String[]{aBook.id}).execute();\n    }\n\n    public void queryAllBook(DbListener\u003cList\u003cBook\u003e\u003e listener) {\n    \t // 查询所有图书\n        selectFrom(BookDao.class).listener(listener).execute();\n    }\n\n    public void queryBook(String selection, String[] args, \n    \t\t\t\t\t\tDbListener\u003cList\u003cBook\u003e\u003e listener) {\n    \t // 按条件查询图书\n        selectFrom(BookDao.class)\n              .where(selection, args).listener(listener).execute();\n    }\n```\n\n## 四、数据库升级的sql语句\n\n升级数据库的sql语句我们也是存放在 assets 目录中, 该目录我们也可以在构建Builder时自行设置，默认为 assets/db/migrations 文件夹。升级的sql文件命名模式为: 版本号.sql , 例如 3.sql 代表数据库从小于3的版本号升级到3时最后一次需要执行的升级语句。加入用户在数据库版本号为1时安装了应用，现在用户更新应用，此时该应用的数据库版本已经为3，并且版本2时也升级了数据库。那么 migrations中应该有两个文件，分别为 2.sql 和 3.sql， 此时该用户升级就会执行 2.sql 和 3.sql 中的语句.\n\n例如, 2.sql 为 : \n\n```\nALTER TABLE users ADD COLUMN age INTEGER;\n```\n\n3.sql 为 : \n\n```\nALTER TABLE users ADD COLUMN gender INTEGER;\n```\n\n从版本号为1升级到3时，那么就会执行上述的sql语句。 数据库升级时需要把 版本号 修改为3 , 例如 : \n\n```\n/**\n* 初始化数据库\n*/\nprivate void initDatabase() {\n    new Builder(getApplicationContext())\n        .setDbName(\"demo.db\")              // 数据库名为 demo.db\n        .setDbVersion(3)                   // 数据库版本号为3\n        .setCreateSqlFile(\"db/create.sql\") // 创建数据库表的sql文件在 asserts/db/create.sql文件中\n        .setUpgradePath(\"db/migrations\")  // 数据库升级文件在 asserts/db/migrations 目录中\n        .create();\n }\n```\n\n\u003e ***注意*** : 如果升级了任意表结构，一定要到升级版本号,并且在 assets/db/migrations中添加对应的sql文件. 文件格式为:  版本号.sql 。\n\u003e \n\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2016 Mr.Simple\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fsimpledb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhehonghui%2Fsimpledb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhehonghui%2Fsimpledb/lists"}