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:

Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
JavaScript logo on top of violet background

Exploring essential DOM methods for frontend development

Learn four groups of DOM methods and their uses to create responsive and dynamic webpages. A helpful DOM reference table is also included.

Chimezie Innocent
Jul 23, 2024 â‹… 12 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