+
+
+
+
+
+
Sqlite
+
Setup connection
+
First thing you need to do is to setup a connection is:
+
let conn = sqlite::open("my_database.db");
+
+
We can also use a in-memory database:
+
let conn = sqlite::open_in_memory("my_database.db");
+
+
Define tables
+
To define a table we use derive with the Table
(provided by njord) and Default
procedural macro. Then we create a simple struct with a given name. The name of the struct will be the table name in the database:
+
#[derive(Table, Default)]
+struct Posts {
+ title: String,
+ description: String,
+}
+
+#[derive(Table, Default)]
+struct Categories {
+ name: String,
+}
+
+
Initialize database with tables
+
First thing we need to do before we do any insert
, select
etc is to initialize the database with the defined tables. To initialize the database we need to box the struct and call default()
and save it to a variable:
+
let posts_table = Box::<Posts>::default();
+let categories_table = Box::<Categories>::default();
+
+
Now we add them to a tables vector:
+
let mut tables: Vec<Box<dyn Table>> = vec![posts_table, categories_table];
+
+
Lastly, we call the init()
function to setup the tables from the tables vector, such as:
+
sqlite::init(conn, tables);
+
+
Insert data to table
+
To insert data, we need to initialize our Posts struct that we created with values for each field:
+
let table_row: Posts = Posts {
+ title: "A post title".to_string(),
+ description: "Some description for for a post".to_string(),
+};
+
+
We insert the row by passing a connection and a reference to the table_row:
+
sqlite::insert(conn, &table_row);
+
+
Drop table
+
To drop a table, it is a simple as:
+
sqlite::drop_table(conn, &Posts::default());
+
+
Select
+
Docs are coming soon...
+
WHERE
+
ORDER BY
+
GROUP BY
+
LIMIT
+
OFFSET
+
+
+ -
+ Last updated:
+
+
+
+ -
+ Contributors
+
+
+
+