{"id":11969,"date":"2025-10-28T19:12:23","date_gmt":"2025-10-28T19:12:23","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11969"},"modified":"2025-10-28T19:12:23","modified_gmt":"2025-10-28T19:12:23","slug":"rust-and-sqlite","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/rust-and-sqlite\/","title":{"rendered":"Rust and Sqlite"},"content":{"rendered":"<p>Add the dependencies <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;dependencies]\r\nrusqlite = { version = &quot;0.37.0&quot;, features = &#x5B;&quot;bundled&quot;] }\r\n<\/pre>\n<p>The <em>bundled<\/em> part will automatically compile and link an upto date SQLite, without this I got errors such as &#8220;LINK : fatal error LNK1181: cannot open input file &#8216;sqlite3.lib'&#8221;, obviously if you have everything installed for SQLite, then you might prefer the non-bundled dependency, so just replace this with.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;dependencies]\r\nrusqlite = &quot;0.37.0&quot;\r\n<\/pre>\n<p><strong>Create a DB<\/strong><\/p>\n<p>Now let&#8217;s create a database as a file and insert an initial row of data<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse rusqlite::Connection;\r\n\r\nfn main() {\r\n    let connection = Connection::open(&quot;.\/data.db3&quot;).unwrap();\r\n    connection.execute(&quot;CREATE TABLE app (id INTEGER PRIMARY KEY, name TEXT NOT NULL&quot;, ()).unwrap();\r\n    connection.execute(&quot;INSERT INTO app (id, name) VALUES (?, ?)&quot;, (1, &quot;Hello&quot;)).unwrap();\r\n}\r\n<\/pre>\n<p>We could also do this in memory using the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet connection = Connection::open_in_memory().unwrap();\r\n<\/pre>\n<p><strong>Reading from our DB<\/strong><\/p>\n<p>We&#8217;ll create a simple structure representing the DB created above<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#&#x5B;derive(Debug)]\r\nstruct App {\r\n    id: i32,\r\n    name: String,\r\n}\r\n<\/pre>\n<p>Now to read into this we use the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet mut statement = connection.prepare(&quot;SELECT * FROM app&quot;).unwrap();\r\nlet app_iter = statement.query_map(&#x5B;], |row| {\r\n  Ok(App {\r\n    id: row.get(0)?,\r\n    name: row.get(1)?,\r\n  })\r\n}).unwrap();\r\n\r\nfor app in app_iter {\r\n  println!(&quot;{:?}&quot;, app.unwrap());\r\n}\r\n<\/pre>\n<p>You&#8217;ll also need the following <em>use<\/em> clause<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse rusqlite::fallible_iterator::FallibleIterator;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Add the dependencies &#x5B;dependencies] rusqlite = { version = &quot;0.37.0&quot;, features = &#x5B;&quot;bundled&quot;] } The bundled part will automatically compile and link an upto date SQLite, without this I got errors such as &#8220;LINK : fatal error LNK1181: cannot open input file &#8216;sqlite3.lib&#8217;&#8221;, obviously if you have everything installed for SQLite, then you might prefer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191,764],"tags":[],"class_list":["post-11969","post","type-post","status-publish","format-standard","hentry","category-rust","category-sqlite"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11969"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11969\/revisions"}],"predecessor-version":[{"id":11972,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11969\/revisions\/11972"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}