Can I insert variable result into database?

First of all hello everyone!

I am a newbie in Rust. Just finished setting up Diesel Actix-web and PostgreSQL. I have a question.

Is it possible to insert into online database some variables? For example server time, or some random output? If yes which module or lib should I start looking for? Does Rocket also support feature like that?

Thank you in advance.

Of course. Databases are for storing data. Data generally comes from variables.

I use Rocket with the rust-postgres driver:
https://rocket.rs/v0.4/guide/state/#databases
Rust-Postgres

Thank you. So this example should work?

  let info = os_info::get();

  let rows_inserted = diesel::insert_into(users)
    .values(&id.eq(&info?))
    .execute(&connection);

assert_eq!(Ok(1), rows_inserted);

When I have .

values(&id.eq(variable-info)) - how to put variable in proper way

? In all examples there is always some strings. Sorry I am completely new.

In that case I suspect you will run into a lot of frustration by diving straight into using databases from Rust and likely give up.

I suggest:

  1. Get a bit "old" at Rust. As it were. Get familiar with it. Work through the Rust book: https://doc.rust-lang.org/book/

  2. Create some complete working programs in Rust that do not use any database. Become confident that you can actually program in Rust.

  3. Then think about writing Rust that uses a database. I started out with that by trying out the examples in the rust-postgres documentation, see link above.

Sorry I can advise about diesel but I'm sure the same approach would work well there.

1 Like

Thank you again ZigCog. I will start from book like you suggest. I like to learn from useful examples but I think Rust is just too complicated to start like that. BTW. can you just tell me how to put variable in the previous example?

As I said. I cannot comment on using Diesel as I have never used it.

I use rust-postgres with Rocket. If you check the rust-prostgres documentation https://docs.rs/postgres/0.15.2/postgres/ you will find an example of inserting fields into a row from a structure. Like so:

struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>
}

fn main() {
    let conn = Connection::connect("postgresql://postgres@localhost:5433", TlsMode::None)
            .unwrap();

    conn.execute("CREATE TABLE person (
                    id              SERIAL PRIMARY KEY,
                    name            VARCHAR NOT NULL,
                    data            BYTEA
                  )", &[]).unwrap();
    let me = Person {
        id: 0,
        name: "Steven".to_owned(),
        data: None
    };
    conn.execute("INSERT INTO person (name, data) VALUES ($1, $2)",
                 &[&me.name, &me.data]).unwrap();
    ...
    ...

All of which should be clear enough to follow if you have ever used SQL with any other language.

1 Like

I will go back to basics. Rust is fascinating but thing I do not like at all is that it is so hard to setup. On windows I was trying for 1 week and failed. But on linux it is much easier and possible for such a noob like me. Have a nice day and thank you again.

I too gave up trying to use Rust for Windows. Admittedly I don't actually need to develop anything for Windows so I did not try very hard.

However what I found works very well is to install the Windows Subsystem For Windows, install Debian into that and then use Rust for Linux from there. Quick and easy to setup and works beautifully.

In fact for the last year of so I have been doing all my Linux development on Win 10 machines with WSL.

I use Windows Visual Studio Code to edit the Rust, and whatever, in the Linux file system in the WSL. So convenient.

It's so nice that after all these decades Microsoft finally made their OS useful for me :slight_smile:

1 Like

Thank you. So this example should work?

let info = os_info::get();

let rows_inserted = diesel::insert_into(users)
  .values(&id.eq(&info?))
  .execute(&connection);

assert_eq!(Ok(1), rows_inserted);

When I have .

values(&id.eq(variable-info)) - how to put variable in proper way

? In all examples there is always some strings. Sorry I am completely new.

It is not possible to answer that question in detail without having more details about your schema and the involved types here.
In general it is possible to insert a value from some variable into a database using diesel this way. Please checkout the guides on the diesel webpage, especially that one about inserts.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.