{"id":1259,"date":"2015-01-06T20:15:42","date_gmt":"2015-01-06T20:15:42","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1259"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"user_version-pragma","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/databases\/sqlite\/pragma\/user_version-pragma","title":{"rendered":"user_version Pragma"},"content":{"rendered":"<p>\nThe user_version pragma gets or sets the user-defined version value that is stored in the database header. Following is the simple syntax:\n<\/p>\n<p>\nPRAGMA [database.]user_version;<br \/>\nPRAGMA [database.]user_version = number;\n<\/p>\n<p>\nThis is a 32-bit signed integer value which can be set by the developer for version tracking purpose. &nbsp;Default value is 0.\n<\/p>\n<h4>\nUsing user_version for database updating example<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tbool DatabaseSqlite::CheckForUpdateDatabase(void)\r\n\t{\r\n\t\tSystem::Data::SQLite::SQLiteConnection ^Connection1;\r\n\t\tString ^sTemp;\r\n\t\tint user_version;\r\n\t\tbool do_loop;\r\n\r\n\t\ttry\r\n\t\t{\r\n\r\n\t\t\t\/\/----- OPEN THE DATABASE CONNECTION -----\r\n\t\t\tConnection1 = gcnew System::Data::SQLite::SQLiteConnection(&quot;data source=&quot; + Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + &quot;\\\\&quot; + Application::CompanyName + &quot;\\\\&quot; + Application::ProductName + &quot;\\\\mydatabasefile.db&quot;);\r\n\t\t\tConnection1-&gt;Open();                             \/\/ Open the connection to the database\r\n\r\n\t\t\tSystem::Data::SQLite::SQLiteCommand ^Command1 = gcnew System::Data::SQLite::SQLiteCommand(Connection1);\r\n\r\n\t\t\t\/\/-------------------------------------\r\n\t\t\t\/\/-------------------------------------\r\n\t\t\t\/\/----- CHECK FOR UPDATE DATABASE -----\r\n\t\t\t\/\/-------------------------------------\r\n\t\t\t\/\/-------------------------------------\r\n\r\n\t\t\t#define OUR_CURRENT_SQLITE_DATABASE_VERSION\t\t1\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;INCREMENT WHEN NEW UPDATE STEP ADDED TO SWITCH BELOW\r\n\r\n\t\t\tdo_loop = true;\r\n\t\t\twhile (do_loop)\r\n\t\t\t{\r\n\t\t\t\t\/\/----- GET OUR CURRENT DATABASE VERSION -----\r\n\t\t\t\t\/\/(We use the PRAGMA user_version field from the database header to store our version number)\r\n\t\t\t\tuser_version = 99999999;\r\n\t\t\t\tCommand1-&gt;CommandText = &quot;PRAGMA user_version;&quot;;      \/\/ Select all rows from our database table\r\n\t\t\t\tSystem::Data::SQLite::SQLiteDataReader ^Reader1 = Command1-&gt;ExecuteReader();\r\n\t\t\t\t{\r\n\t\t\t\t\tif (Reader1-&gt;Read())\r\n\t\t\t\t\t\tuser_version = Convert::ToInt32(Reader1[\"user_version\"]);\r\n\t\t\t\t}\r\n\t\t\t\tReader1-&gt;Close();\r\n\r\n\t\t\t\t\/\/Exit if no updates needed\r\n\t\t\t\tif (user_version &gt;= OUR_CURRENT_SQLITE_DATABASE_VERSION)\r\n\t\t\t\t\tbreak;\r\n\r\n\t\t\t\t\/\/--------------------------\r\n\t\t\t\t\/\/----- DO NEXT UPDATE -----\r\n\t\t\t\t\/\/--------------------------\r\n\t\t\t\tswitch (user_version)\r\n\t\t\t\t{\r\n\t\t\t\t\tcase 0:\r\n\t\t\t\t\t\/\/----- CURRENTLY VERSION 0 - ADD NEW #### FIELD TO #### ------\r\n\t\t\t\t\tsTemp = &quot;ALTER TABLE tblLogEvents ADD COLUMN my_new_field_name TEXT;&quot;;\r\n\t\t\t\t\tCommand1-&gt;CommandText = sTemp;\r\n\t\t\t\t\tCommand1-&gt;ExecuteNonQuery();\r\n\t\t\t\t\tbreak;\r\n\r\n\t\t\t\tdefault:\r\n\t\t\t\t\t\/\/----- ERROR -----\r\n\t\t\t\t\tdo_loop = false;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\r\n\r\n\t\t\t\t\/\/----- UPDATE OUR DATABASE VERSION NUMBER -----\r\n\t\t\t\tif (do_loop)\r\n\t\t\t\t{\r\n\t\t\t\t\tuser_version++;\r\n\t\t\t\t\tCommand1-&gt;CommandText = &quot;PRAGMA user_version=&quot; + user_version + &quot;;&quot;;\r\n\t\t\t\t\tCommand1-&gt;ExecuteNonQuery();\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n\r\n\t\t\treturn(true);\r\n\t\t}\r\n\t\tcatch (Exception ^e)\r\n\t\t{\r\n\t\t\tMessageBox::Show(L&quot;Error:\\n&quot; + e, L&quot;Error&quot;, MessageBoxButtons::OK, MessageBoxIcon::Error);\r\n\t\t\treturn(false);\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\t\/\/----- CLOSE THE DATABASE CONNECTION -----\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tif (Connection1 != nullptr)\r\n\t\t\t\t\tConnection1-&gt;Close();\r\n\t\t\t}\r\n\t\t\tcatch (Exception ^)\r\n\t\t\t{\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The user_version pragma gets or sets the user-defined version value that is stored in the database header. Following is the simple syntax: PRAGMA [database.]user_version; PRAGMA [database.]user_version = number; This is a 32-bit signed integer value which can be set by the developer for version tracking purpose. &nbsp;Default value is 0. Using user_version for database updating [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-1259","post","type-post","status-publish","format-standard","hentry","category-pragma"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=1259"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1259\/revisions"}],"predecessor-version":[{"id":1271,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1259\/revisions\/1271"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}