{"id":140,"date":"2009-09-02T12:17:26","date_gmt":"2009-09-02T17:17:26","guid":{"rendered":"http:\/\/www.juggleware.com\/blog\/?p=140"},"modified":"2009-09-02T12:24:57","modified_gmt":"2009-09-02T17:24:57","slug":"mysql-to-sqlite-cheatsheet-for-iphone-developers","status":"publish","type":"post","link":"https:\/\/www.juggleware.com\/blog\/2009\/09\/mysql-to-sqlite-cheatsheet-for-iphone-developers\/","title":{"rendered":"mySQL to SQLite cheatsheet for iPhone developers"},"content":{"rendered":"<p>Although I started writing this as a cheat-sheet for myself (after many hours of struggling with this by trial-and-error), I figured it would be helpful for other iPhone developers who&#8217;d like to take a mySQL database online and migrate it to their current iPhone project. While other tutorials cover how to read the database into the application from the coding point-of-view, this is just to make sure you get your data uncompromised from your existing mySQL database into a new SQLite DB that your iPhone app can read.<\/p>\n<p>First of all, here&#8217;s what I am currently using; I am including versions in case that&#8217;s relevant to your situation:<\/p>\n<ul>\n<li><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/dev.mysql.com\/');\"  href=\"http:\/\/dev.mysql.com\/\">MySQL<\/a> 5.0.81<\/li>\n<li>MySQL charset: UTF8-Unicode (utf8)<\/li>\n<li>MySQL connection collation: utf8_unicode_ci<\/li>\n<li><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.phpmyadmin.net\/home_page\/index.php');\"  href=\"http:\/\/www.phpmyadmin.net\/home_page\/index.php\">phpMyAdmin<\/a> &#8211; 2.11.9.5<\/li>\n<li><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/sqlitebrowser.sourceforge.net\/');\"  href=\"http:\/\/sqlitebrowser.sourceforge.net\/\">SQLite Database Browser<\/a> 1.3 (includes 3.3.5 of the SQLite database engine)<\/li>\n<li>iPhone Dev Kit &#8211; 3.0<\/li>\n<li>XCode 3.1.3<\/li>\n<li>SQLite 3 framework found at \/Developer\/Platforms\/iPhoneOS.platform\/Developer\/SDKs\/iPhoneOS3.0.sdk\/usr\/lib\/libsqlite3.dylib<\/li>\n<\/ul>\n<p>You can try other solutions for importing the data into a SQLite format, but I&#8217;ve had the best luck with Mauricio Piacentini&#8217;s SQLite Database Browser. <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.sqlite.org\/cvstrac\/wiki?p=ManagementTools');\"  href=\"http:\/\/www.sqlite.org\/cvstrac\/wiki?p=ManagementTools\">Other options<\/a> you might want to try include\u00a0a <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/addons.mozilla.org\/en-US\/firefox\/addon\/5817');\"  href=\"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/5817\">Firefox plugin<\/a>.<\/p>\n<p><strong>Here are the steps, using SQLite Database Browser:<\/strong><\/p>\n<ol>\n<li><a onclick=\"javascript:pageTracker._trackPageview('\/downloads\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export.png');\"  href=\"http:\/\/www.juggleware.com\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-143 alignright\" title=\"phpAdmin-SQLite_export\" src=\"http:\/\/www.juggleware.com\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export-300x250.png\" alt=\"phpMyAdmin options for exporting to SQLite\" width=\"300\" height=\"250\" srcset=\"https:\/\/www.juggleware.com\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export-300x250.png 300w, https:\/\/www.juggleware.com\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export-1024x855.png 1024w, https:\/\/www.juggleware.com\/blog\/wp-content\/uploads\/2009\/09\/phpAdmin-SQLite_export.png 1070w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>log into phpMyAdmin, select your database and go to <strong>Database&gt; Export<\/strong>.<\/li>\n<li>On the export tab, make sure all databases are selected, and the <strong>&#8220;SQL&#8221;<\/strong> radio button is selected.<\/li>\n<li>Select\u00a0<strong>ANSI<\/strong> from the pulldown for export compatibility.<\/li>\n<li>Uncheck all options under <strong>Structure<\/strong> and <strong>Data<\/strong>, but leave each section checked.<\/li>\n<li><strong>Save as file<\/strong> should be checked, and probably be via ZIP or GZIP, just in case.<\/li>\n<li>Click the <strong>Go<\/strong> button to download your file.<\/li>\n<li>Extract the file and open it up in your favorite text editor (I use <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/barebones.com\/products\/bbedit\/');\"  href=\"http:\/\/barebones.com\/products\/bbedit\/\">BBEdit<\/a>).<\/li>\n<li>Find the first <strong>CREATE TABLE<\/strong> line. We need to modify these by hand to simplify the table creation so that SQLite doesn&#8217;t get confused. For example, my file came down looking like this after the comment section ended:<br \/>\n<code><br \/>\nCREATE TABLE dictionary (<br \/>\nid int(11) NOT NULL auto_increment,<br \/>\nword varchar(100) NOT NULL,<br \/>\nadjective tinyint(1) NOT NULL default '0' COMMENT 'is an adjective',<br \/>\nnoun tinyint(1) NOT NULL default '0' COMMENT 'is a noun ',<br \/>\nintro tinyint(1) NOT NULL default '0' COMMENT 'is an intro',<br \/>\nPRIMARY KEY \u00a0(id),<br \/>\nUNIQUE KEY words (word)<br \/>\n);<br \/>\n<\/code><\/p>\n<p>You&#8217;ll want to edit this to remove pretty much everything but the most basic information and normalize the data types to <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.sqlite.org\/datatype3.html');\"  href=\"http:\/\/www.sqlite.org\/datatype3.html\">SQLite 3 Data Types<\/a>, which are <strong>TEXT, NUMERIC, INTEGER, REAL, or BLOB<\/strong>.  Your CREATE TABLE should look something like this:<\/p>\n<p><code>CREATE TABLE dictionary(<br \/>\nid  INTEGER PRIMARY KEY ASC,<br \/>\nword TEXT,<br \/>\nadjective  INTEGER,<br \/>\nnoun INTEGER,<br \/>\nintro TEXT<br \/>\n);<\/code><\/p>\n<p>I was using id as a primary key so I&#8217;ve added &#8220;PRIMARY KEY ASC&#8221; after &#8220;id INTEGER&#8221;. (Primary Keys are aliases to row IDs in SQLite, if you have questions read <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.sqlite.org\/lang_createtable.html#rowid');\"  href=\"http:\/\/www.sqlite.org\/lang_createtable.html#rowid\">this<\/a>.)<\/p>\n<p>However, I&#8217;ve noticed that the SQLite Database browser exports SQL in the even more simplified format with no datatype specification as such, which also seemed to work for me and may work just as well for your needs:<\/p>\n<p><code>CREATE TABLE dictionary (id, word, adjective, noun, intro);<br \/>\n<\/code><\/li>\n<li>Continue going through your text file and fix all CREATE TABLE lines as in #8, above.<\/li>\n<li>Check your file text encoding. BBEdit may default to Western (Mac OS Roman), which worked for me, but if you have problems you may need to set it to Unicode, depending on your character set.<\/li>\n<li>Check your file for suspicious characters that might cause the import to choke. Single quotation marks used as apostrophes show up as escaped by themselves; for example <code>you're<\/code> becomes <code>you''re<\/code>. This seemed to import fine as long as the other guidelines are followed. Here&#8217;s what an example INSERT should look like:\n<p><code>INSERT INTO dictionary VALUES(305, 'ne''er-do-well', 0, 1, 0);<br \/>\n<\/code><\/li>\n<li>Check your line endings. BBEdit defaulted to Unix (LF) which worked for me.<\/li>\n<li>Open up SQLite Database Browser, create a new database (.db) file, and import your SQL text file by selecting <strong>File&gt; Import&gt; Database From SQL File<\/strong> from the menu.<\/li>\n<li>Check your data by clicking the <strong>Browse Data<\/strong> tab and make sure everything came in all right. If not, go back over the steps. Look for weird characters, text and line encoding issues, and syntax issues. The only two commands in your file should be CREATE TABLE and INSERT, although technically it should probably begin with <code>BEGIN TRANSACTION;<\/code> and end with <code>COMMIT;<\/code><\/li>\n<\/ol>\n<p>Again these steps were derived mostly by trial-and-error, so there may be issues particular to your DB that these guidelines didn&#8217;t solve. Please feel free to add any corrections, tips, and questions to the comments area. <\/p>\n<p>Also, I know that with the iPhone OS 3.0, Core Data can take care of a lot of database functionality for you. I&#8217;d love to hear how people made the transition, and especially how anyone got a pre-existing SQL database into a Core Data store.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although I started writing this as a cheat-sheet for myself (after many hours of struggling with this by trial-and-error), I figured it would be helpful for other iPhone developers who&#8217;d like to take a mySQL database online and migrate it &hellip; <a href=\"https:\/\/www.juggleware.com\/blog\/2009\/09\/mysql-to-sqlite-cheatsheet-for-iphone-developers\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,49],"tags":[20,53,52,113,38,51,50],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-iphone-dev","category-programming","tag-apple","tag-core-data","tag-database","tag-iphone-dev","tag-mac","tag-mysql","tag-sqlite"],"_links":{"self":[{"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":27,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/posts\/140\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juggleware.com\/blog\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}