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?
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.
Create some complete working programs in Rust that do not use any database. Become confident that you can actually program in Rust.
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.
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 postgres - Rust 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.
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
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.