2023-01-03
1867
#rust
MacBobby Chibuzor
152731
107
Jan 3, 2023 â‹… 6 min read

Create an API in Rust with SQLite and Rocket

MacBobby Chibuzor Go, Solidity, and Haskell developer interested in the cloud native world and blockchain technology. A fanatic for technical writing and open source contribution.

Recent posts:

Vue.js logo over a dark, textured purple background. The article discusses using defineExpose and in Vue 3 to enhance component interaction and enable dynamic theming.

defineExpose and <style vars> in Vue 3 for component interaction and theming

Simplify component interaction and dynamic theming in Vue 3 with defineExpose and for better control and flexibility.

Clara Ekekenta
Nov 7, 2024 â‹… 8 min read
How to set up TypeScript with Node.js and Express

How to set up TypeScript with Node.js and Express

Explore how to integrate TypeScript into a Node.js and Express application, leveraging ts-node, nodemon, and TypeScript path aliases.

Aman Mittal
Nov 7, 2024 â‹… 10 min read
Cover image for es-toolkit, a Lodash alternative

es-toolkit, a Lodash alternative

es-toolkit is a lightweight, efficient JavaScript utility library, ideal as a modern Lodash alternative for smaller bundles.

Rishi Purwar
Nov 6, 2024 â‹… 5 min read
JS Logo Over Blue Background

The ResizeObserver API: A tutorial with examples

The use cases for the ResizeObserver API may not be immediately obvious, so let’s take a look at a few practical examples.

Kevin Drum
Nov 5, 2024 â‹… 9 min read
View all posts

9 Replies to "Create an API in Rust with SQLite and Rocket"

  1. Whoa. Can’t believe I’ve been doing this the wrong way. Thank you for this guide – it’s timely indeed.

  2. Please stop recommending Rocket. It should be considered depreciated. It is no longer actively maintained. I would recommend using something like Axum since it is very well maintained.

    1. Hi Elliot,

      We already have an article about using Actix-web framework here: https://blog.logrocket.com/building-rest-api-rust-rhai-actix-web/. However, based on high requests from developers, this one had to be addressed.

      Refer to this report on Stackshare to confirm how Rocket is more used before now: https://stackshare.io/stackups/actix-vs-rocket#:~:text=Rocket%20and%20Actix%20can%20be,stars%20and%20216%20GitHub%20forks.

      But your point and recommendations are very valid, we only hope you understand ours too.

  3. Hey! I am a beginner with the rust programming language and I find this tutorial great. I followed all the steps, but after I run the “cargo run” command, I get the following error:

    error with configuration: unrecognized database url: “test_data.db”
    –> src/database.rs:20:14
    |
    20 | let id = sqlx::query_as!(
    | ______________^
    21 | | Profile,
    22 | | r#”
    23 | | INSERT INTO profiles (name, humidity_min, humidity_max, light) VALUES (?, ?, ?, ?);
    … |
    28 | | light
    29 | | )
    | |_____^
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

    1. sorry my personal changed were applied, it is rather this error:

      error: error with configuration: unrecognized database url: “data.db”
      –> src/database.rs:18:14
      |
      18 | let id = sqlx::query_as!(
      | ______________^
      19 | | Task,
      20 | | r#”
      21 | | INSERT INTO tasks (name, description) VALUES (?, ?);
      … |
      24 | | description
      25 | | )
      | |_____^
      |
      = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

      1. change your .env file to
        “`
        DATABASE_URL=sqlite://data.db
        “`
        and the start of your main function to
        “`
        let mut config = sqlx::sqlite::SqliteConnectOptions::new();
        config = config.filename(“data.db”);

        let pool = SqlitePool::connect_with(config)
        .await
        .expect(“Couldn’t connect to sqlite database”);
        “`
        and ofc you can modify the config as you want

        1. Hi — Thank you for that ‘obvious’ tip, that I didn’t see. I used the `env` file to set my DB to use a relative path as: “sqlite://../data/data.db”. It just works. Gotta love Rust (sometimes).

Leave a Reply