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:

Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 min read
Using Aws Lambda And Aws Cloudfront To Optimize Image Handling

Using AWS Lambda and CloudFront to optimize image handling

Leverage services like AWS Lambda, CloudFront, and S3 to handle images more effectively, optimizing performance and providing a better UX.

Nitish Sharma
Apr 12, 2024 â‹… 12 min read
Building Web-Based Terminal Components With Termino.js

Building web-based terminal components with Termino.js

Explore Termino.js, an open source library for integrating web-based terminals into applications, in this introduction article.

Chibuike Nwachukwu
Apr 11, 2024 â‹… 6 min read
How To Build A Custom Gpt: Step By Step Tutorial

How to build a custom GPT: Step-by-step tutorial

Let’s see why and how to build custom GPTs — personalized versions of ChatGPT that act as custom chatbots to serve a specific purpose.

Peter Aideloje
Apr 10, 2024 â‹… 7 min read
View all posts

8 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