{"id":1857,"date":"2022-08-30T15:20:01","date_gmt":"2022-08-30T15:20:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/cannot-open-sqlite-database-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:01","modified_gmt":"2022-08-30T15:20:01","slug":"cannot-open-sqlite-database-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cannot-open-sqlite-database-collection-of-common-programming-errors\/","title":{"rendered":"Cannot open SQLite database-Collection of common programming errors"},"content":{"rendered":"<p>Using this code:<\/p>\n<pre><code>public void InsertPlatypiRequestedRecord(string PlatypusId, string PlatypusName, DateTime invitationSentLocal)\n{\n    var db = new SQLiteConnection(SQLitePath);\n    {\n        db.CreateTable();\n        db.RunInTransaction(() =&gt;\n                                {\n                                    db.Insert(new PlatypiRequested\n                                                  {\n                                                      PlatypusId = PlatypusId,\n                                                      PlatypusName = PlatypusName,\n                                                      InvitationSentLocal = invitationSentLocal\n                                                  });\n                                    db.Dispose();\n                                });\n    }\n}\n<\/code><\/pre>\n<p>&#8230;I get, &#8220;<em>SQLite.SQLiteException was unhandled by user code HResult=-2146233088 Message=Cannot create commands from unopened database<\/em>&#8220;<\/p>\n<p>&#8230;but attempting to add a &#8220;db.Open()&#8221; doesn&#8217;t work, because there is apparently no such method.<\/p>\n<ol>\n<li>\n<p>You are disposing the database prematurely (inside of the transaction). It is better to wrap things up inside of a &#8220;using&#8221; statement, which will dispose of the db connection:<\/p>\n<pre><code>private void InsertPlatypiRequestedRecord(string platypusId, string platypusName, DateTime invitationSentLocal)\n{\n    using (var db = new SQLiteConnection(SQLitePath))\n    {\n        db.CreateTable();\n        db.RunInTransaction(() =&gt;\n        {\n            db.Insert(new PlatypiRequested\n            {\n                PlatypusId = platypusId,\n                PlatypusName = platypusName,\n                InvitationSentLocal = invitationSentLocal\n            });\n        });\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>string connecString = @\"Data Source=D:\\SQLite.db;Pooling=true;FailIfMissing=false\";       \n\/*D:\\sqlite.db??sqlite????????,???????????*\/\nSQLiteConnection conn = new SQLiteConnection(connectString); \/\/??????\nconn.Open();  \/\/????\nSQLiteCommand cmd = conn.CreateCommand();\n\ncmd.CommandText = \"select * from orders\";   \/\/?????????orders?\n\ncmd.CommandType = CommandType.Text;\nusing (SQLiteDataReader reader = cmd.ExecuteReader())\n{\n   while (reader.Read())\n                Console.WriteLine( reader[0].ToString());\n}\n<\/code><\/pre>\n<p>you can download System.Data.SQLite.dll here<\/p>\n<p>here is a chinese article for csharp connect sqlite<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:01:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Using this code: public void InsertPlatypiRequestedRecord(string PlatypusId, string PlatypusName, DateTime invitationSentLocal) { var db = new SQLiteConnection(SQLitePath); { db.CreateTable(); db.RunInTransaction(() =&gt; { db.Insert(new PlatypiRequested { PlatypusId = PlatypusId, PlatypusName = PlatypusName, InvitationSentLocal = invitationSentLocal }); db.Dispose(); }); } } &#8230;I get, &#8220;SQLite.SQLiteException was unhandled by user code HResult=-2146233088 Message=Cannot create commands from unopened database&#8220; &#8230;but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1857","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1857"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}