Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1.08 KB

initialization.zh.md

File metadata and controls

39 lines (30 loc) · 1.08 KB

创建 sqlite 数据库

[![rusqlite-badge]][rusqlite] [![cat-database-badge]][cat-database]

使用rusqlite箱子,打开 sqlite 数据库。见箱子文档中 Windows 的编译。

Connection::open:如果数据库不存在,将创建该数据库。

extern crate rusqlite;

use rusqlite::{Connection, Result};
use rusqlite::NO_PARAMS;

fn main() -> Result<()> {
    let conn = Connection::open("cats.db")?;

    conn.execute(
        "create table if not exists cat_colors (
             id integer primary key,
             name text not null unique
         )",
        NO_PARAMS,
    )?;
    conn.execute(
        "create table if not exists cats (
             id integer primary key,
             name text not null,
             color_id integer not null references cat_colors(id)
         )",
        NO_PARAMS,
    )?;

    Ok(())
}