Monday, 26 October 2009

MonoTouch "Corp411" (part III: Revenge of System.Data)

As promised, the Corp411 sample app has been 'converted' from sqlite-net to System.Data. MonoTouch 1.2 isn't quite out yet, so you must follow the instructions on this page to download the System.Data assemblies to your installation, put them in the correct place AND add references to your MonoDevelop solution for Mono.Data.Sqlite.dll, Mono.Data.Tds.dll, System.Data.dll, System.Transactions.dll (I'm not sure if they're all required, but it seemed like a sensible set).

It's pretty easy to use after all that:
// db is the path to the SQLite database file
var conn = new SqliteConnection("Data Source=" + db);
and
listData = new List<Employee>();
// System.Data from http://monotouch.net/Documentation/System.Data
var sd = new SystemDataHelper("phonebook");
var connection = sd.GetConnection();
using (var cmd = connection.CreateCommand())
{
connection.Open ();
cmd.CommandText = "SELECT Firstname, Lastname, Work, Mobile,"
+ " Department, Email "
+ " FROM Phonebook ORDER BY Lastname";
using (var reader = cmd.ExecuteReader ())
{
while (reader.Read ())
{
var emp = new Employee();
emp.Firstname = (string)reader["Firstname"];
emp.Lastname = (string)reader["Lastname"];
emp.Work = (string)reader["Work"];
emp.Mobile = (string)reader["Mobile"];
emp.Department = (string)reader["Department"];
emp.Email = (string)reader["Email"];
Console.WriteLine("Column {0}",reader["Lastname"]);
listData.Add(emp);
}
}
}
Cool eh? Doesn't that look familiar... good old ADO.NET :-)


THE CODE
That's a small part of the System.Data namespace, so the code is pretty small :) here 'tis Corp411.3.zip (38Kb)

... and proof it runs on a device ...

2 comments:

  1. Your sqlite3 version is old - please upgrade to at least v3.5.0!

    Unhandled Exception: System.EntryPointNotFoundException: sqlite3_next_stmt

    trhow exeption conexion.open()

    ReplyDelete
  2. This was discussed on the mailing list. It occurs in the simulator on Leopard (MacOS 10.5) but does NOT occur on Snow Leopard (10.6). It should also work fine on a device (regardless of the OS you're developing with).

    Jonathan Pryor said: "So what appears to be the problem is that when running within the simulator, instead of picking up the libsqlite3.dylib provided by iPhoneSimulator.platform, it instead picks up /usr/lib/libsqlite3.dylib. Snow Leopard's libsqlite3.dylib is a more recent version than Leopard's (and Snow Leopard's apparently better matches what's on the device), which is why it works on both Snow Leopard Simulator & the device."

    and on 3rd November

    "This should be fixed in the next MonoTouch release."

    ReplyDelete

Note: only a member of this blog may post a comment.