<?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>Software for Exploratory Data Analysis and Statistical Modelling &#187; Database Connectivity</title>
	<atom:link href="http://www.wekaleamstudios.co.uk/topics/r-environment/database-connectivity/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wekaleamstudios.co.uk</link>
	<description>Statistical Modelling with R</description>
	<lastBuildDate>Wed, 01 Feb 2012 19:44:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using SQL to Access Data in MySQL Databases</title>
		<link>http://www.wekaleamstudios.co.uk/posts/using-sql-to-access-data-in-mysql-databases/</link>
		<comments>http://www.wekaleamstudios.co.uk/posts/using-sql-to-access-data-in-mysql-databases/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 22:04:46 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Database Connectivity]]></category>
		<category><![CDATA[dbGetQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[RMySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.wekaleamstudios.co.uk/?p=376</guid>
		<description><![CDATA[In a previous post we have looked at opening and closing connections from R to a MySQL database and some basic operations for creating and deleting tables. In this post we will consider using SQL queries to extract parts of a table from the database based on different search criteria. We can run an SQL [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post we have looked at opening and closing connections from <strong>R</strong> to a <strong>MySQL</strong> database and some basic operations for creating and deleting tables. In this post we will consider using <strong>SQL</strong> queries to extract parts of a table from the database based on different search criteria.<span id="more-376"></span></p>
<p>We can run an <strong>SQL</strong> query with the <strong>dbGetQuery</strong> function which expects a connection object and a string with the <strong>SQL</strong> commands. If we wanted to take a subset of the CO2 dataset and view only those observations taken in Quebec we could use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbGetQuery(con, paste(&quot;select * from co2 where Type = 'Quebec'&quot;))
   row_names Plant   Type  Treatment conc uptake
1          1   Qn1 Quebec nonchilled   95   16.0
2          2   Qn1 Quebec nonchilled  175   30.4
...
41        41   Qc3 Quebec    chilled  675   39.6
42        42   Qc3 Quebec    chilled 1000   41.4</pre></div></div>

<p>We can narrow the search by specifying a condition on one of the other columns, for example limit the search to concentrations under 300 units:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbGetQuery(con, paste(&quot;select * from co2 where Type = 'Quebec' and Conc &lt; 300&quot;))
   row_names Plant   Type  Treatment conc uptake
1          1   Qn1 Quebec nonchilled   95   16.0
2          2   Qn1 Quebec nonchilled  175   30.4
...
17        37   Qc3 Quebec    chilled  175   21.0
18        38   Qc3 Quebec    chilled  250   38.1</pre></div></div>

<p>As a final example we could look for all observations with a concentration less than 300 units buy order the data by increasing values of uptake:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbGetQuery(con, paste(&quot;select * from co2 where Conc &lt; 300 order by uptake&quot;))
   row_names Plant        Type  Treatment conc uptake
1         71   Mc2 Mississippi    chilled   95    7.7
2         29   Qc2      Quebec    chilled   95    9.3
...
35        38   Qc3      Quebec    chilled  250   38.1
36        17   Qn3      Quebec nonchilled  250   40.3</pre></div></div>

<p>This only scratches the surface of the <strong>SQL</strong> statements that could be run to extract data from a <strong>MySQL</strong> database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wekaleamstudios.co.uk/posts/using-sql-to-access-data-in-mysql-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using R to Access Data in a MySQL database</title>
		<link>http://www.wekaleamstudios.co.uk/posts/using-r-to-access-data-in-a-mysql-database/</link>
		<comments>http://www.wekaleamstudios.co.uk/posts/using-r-to-access-data-in-a-mysql-database/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 07:57:39 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Data Manipulation]]></category>
		<category><![CDATA[Database Connectivity]]></category>
		<category><![CDATA[data frame]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[dbConnect]]></category>
		<category><![CDATA[dbDisconnect]]></category>
		<category><![CDATA[dbListTables]]></category>
		<category><![CDATA[dbReadTable]]></category>
		<category><![CDATA[dbRemoveTable]]></category>
		<category><![CDATA[dbWriteTable]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[RMySQL]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://www.wekaleamstudios.co.uk/?p=359</guid>
		<description><![CDATA[The R import/export manual discusses various approaches to handling data and mentions that R is not suitable for working with large data sets because data objects are stored in memory during a session. There are situations where using a database to hold the data and making use of one of the R libraries for database [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>R</strong> import/export manual discusses various approaches to handling data and mentions that <strong>R</strong> is not suitable for working with large data sets because data objects are stored in memory during a session. There are situations where using a database to hold the data and making use of one of the R libraries for database connectivity to access the data or to save the data.<span id="more-359"></span></p>
<p><strong>MySQL</strong> is a popular database system and there is a library <strong>RMySQL</strong> that can be used to access this database &#8211; it is important to ensure that the version of <strong>MySQL</strong> matches with the R library. If there isn&#8217;t a match then the system might exhibit erratic behaviour.</p>
<p>The first step is to make the <strong>RMySQL</strong> library available in the working session:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">library(RMySQL)</pre></div></div>

<p>If this command runs without problems then we need to create a connection object for our session:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">con = dbConnect(MySQL(), dbname = &quot;test&quot;, user = &quot;user1&quot;,
  password = &quot;pwd1&quot;)</pre></div></div>

<p>The first argument to this function <em>creates and initializes a MySQL client. It returns an driver object that allows you to connect to one or several MySQL servers</em>. The <strong>dbname</strong> argument is used to specify the name of the database and the <strong>user</strong> and <strong>password</strong> arguments should be self-explanatory.</p>
<p>After creating our connection successfully we can get <strong>R</strong> to list the tables that are stored in this database:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbListTables(con)
[1] &quot;co2&quot;</pre></div></div>

<p>In this example there is only one table and its name is returned by the <strong>dbListTables</strong> function. To read the data from this table we use the <strong>dbReadTable</strong> function and specify the connection object as well as the name of the table in the <strong>MySQL</strong> database:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbReadTable(con, &quot;co2&quot;)
   Plant        Type  Treatment conc uptake
1    Qn1      Quebec nonchilled   95   16.0
2    Qn1      Quebec nonchilled  175   30.4
3    Qn1      Quebec nonchilled  250   34.8
...</pre></div></div>

<p>We can save the table to a data frame object rather than the default action of printing to the console.</p>
<p>After undertaken some analysis we might want to save a data set to the database and the <strong>dbWriteTable</strong> function is used:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbWriteTable(con, &quot;CO2&quot;, data.frame(CO2),
  overwrite = TRUE)
[1] TRUE</pre></div></div>

<p>The first argument is the connection object, the second is the name that the table will be referred to in the database and the third argument is the data to be saved. In this case we have used the <strong>overwrite</strong> argument to copy over any existing table of the same name.</p>
<p>We can delete a table using the <strong>dbRemoveTable</strong> function:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbRemoveTable(con, &quot;CO2&quot;)
[1] TRUE</pre></div></div>

<p>and when we reach the end of our need for the connection, the <strong>dbDisconnect</strong> function will remove the connection that we have been using:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; dbDisconnect(con)
[1] TRUE</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.wekaleamstudios.co.uk/posts/using-r-to-access-data-in-a-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

