home ~ projects ~ socials

Create A SQLite Database In Rust

---
[dependencies]
rusqlite = { version = "0.35.0" }
---

use rusqlite::Connection;
use rusqlite::Result;

fn main() -> Result<()> {
  let db_path = "/Users/alan/Desktop/db_test.sqlite";
  let conn = Connection::open(db_path)?;

  let statements = vec![
    "CREATE TABLE IF NOT EXISTS
      some_data (
        id INTEGER PRIMARY KEY,
        item TEXT NOT NULL UNIQUE
      )",
    "CREATE TABLE IF NOT EXISTS
      more_data (
        id INTEGER PRIMARY KEY,
        item TEXT NOT NULL UNIQUE
      )"
  ];

  for sql in statements.iter() {
    conn.execute(sql,())?;
  } 

  println!("done");

  Ok(())
}
Output:
done
-- end of line --