Undercrank

Scripting

February 3, 2005

Importing CSV files to SQLite

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 CSV file to look up some data - which struck me that it would benefit greatly from being switched to use the in-built SQLite support in PHP5. In fact, backing up my theory, the SQLite site has a page entitled Situations Where SQLite Works Well, and enthused that "Raw data can be imported from CSV files", and of topped off the excitement with the claim that "SQLite is different from most other SQL database engines in that its primary design goal is to be simple".

And I always like simple. But of course, it's anything but.

The problem soon arises that there'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's how I did it:

  1. 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 Binsub with the following syntax: binsub my.csv /034 ... where 034 is the decimal ASCII code for a double quote.
  2. Use the utility dgSQLite 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 SQLite Browser, however that only worked with SQLite 2.x files. (And somehow along the way I decided I was using 3.x.)

Lastly, to see if it worked, you can try the following snippet in your PHP5 installation:

$db = new SQLiteDatabase("path/to/your/database.sqlite"); $result = $db->query('SELECT * FROM your_table_name '); while ($result->valid()) { $result = $result->current(); print_r($result); $result->next(); }

More of the same RSS

Yahoo! Web Services news over Atom

aka: "When you realise Yahoo! already serve their news over RSS..."

Yahoo! Web Services news over RSS

I've put together a small project that will create an dynamic RSS 2.0 feed based on the Yahoo! News Search hooks.

Despamming Shortstat (Part 2)

Further to my earlier post about Despamming Shortstat, I've made a small update to the code that has a few improvements on the original.

Despamming Shortstat

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.

Trackbacks

Trackback URL for this entry is:

Comments

Andrew on March 10, 2006

You can also do this from the sqlite command line tool (sqlite3 in my case). I ran the following commands:

sqlite> .mode csv
sqlite> .import ./test.csv test

I created a really simple table to test with.

sqlite> .schema test
CREATE TABLE test (name varchar(255) not null, blah varchar(255) not null);

And my CSV file:

andrew,nklnsdf
blah,kls
joey,aknlkn
jim,asdf

After the import:

sqlite> select * from test;
andrew|nklnsdf
blah|kls
joey|aknlkn
jim|asdf

Jakob on March 16, 2006

Sorry, but step 1 (Make sure that the source file is stripped of quotation marks.) already makes the rest of this website irrelevant (to me).

While your procedure may be valuable to some people, I often work with databases containing free text, including commas and quotationmarks.

I guess I'll have to search on....

bart on May 6, 2006

Things can change quickly. The home page for SQLite browser, http://sqlitebrowser.sourceforge.net/, claims that version 1.2, released 2 months after your post, is compatible with SQLite 3.

Furbo on September 10, 2006

The easiest way to do this is with the tksqlite application available for download at:

http://reddog.s35.xrea.com/wiki/TkSQLite.html

I'm surprised it has not received more attention as it is (in my opinion) the best free tool for working with sqlite.

Post a comment

Remember personal info?