Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Rust connects to SQLite database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to connect Rust to SQLite database, the article is very detailed, has a certain reference value, interested friends must read it!

Using the Rust language connection to operate the SQLite database, I use the crate of rusqlite.

Look at the example:

First, create a Rust project using cargo, and then add the dependency rusqlite:

When you come to main.rs, all the rest of the code is written here.

First introduce rusqlite-related types and create a Person struct:

Person has three fields, id, name, and data, but in this case, we will only use the first two fields.

Next, write a function to create the database and the person table:

This function creates a database file named data.db (if it doesn't exist), then opens a database lian jie and deletes the person table (if it exists), then creates a person table, and returns Connection.

Next, we create an insert_data function that inserts two pieces of data, using a reference to Connection returned by the create_db function as an argument:

Then create a function get_data that can query data from the database, and it will return a Vec of Person:

Finally, we call these functions in turn in the main function and print the data read from the database:

Running result:

These are all the contents of the article "how Rust connects to SQLite databases". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report