<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Karl Kraft &#187; MySQL</title>
	<atom:link href="http://www.karlkraft.com/index.php/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.karlkraft.com</link>
	<description>Just a 2 bit programmer in a 64 bit world</description>
	<lastBuildDate>Wed, 18 Jan 2012 16:01:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Mysql SVN Repository Access</title>
		<link>http://www.karlkraft.com/index.php/2012/01/18/mysql-svn-repository-access/</link>
		<comments>http://www.karlkraft.com/index.php/2012/01/18/mysql-svn-repository-access/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 15:39:48 +0000</pubDate>
		<dc:creator>Karl Kraft</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.karlkraft.com/?p=176</guid>
		<description><![CDATA[My source code for the Mysql Objective-C connector is now available via Subversion. You can grab the latest version at https://svn.karlkraft.com/mysql_connector/trunk The current trunk is in production use so the trunk is seldom broken. This update now requires ARC. Previous builds are available in https://svn.karlkraft.com/mysql_connector/versions]]></description>
			<content:encoded><![CDATA[<p>My source code for the Mysql Objective-C connector is now available via Subversion.  You can grab the latest version at https://svn.karlkraft.com/mysql_connector/trunk</p>
<p>The current trunk is in production use so the trunk is seldom broken.  This update now requires ARC.  Previous builds are available in https://svn.karlkraft.com/mysql_connector/versions</p>
]]></content:encoded>
			<wfw:commentRss>http://www.karlkraft.com/index.php/2012/01/18/mysql-svn-repository-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql for iPhone and OSX &#8211; Version 2.0</title>
		<link>http://www.karlkraft.com/index.php/2011/06/07/mysql-for-iphone-and-osx-version-2-0/</link>
		<comments>http://www.karlkraft.com/index.php/2011/06/07/mysql-for-iphone-and-osx-version-2-0/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 20:15:22 +0000</pubDate>
		<dc:creator>Karl Kraft</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.karlkraft.com/?p=162</guid>
		<description><![CDATA[It has been a while since I released an update to my MysqlConnector but I wanted to get this and a few other things out before WWDC, but it didn&#8217;t happen. The zip file weighs in at about 11 MB. Most updates will require no changes. Things will just work better. MysqlConnection supports an idle [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I released an update to my MysqlConnector but I wanted to get this and a few other things out before WWDC, but it didn&#8217;t happen.  <span id="more-162"></span><a href="http://www.karlkraft.com/wp-content/uploads/2011/06/MysqlConnector_2.0.zip" title="MysqlConnector_2.0.zip" alt="">The zip file</a>  weighs in at about 11 MB.</p>
<p>Most updates will require no changes.  Things will just work better.</p>
<p>MysqlConnection supports an idle loop that sends a periodic request to the MySQL server to keep the connection alive.  However this wasn&#8217;t protected in multi threaded programs and could crash.</p>
<p>MysqlFetch was creating the name of the field on each row iteration.  On large selects this resulted in a large amount of duplicated memory being allocated.</p>
<p>MysqlFetch now supports the DECIMAL type.</p>
<p>MysqlFetch now deals with negative LONG and LONGLONG values. Corrected the size of these types on 64 bit compiles to match the MySQL definition.  Also fixed the use of TINY, FLOAT and DOUBLE.  Thanks to Bill Allen for contributing this code.</p>
<p>Added support for connecting to multiple servers in a failover pattern.  Connection are now created with a MysqlServer instance or array of the same.  This will require a code change, and you will need to add MysqlServer to your project.</p>
<p>Fixed some debug messages.  Two of them had bad formatting, and one was still using NSLog.</p>
<p>Added a method to check status of autocommit.</p>
<p>Add ability to ignore mysql error when a record is inserted that matches a previously inserted records unique value for 1 or more columns. I have to admit that I ended up not using this code, so it is of dubious value.</p>
<p>The length of data passed to mysql_stmt_prepare needs to be expressed in byte length, not character length.   Thanks to Coupez Julien for pointing this out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.karlkraft.com/index.php/2011/06/07/mysql-for-iphone-and-osx-version-2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql for iPhone and OSX</title>
		<link>http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/</link>
		<comments>http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 08:07:03 +0000</pubDate>
		<dc:creator>Karl Kraft</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.karlkraft.com/?p=149</guid>
		<description><![CDATA[A while back I posted an Objective-C library for accessing MySQL, but it was limited to working on Desktop OSX, and had no support for running on the iPhone or iPad.  This release fixes that. Download ﻿Objc_Mysql_iPhone_OSX_1.0.zip (7.3 MB), while you read the rest.   Support for iOS I had been hoping that with the announcement [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://www.karlkraft.com/index.php/2010/06/02/mysql-and-objective-c/">while back</a> I posted an Objective-C library for accessing MySQL, but it was limited to working on Desktop OSX, and had no support for running on the iPhone or iPad.  This release fixes that.</p>
<p>Download ﻿<a title="Objc_Mysql_iPhone_OSX_1.0.zip" href="http://www.karlkraft.com/wp-content/uploads/2010/09/Objc_Mysql_iPhone_OSX_1.0.zip">Objc_Mysql_iPhone_OSX_1.0.zip</a> (7.3 MB), while you read the rest.</p>
<p><span id="more-149"></span>
<p> </p>
<p><span style="font-size: 18px; font-weight: bold;">Support for iOS</span></p>
<p>I had been hoping that with the announcement of new devices and versions of iOS, that Apple might add garbage collection to the iOS platform, but this appears to not be happening.  So the time was right to bite the bullet and clean this up so it worked on iOS.</p>
<p>﻿Before you jump with both feet into using MySQL on iOS, let me point out a few reasons you may not want to use this as a solution.</p>
<p>If you are planning on submitting an application with this library to the AppStore, there is no guarantee that it will be accepted by Apple.  While I use this library on a daily basis for my code, I don&#8217;t have any plans to submit anything to the Apple Store using this library, for the reasons I will outline shortly.  So an AppStore submission that uses this library will be on the bleeding edge.</p>
<p>While MySQL is popular, it is not designed to be a forward facing internet accessible application.  All of my MySQL deployments are hidden behind firewalls with limited access.    If you use this to allow people to access a public MySQL database, you are asking for trouble, both in terms of load handling and security.  Keep in mind that AppStore apps are not encrypted, only signed.  So extracting the username, password and host from your binary is a trivial exercise.</p>
<p>You may find this useful however for in house iOS applications, which is where I am using it.</p>
<p><span style="font-size: 18px; font-weight: bold;">Features, functionality and bug fixes﻿</span></p>
<h2><span style="font-weight: normal; font-size: medium;">In adition to the major new feature of iOS support, this version has several bug fixes, plus some added functionality.</span></h2>
<p> </p>
<p>﻿This version features 6 architectures in a single static library.  The architectures are  armv6 and armv7 for iOS, and ppc, ppc64, i386, and x86_64 for desktop OSX.  While this increases the download size, only the architectures matching your target will be included in your final product.  So if you are building for iPhone 4 only, then only armv7 will be included.  If you are building for Snow Leopard on 64 bit, then only x86_64 will be included.</p>
<p>The iPhone versions should work on 3.0 all the way up to 4.1.</p>
<p>Since the iPhone does not have garbage collection, I had to #ifdef some code to get it working correctly there.  While it has not been tested extensively, the most common cases have been tested with Instruments, and there are no leaks.</p>
<p> </p>
<p>Since the most common case for non garbage collected environments is the iPhone, the default size for blobs is is 10K on those platforms and 1M in garbage collected environments.  Blobs under those sizes are grabbed in a single pass, blobs larger than that take a second pass, which is slower, but transparently done after the initial fetch.  Of course, since source is included, you can change that if you have special needs.</p>
<p>MysqlFetchField will now identify which fields are part of the primary key.</p>
<p>Even though the classes were garbage collected, the underlying MySQL ANSI-C connector was not, and there were some bugs related to memory allocation that have been cleaned up.</p>
<p>MysqlConnection now supports strict mode.  In strict mode, if you insert data that is too long for a field it will throw an exception instead of being silently truncated.</p>
<p>While this code is in day to day use in dozens of projects, it by no means is bug free.  Caveat emptor.  In particular, since iOS does not support NSCalendarDate I needed to move to NSDateComponents.  As a result, when invalid dates like &#8217;0000-00-00 00:00:00&#8242; are fetched, they will be munged into something else. (although it appears consistent)</p>
<p> </p>
<p> </p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql and Objective-C</title>
		<link>http://www.karlkraft.com/index.php/2010/06/02/mysql-and-objective-c/</link>
		<comments>http://www.karlkraft.com/index.php/2010/06/02/mysql-and-objective-c/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 02:02:15 +0000</pubDate>
		<dc:creator>Karl Kraft</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.karlkraft.com/?p=137</guid>
		<description><![CDATA[In honor of Objective-C becoming a top-ten language, I&#8217;ve decided to share a library of source code I created to access MySQL. Update: A newer post has a version of this library that works on the iPhone as well. Over the past 2 years I&#8217;ve worked on a small bit of glue to make selects, [...]]]></description>
			<content:encoded><![CDATA[<p>In honor of <a href="http://apple.slashdot.org/story/10/06/02/1930209/Objective-C-Enters-Top-Ten-In-Language-Popularity">Objective-C becoming a top-ten language</a>, I&#8217;ve decided to share a library of source code I created to access MySQL.</p>
<p><span id="more-137"></span>
<p><em>Update: A <a href="http://www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/">newer post</a> has a version of this library that works on the iPhone as well.</em></p>
<p>Over the past 2 years I&#8217;ve worked on a small bit of glue to make selects, updates, inserts and deletes work easily from Objective-C.  This includes support for converting MySQL types to Cocoa types, working correctly with garbage collection, and support for reading and writing large blobs.</p>
<p>It is possible to do all these things with the generic MySQL C connector, and if your needs are simple that can be a quick way to access a MySQL databases, since all C libraries can easily be called from Objective-C.  However, once you start dealing with more advanced topics, the C connector can become burdensome.</p>
<p>If you are working on intel platforms only, you should grab the intel only version.  This supports both 32 and 64 bit intel platforms, using version 6.0.2 of the MySQL C Connector.</p>
<p><a href="http://www.karlkraft.com/wp-content/uploads/2010/06/ObjC_mysql_intel.zip">ObjC_mysql_intel.zip﻿</a> 5.5 MB &#8211; 32/64 bit</p>
<p>I still have some client programs running on PPC boxes, but MySQL has dropped support for PPC based OSX﻿ in the most recent versions of the mysql_connector.  So I have an older version that I occasionally update, although not as often as the intel only version.  This version uses an older version (5.0.51a) of the MySQL C connector, and supports both 32 bit PPC and Intel platforms.</p>
<p><a title="ObjC_mysql_ppc.zip" href="http://www.karlkraft.com/wp-content/uploads/2010/06/ObjC_mysql_ppc.zip">ObjC_mysql_ppc.zip</a> ﻿2.3 MB &#8211; 32 bit intel and PPC.</p>
<p>Both versions use an identical syntax and are for the most part interchangeable.</p>
<p>﻿Both require that garbage collection be enabled in your application.  This means this version won&#8217;t support versions of iPhone OS that don&#8217;t include garbage collection. Which is pretty much all of them, as of the writing of this post.</p>
<p>Both include the headers and a static library for the MySQL C connector.</p>
<p>While I have used these libraries to process well over half a billion rows of MySQL data, they are not by any means guaranteed to be free of bugs.  The most recent fix was a leak in MysqlUpdate.  If you are interested in getting updates, subscribe to the <a href="http://www.karlkraft.com/index.php/category/mysql/feed/">MySQL Category Feed</a>, or <a href="mailto:karl@karlkraft.com?subject=MySQL SVN Access Request">email me</a> for access to the subversion repository for the project.</p>
<h1 style="font-size: 2em;">Adding to Your Project</h1>
<p>Once you have extracted the zip, add everything inside to your project.  The static mysql library will be linked into your application, and the headers and all the necessary bits to compile should just fall into place.</p>
<h3>Forming the connection</h3>
<p>You connect by creating an instance of MysqlConnection.  The best method for starting is</p>
<p> </p>
<pre>+ (MysqlConnection *)connectToHost:(NSString *)host
                              user:(NSString *)user
                          password:(NSString *)password
                            schema:(NSString *)schema
                             flags:(unsigned long)flags;
</pre>
<p>The best option for flags is MYSQL_DEFAULT_CONNECTION_FLAGS﻿</p>
<p>If you are connecting to a MySQL server that will kick you if inactive, call the method</p>
<pre>-[MysqlConnection startIdle]</pre>
<p>to start a low impact periodic select that will run once every 60 seconds.  Four additional methods add support for transactions</p>
<h3>Performing Selects</h3>
<p>Selects are pretty straight forward. Use MysqlFetch, and pass a SQL select command.  You can then look at the results property to get an array of dictionaries.  Each dictionary represents a single row.  They key will be the name of each column, and the value will be the Objective-C type that matches the column. If you aren&#8217;t sure of the names of the fields, you can look at the fields an fieldNames properties for more details.</p>
<pre>Example:
MysqlConnection *connection = [MysqlConnection connectToHost:@"serverName"
                                                        user:@"aUserName"
                                                    password:@"aPassword"
                                                      schema:@"myForumName"
                                                       flags:MYSQL_DEFAULT_CONNECTION_FLAGS];
MysqlFetch *userFetch = [MysqlFetch fetchWithCommand:@"select U_Number,U_Username from w3t_Users"
                                        onConnection:connection];
QLog(@"There are %d members",[userFetch.results count]);
for (NSDictionary *userRow in userFetch.results) {
  NSNumber *userNumber = [userRow objectForKey:@"U_Number"];
  NSString *userName = [userRow objectForKey:U_Username"];
  NSLog("%@ %@",userNumber,userName);
}
</pre>
<h3>Performing Deletes</h3>
<p>Deletes, inserts, and updates are very similar.  To perform a delete:</p>
<pre>MysqlDelete *deleteCommand = [MysqlDelete deleteWithConnection:connection];
delete.tableName=@"w3t_Users";
delete.qualifier=[NSDictionary dictionaryWithObject:@"3" andKey:@"U_Number"];
[deleteCommand execute];
</pre>
<p>You can then look at the affectedRows property to find out how many rows were deleted.</p>
<h3>Performing Inserts</h3>
<p>Inserts allow you to insert NSString, NSNumber, NSData, and NSNull.  All other classes are converted using their -description method.  The insert code uses bindings instead of composing a SQL string, so you don&#8217;t need to worry about escaping special characters, or filtering user input.</p>
<pre>MysqlInsert *insertCommand = [MysqlInsert insertWithConnection:connection];
insertCommand.table =@"w3t_Users";
NSData *picture = [NSData dataWithContentsOfFile:@"sample.png"];
NSString *password=@";'!#";
insertCommand.rowData=[NSDictionary dictionaryWithObjectsAndKeys:@"Karl Kraft",@"U_Username",
                                                          picture,@"U_UserImage"
                                                          password,@"UserPassword"
                                                          nil];
[insertCommand execute];
</pre>
<p>After the insert, you can get the auto increment rowid by using the rowid property on your MysqlInsert instance.</p>
<h3>Performing Updates</h3>
<p>Once you have mastered delete and insert, updates are a simple cross of the two.  Specify a qualifier, like you would for deletion, but rowData like you would use for an insert.  Only the specified fields in the rowData are modified.  Again you can pass NSString, NSNumber, NSData, and NSNull, and the values are inserted using bindings so that you don&#8217;t need to provide odd escapes.</p>
<p> </p>
<pre>MysqlUpdate *updateCommand = [MysqlInsert insertWithConnection:connection];
updateCommand.table =@"w3t_Users";
updateCommand.qualifier=[NSDictionary dictionaryWithObject:@"3" andKey:@"U_Number"];
NSData *picture = [NSData dataWithContentsOfFile:@"sample.png"];
NSString *password=@";'!#";
updateCommand.rowData=[NSDictionary dictionaryWithObjectsAndKeys:@"Karl Kraft",@"U_Username",
                                                          picture,@"U_UserImage"
                                                          password,@"UserPassword"
                                                          nil];
[updateCommand execute];
</pre>
<p>Like delete you can query the affectedRows property to find out how many records were updated.</p>
<h3>Catching exceptions</h3>
<p>Any exceptions while performing the mysql commands are thrown as MysqlException or a subclass of MysqlException.  Common reasons for exceptions being throw are:</p>
<ul>
<li>Unable to connect to the database, or wrong username / password </li>
<li>Commit or rollback fails </li>
<li>Unsupported field type in a MysqlFetch </li>
<li>Unqualified delete or update commands </li>
<li>Any underlying mysql error </li>
</ul>
<h3>MYSQL_LOGGING</h3>
<p>As a final useful trick you can define MYSQL_LOGGING in the CFLAGS of your project, or in a common header file, and useful debugging information will be generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.karlkraft.com/index.php/2010/06/02/mysql-and-objective-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

