Noah Hart has ported SQLite to C#. I was intrigued to see whether the code could be adapted to run in Silverlight, which has a cut-down .NET Framework and prohibits platform invoke to native code.
I hacked away at his code until it ran in Silverlight:
Note that the sole purpose of the exercise was to see if SQLite could be made to run, not to prepare a port that is production-ready or even code that is fit to check-in. In essence, I defined SQLITE_SILVERLIGHT, removed all the locking, adapted the file I/O to use isolated storage, and generally took as many shortcuts as I could think of to get it to compile and run. Unfortunately transactions do not work, though that can probably be fixed.
As far as I can tell, Silverlight’s Isolated Storage does not support locking at all. If the same application were opened twice, for example in different browsers, that could cause problems, but in normal use it would likely be OK.
One other point of interest: the XAP file in release mode is 257KB.
Update: The sample app is here. The hacked code is here [zip].
Congratulation man! What about the sample and download?
Thanks for the effort. Do you plan on sharing your work?
@Peter I’m embarrassed to share the code, I’ve taken every possible shortcut, but maybe on condition nobody holds it against me 🙂
Tim
>>I’m embarrassed to share the code
C’mon Tim, that’s just teasing 🙂 If it helps, the code in this port of HSql in Silverlight is just as dodgy, and available to download full of //HACK: tags! Hsql-in-Silverlight suffers the same problem with isolated storage/locking: doesn’t like to be accessed by two copies of the app.
An Sqlite-port would be much more attractive from a feature perspective – would love to play with it in Silverlight and look forward to seeing it progress – great work.
Very interesting. Regarding locking and multiple applications, you may be able to use the LocalConnection API in Silverlight 3 to coordinate operations between multiple applications.
Tim,
You should make your work available for download. Everybody understands that you hacked it just to “get it to work”. There is no shame in that. Other people can then work on it, and see if it can be made to be useful with Silverlight and IsolatedStorage.
OK, I will post it later today.
Tim
Tim,
Congratluaitons. It might be a “quick hack” but I am sure it can be a good starting point for a cleaner version. Getting it to compile and work is the tough bit, then adding things back in and tweaking can be easier!
Again, well done. Gary.
Update: The sample app is here. The hacked code is here [zip].
Tim
Nice work, Tim! Checeked out your code, and I think it has some real potential. Thanks for your contribution!
hi,can it use in asp.net or winform ?
thanks a lot
Did you try this on any other Silverlight platform such as Apple or Moonlight?
Might be very useful regarding windows phone 7 Series