|
1 | | -# sqlite-java |
| 1 | +[](https://jitpack.io/#artbits/sqlite-java) |
| 2 | +[](https://jdk.java.net/) |
| 3 | +[](#license) |
| 4 | + |
| 5 | + |
| 6 | +## SQLite-Java |
| 7 | +``SQLite-Java`` is a Java ORM for SQLite databases. Using ``SQLite-JDBC`` as the driver at the bottom. It provides simple and efficient APIs without writing a large number of SQL statements. You only need to know the basics of SQL to get started. |
| 8 | + |
| 9 | + |
| 10 | +## Features |
| 11 | + + Support for automatic table creation and addition columns. |
| 12 | + + Provide APIs for adding, deleting, modifying, and querying. |
| 13 | + + APIs are simple, elegant and efficient to use. |
| 14 | + |
| 15 | + |
| 16 | +## Download |
| 17 | +Gradle: |
| 18 | +```groovy |
| 19 | +repositories { |
| 20 | + maven { url 'https://www.jitpack.io' } |
| 21 | +} |
| 22 | +
|
| 23 | +dependencies { |
| 24 | + implementation 'com.github.artbits:sqlite-java:1.0.0' |
| 25 | +} |
| 26 | +``` |
| 27 | +Maven: |
| 28 | +```xml |
| 29 | +<repository> |
| 30 | + <id>jitpack.io</id> |
| 31 | + <url>https://www.jitpack.io</url> |
| 32 | +</repository> |
| 33 | + |
| 34 | +<dependency> |
| 35 | + <groupId>com.github.artbits</groupId> |
| 36 | + <artifactId>sqlite-java</artifactId> |
| 37 | + <version>1.0.0</version> |
| 38 | +</dependency> |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Usage |
| 43 | +Let Java classes be mapped into database tables. |
| 44 | +```java |
| 45 | +public class User { |
| 46 | + public Long id; |
| 47 | + public String name; |
| 48 | + public Integer age; |
| 49 | + public Boolean vip; |
| 50 | + |
| 51 | + public User(Consumer<User> consumer) { |
| 52 | + consumer.accept(this); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +public class Book { |
| 57 | + public Long id; |
| 58 | + public String name; |
| 59 | + public String author; |
| 60 | + public Double price; |
| 61 | + |
| 62 | + public Book(Consumer<Book> consumer) { |
| 63 | + consumer.accept(this); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Connect to the database and load tables (automatically add tables and columns). |
| 69 | +```java |
| 70 | +DB db = DB.connect("database/example.db"); |
| 71 | +db.tables(User.class, Book.class); |
| 72 | +``` |
| 73 | + |
| 74 | +Insert data. |
| 75 | +```java |
| 76 | +// No need to set ID, ID will increase automatically when inserting data. |
| 77 | +User user = new User(u -> {u.name = "Lake"; u.age = 25; u.vip = true;}); |
| 78 | +db.insert(user); |
| 79 | +System.out.printf("userId = %d", user.id); |
| 80 | +``` |
| 81 | + |
| 82 | +Update data. |
| 83 | +```java |
| 84 | +// Update data by id. |
| 85 | +db.update(new User(u -> { |
| 86 | + u.id = 11L; |
| 87 | + u.vip = false; |
| 88 | +})); |
| 89 | + |
| 90 | +// Update data by condition. |
| 91 | +db.update(new User(u -> u.vip = true), "age < ?", 50); |
| 92 | +``` |
| 93 | + |
| 94 | +Delete data. |
| 95 | +```java |
| 96 | +// Delete all data in this table. |
| 97 | +db.deleteAll(User.class); |
| 98 | + |
| 99 | +// Delete data by IDs. |
| 100 | +db.delete(User.class, 1L, 2L, 3L); |
| 101 | + |
| 102 | +// Delete data by ID list. |
| 103 | +db.delete(User.class, Arrays.asList(1L, 2L, 3L)); |
| 104 | + |
| 105 | +// Delete data by condition. |
| 106 | +db.delete(User.class, "name = ?", "Lake"); |
| 107 | +``` |
| 108 | + |
| 109 | +Query data. |
| 110 | +```java |
| 111 | +// Find one by ID. |
| 112 | +User user1 = db.findOne(User.class, 1L); |
| 113 | + |
| 114 | +// Find one by condition. |
| 115 | +User user2 = db.findOne(User.class, "name = ?", "Lake"); |
| 116 | + |
| 117 | +// Find all. |
| 118 | +List<User> users1 = db.findAll(User.class); |
| 119 | + |
| 120 | +// Find many by IDs. |
| 121 | +List<User> users2 = db.find(User.class, 1L, 2L, 3L); |
| 122 | + |
| 123 | +// Find many by ID list. |
| 124 | +List<User> users3 = db.find(User.class, Arrays.asList(1L, 2L, 3L)); |
| 125 | + |
| 126 | +// Find many by custom option rules. Options APIs are optional, choose according to actual needs. |
| 127 | +List<User> users4 = db.find(User.class, options -> options |
| 128 | + .select("name", "age") |
| 129 | + .where("age <= ?", 50) |
| 130 | + .order("age", Options.DESC) |
| 131 | + .limit(5) |
| 132 | + .offset(1)); |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## Links |
| 138 | ++ Thanks: |
| 139 | + + [SQLite-JDBC](https://github.com/xerial/sqlite-jdbc) |
| 140 | + + [QuickIO](https://github.com/artbits/quickio) |
| 141 | + |
| 142 | + |
| 143 | +## License |
| 144 | +``` |
| 145 | +Copyright 2023 Zhang Guanhu |
| 146 | +
|
| 147 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 148 | +you may not use this file except in compliance with the License. |
| 149 | +You may obtain a copy of the License at |
| 150 | +
|
| 151 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 152 | +
|
| 153 | +Unless required by applicable law or agreed to in writing, software |
| 154 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 155 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 156 | +See the License for the specific language governing permissions and |
| 157 | +limitations under the License. |
0 commit comments