{"id":80,"date":"2016-02-12T12:57:04","date_gmt":"2016-02-12T12:57:04","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=80"},"modified":"2023-11-24T15:29:56","modified_gmt":"2023-11-24T15:29:56","slug":"creating-tables","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/databases\/sqlite\/tables\/creating-tables","title":{"rendered":"Creating Tables"},"content":{"rendered":"<h4>An Example<\/h4>\n<pre><code>\n\t\t\/\/*****************************************\n\t\t\/\/*****************************************\n\t\t\/\/********** INITIALISE DATABASE **********\n\t\t\/\/*****************************************\n\t\t\/\/*****************************************\n\t\tbool InitialiseDatabase()\n\t\t{\n\t\t\tSystem.Data.SQLite.SQLiteConnection Connection1 = null;\n\t\t\tString sTemp;\n\n\t\t\ttry\n\t\t\t{\n\n\t\t\t\t\/\/----- OPEN THE DATABASE CONNECTION -----\n\t\t\t\tConnection1 = new System.Data.SQLite.SQLiteConnection(\"data source=\" + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + \"\\\\\" + Application.CompanyName + \"\\\\\" + Application.ProductName + \"\\\\mydatabasefile.db;Password=\" + SQLITE_DATABASE_FILE_PASSWORD);\n\t\t\t\tConnection1.Open();\n\n\t\t\t\tSystem.Data.SQLite.SQLiteCommand Command1 = new System.Data.SQLite.SQLiteCommand(Connection1);\n\n\t\t\t\t\/\/----- CREATE MY TABLE IF NECESSARY -----\n\t\t\t\tsTemp = @\"CREATE TABLE IF NOT EXISTS tblMyTableName ( \n\t\t\t\t\t\t\t\t\t\tId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \n\t\t\t\t\t\t\t\t\tMyIntigerField INTEGER, \n                                    MyNumericField NUMERIC, \n                                    MyRealField REAL, \n                                    MyTextField TEXT \n\t\t\t\t\t\t\t\t\t)\";\n\t\t\t\tCommand1.CommandText = sTemp;\n\t\t\t\tCommand1.ExecuteNonQuery();\n\n\t\t\t\treturn(true);\n\t\t\t}\n\t\t\tcatch (Exception err)\n\t\t\t{\n\t\t\t\tMessageBox.Show(\"Error:\\n\" + err, \"Error\", MessageBoxButtons.OK, MessageBoxIcon.Error);\n\t\t\t\treturn(false);\n\t\t\t}\n\t\t\tfinally\n\t\t\t{\n\t\t\t\t\/\/----- CLOSE THE DATABASE CONNECTION -----\n\t\t\t\ttry\n\t\t\t\t{\n\t\t\t\t\tif (Connection1 != null)\n\t\t\t\t\t\tConnection1.Close();\n\t\t\t\t}\n\t\t\t\tcatch (Exception)\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n<\/code><\/pre>\n<h4>Unique Columns<\/h4>\n<pre><code>\nCREATE TABLE bookmarks(\n    users_id INTEGER,\n    lessoninfo_id INTEGER,\n    UNIQUE(users_id, lessoninfo_id)\n);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An Example \/\/***************************************** \/\/***************************************** \/\/********** INITIALISE DATABASE ********** \/\/***************************************** \/\/***************************************** bool InitialiseDatabase() { System.Data.SQLite.SQLiteConnection Connection1 = null; String sTemp; try { \/\/&#8212;&#8211; OPEN THE DATABASE CONNECTION &#8212;&#8211; Connection1 = new System.Data.SQLite.SQLiteConnection(&#8220;data source=&#8221; + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &#8220;\\\\&#8221; + Application.CompanyName + &#8220;\\\\&#8221; + Application.ProductName + &#8220;\\\\mydatabasefile.db;Password=&#8221; + SQLITE_DATABASE_FILE_PASSWORD); Connection1.Open(); System.Data.SQLite.SQLiteCommand Command1 = new System.Data.SQLite.SQLiteCommand(Connection1); \/\/&#8212;&#8211; CREATE MY [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-tables"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":1320,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/80\/revisions\/1320"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}