<?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>Undercrank &#187; query</title>
	<atom:link href="http://undercrank.com/posts/tagged/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://undercrank.com</link>
	<description></description>
	<lastBuildDate>Tue, 06 Jul 2010 18:05:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Importing CSV files to SQLite</title>
		<link>http://undercrank.com/posts/2005/02/importing-csv-files-to-sqlite/</link>
		<comments>http://undercrank.com/posts/2005/02/importing-csv-files-to-sqlite/#comments</comments>
		<pubDate>Thu, 03 Feb 2005 23:52:45 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://undercrank.com/?p=87</guid>
		<description><![CDATA["SQLite is different from most other SQL database engines in that its primary design goal is to be simple".  Which isn't strictly true...
 <a href="http://undercrank.com/posts/2005/02/importing-csv-files-to-sqlite/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst experimenting with the new features in PHP5, I recalled that one of my current projects had a very weak Class file that was using a <acronym title="Comma Separated Value">CSV</acronym> file to look up some data &#8211; which struck me that it would benefit greatly from being switched to use the in-built <a href="http://www.zend.com/php5/articles/php5-mysqli.php">SQLite support in PHP5</a>.  In fact, backing up my theory, the SQLite site has a page entitled <q><a href="http://www.sqlite.org/whentouse.html">Situations Where SQLite Works Well</a></q>, and enthused that &#8220;Raw data can be imported from CSV files&#8221;, and of topped off the excitement with the claim that &#8220;SQLite is different from most other SQL database engines in that its primary design goal is to be simple&#8221;.</p>
<p>And I always like simple.  But of course, it&#8217;s anything but.</p>
<p><span id="more-87"></span></p>
<p>The problem soon arises that there&#8217;s er, a bit of a lack of documentation how to actually get a CSV file into a SQLite database. So, cutting a long story short, here&#8217;s how I did it:</p>
<ol>
<li>Make sure that the source file is stripped of quotation marks.  I found the quickest way to do this on a larger file was to use the tiny freeware DOS utility <a href="http://avolys.pcse.fr/html/tools32.htm">Binsub</a> with the following syntax:<br />
<code><br />
binsub my.csv /034<br />
</code><br />
&#8230; where 034 is the decimal ASCII code for a double quote.
</li>
<li>
Use the utility <a href="http://www.microemacs.de/tcltk_dgsqlite.html">dgSQLite</a> to create a new database, and subsequently import your CSV into a table of your choice.  This utility does most of the grunt work for you, and I think requires TCL/Tk for Windows to run.  I had previously tried and failed to get things moving with the slightly more user-friendly <a href="http://sqlitebrowser.sourceforge.net/">SQLite Browser</a>, however that only worked with SQLite 2.x files.  (And somehow along the way I decided I was using 3.x.)
</li>
</ol>
<p>Lastly, to see if it worked, you can try the following snippet in your PHP5 installation:</p>
<p><code language="php"><br />
$db = new SQLiteDatabase("path/to/your/database.sqlite");<br />
$result = $db->query('SELECT * FROM your_table_name ');<br />
while ($result->valid()) {<br />
$result = $result->current();<br />
print_r($result);<br />
$result->next();<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://undercrank.com/posts/2005/02/importing-csv-files-to-sqlite/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Despamming Shortstat</title>
		<link>http://undercrank.com/posts/2005/01/despamming-shortstat/</link>
		<comments>http://undercrank.com/posts/2005/01/despamming-shortstat/#comments</comments>
		<pubDate>Sat, 22 Jan 2005 06:28:45 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[blacklist]]></category>
		<category><![CDATA[de-spamming]]></category>
		<category><![CDATA[despam]]></category>
		<category><![CDATA[Jay Allen]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[referer spam]]></category>
		<category><![CDATA[Shaun Inman]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://undercrank.com/?p=86</guid>
		<description><![CDATA[I've been using Shaun Inman's Shortstat package for a short while now as my main source of web statistics.  However, it's fairly susceptible to the, er, 'innovation' known as referer spam - so here's some code that use's Jay Allen's MT-Blacklist master list to clean it up.
 <a href="http://undercrank.com/posts/2005/01/despamming-shortstat/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <cite>Shaun Inman</cite>&#8216;s <a href="http://shortstat.shauninman.com/free/">Shortstat</a> package for a short while now as my main source of web statistics.  However, as with most other blog-related things these days, it&#8217;s fairly susceptible to the <q>innovation</q> known as referer spam.</p>
<p><strong>Note:</strong> This script has been <a href="http://undercrank.com/posts/05/02/despamming-shortstat-part-2">updated somewhat</a> now, but the basic story here remains the same.</p>
<p>Anyway, this got me thinking, and cutting a long story short it occurred to me that I already had a great blacklist of spam domains supplied by <cite>Jay Allen</cite>&#8216;s <a href="http://www.jayallen.org/projects/mt-blacklist/">MT-Blacklist</a>, and it shouldn&#8217;t be so hard to use this list as a basis to remove the referer spam from the Shortstat database &#8211; and so, here&#8217;s the results of about 13 minutes of investigation:</p>
<p><code language="php"><br />
include_once("configuration.php");<br />
include_once("functions.php");<br />
if ($shortstat) {<br />
SI_pconnect();<br />
$urlpatterns = mysql_query("SELECT ext_bl_item_text FROM mt_ext_bl_item");<br />
while ($row = mysql_fetch_array($urlpatterns, MYSQL_NUM)) {<br />
$query = "DELETE FROM si_shortstat WHERE domain LIKE \"%$row[0]\"";<br />
@mysql_query($query);<br />
}<br />
}<br />
</code></p>
<p>I&#8217;ve called this &#8220;_despam.php&#8221; and installed in the Shortstat installation directory it will use your existing database connection settings.  Note &#8211; the script makes the assumption that MT-Blacklist has been set up to use the same MySQL database that Shortstat uses, but that said, I imagine that&#8217;s most setups.</p>
<p>It certainly could do with a few more features (actually reporting back what it&#8217;s done/doing would be a start), but the basic functionality is there and lovely shiny clean reports are the result.</p>
<p><strong>Update:</strong> Tony at <a href="http://www.juju.org/archives/2005/01/21/derefspam">juju.org</a> has taken things a step further with a Perl script that &#8216;de-spams&#8217; your server log files using MT-Blacklist too.</p>
]]></content:encoded>
			<wfw:commentRss>http://undercrank.com/posts/2005/01/despamming-shortstat/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
