Post: SQLite-you can create and run it on. NET in 3 minutes.

Source: Internet
Author: User
Tags sqlite db sqlite download sqlite tutorial

From: http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/

 

SQLite on. NET-Get up and running in 3 minutes.

January 15th, 2008 | grokable

You found it! The quick and dirty guide to setting up SQLite. net in 3 minutes. small, fast, and ass-kicking like a transactional Jackie Chan. at least that's what this campy image created just for this post says. (shout out to righteous graphics dude Brian cook for the sweet photoshoppery)

SQLite: the compelling Introduction
You're writing some smallish app. you know the type. those little one-offs where you clean up some data, scrape your competitor's Web site, change traffic light colors, blah. it's cool enough to warrant it's own solution, but not so big that you are going to tie into your company's SQL Server instances. let's face it, you don't really need the hassystemic of dealing Power tripping DBA/IT guys for this project, especially now that their delusions of grandeur have been magnified by the return of American Gladiators to prime time.

Enter SQLite: the quick skinny/speed-date overview:
SQLite, if you weren't already aware, is an open source, tiny (600 K !), * Zero install *, transactional database. But theres more!SQLite is as fast or faster than some of the big guys you use every day, Largely because there is no server process, no listeners, no FD & C yellow #5, none of that crapola. the database is just a single file and you communicate with it through a linked in library. think of the old access days, but not really.

SQLite implements most of the SQL-92 standard. all of the query related stuff you actually care about is there, triggers, views, etc. the only thing missing is stored procedures but that's probably not a requirement for your project, if at all. and no, I don't want to get in to the old SPS. not-SPS tangent, man thats so aggressively nerdy in a Kirk vs Picard way.

... But I digest ....

As the SQLite home page will happily tell you, "SQLite is the most widely deployed SQL database engine in the world. "It's used by little guys you may have heard of like, oh, I don't know, Adobe, apple, Firefox, GE, Google, Skype, sun, and several shadow governments best left un-named. the crazy part is that you rarely hear about SQLite as a real. net dude/dudette developer. I think this is because it's often bundled inside of things you didn't really think were using a database like Firefox or Google gears, but the research bears out that this bad boy is more than capable for real-life tasks like being the back-end dB for dynamic web sites with some pretty serous traffic.

SQLite is great for that small-mediumish application we already spec-ed out so lets get into the quick and dirty setup to get this hog Rockin 'on. NET.

SQLite: the quick and dirty setup for. net.

1) download SQLite
While you can get the generic Windows binary on the SQLite download page, I'm going to recommend you instead grab the ADO. NET 2.0 provider for SQLite from SourceForge. i'm not saying this is the most variable t version (it does have an ADO wrapper with its attendant malarkey ), but it really is a super-easy starting implementation that's probably good enough for the long haul.

2) copy the resultant DLL(System. Data. SQLite. dll) to your project and add a reference.

3) download and install one of the billions of SQLite GUI clients. I 've been using the aptly named "SQLite administrator" (free) which has a sweet, query analyzer-alike interface. You can find a big list of slqlite GUI clients here http://www.sqlite.org/cvstrac/wiki? P = managementtools if you are so inclined.

4) through the GUI, create a databaseAnd make a test table of whatever floats your boat. The result will be a single file with a. s3db extension.

5) There is no step 5! Done!You can now query, insert, update, delete, create, truncate, etc, to your heart's content using the system. data. SQLite ADO wrapper. here is a little helper dB util type class to show you the basic schleck:

Public static datatable getdatatable (string SQL)
{
Datatable dt = new datatable ();
Try
{
Sqliteconnection CNN = new sqliteconnection ("datasource = C: checkoutworlddominator. s3db ");
CNN. open ();
Sqlitecommand mycommand = new sqlitecommand (CNN );
Mycommand. commandtext = SQL;
Sqlitedatareader reader = mycommand. executereader ();
DT. Load (Reader );
Reader. Close ();
CNN. Close ();
}
Catch
{
// Catching exceptions is for Communists
}
Return DT;
}

Public static int executenonquery (string SQL)
{
Sqliteconnection CNN = new sqliteconnection ("Data Source = C: checkoutworlddominator. s3db ");
CNN. open ();
Sqlitecommand mycommand = new sqlitecommand (CNN );
Mycommand. commandtext = SQL;
Int rowsupdated = mycommand. executenonquery ();
CNN. Close ();
Return rowsupdated;
}

Public static string executescalar (string SQL)
{
Sqliteconnection CNN = new sqliteconnection ("datasource = C: checkoutworlddominator. s3db ");
CNN. open ();
Sqlitecommand mycommand = new sqlitecommand (CNN );
Mycommand. commandtext = SQL;
Object value = mycommand. executescalar ();
CNN. Close ();
If (value! = NULL)
{
Return Value. tostring ();
}
Return "";
}

Note: The above code is quicky crap. It's just to show you the gist.

Some uses for SQLite to consider:

Configs/settings:SQLite is a good alternative to XML config files or registry settings for things like user account info and application preferences. It supports encryption, so feel free to keep your brilliant Patent ideas in there.

Persistent Caching:You can use SQLite as a DB for cache data that needs to persist through reboots/Application recycles. maybe you have some expensive one-time-on-loadup queries from your enterprise DB that you cache up and use in your website or app. by timestamping the data into a local SQLite dB, you can live through application restarts and only refresh your cache at the threshold you want.

Slicing and dicing data:Load in some data and query to your heart's content. great for analyzing data at your leisure, worming through subsets of data, etc. since its just a little dB on your on box, no one is going to hassystemic you. managers who were once developers will appreciate being to query through data with SQLite. using Excel as they usually are too crusty to still have permissions on the real dB.

Full dB for one-off apps:Sometimes you write a quickie app that just harvests something in a funky way and collects data. you can output the data as you are grabbing it in all kinds of ways, but throwing into a DB is ideal.

Linkage to some more in-depth stuff ..........

-SQLite is pretty hip with the alt.net scene, you can follow on from here to check out a SQLite Nhibernate provider, or here for a SQLite subsonic provider.

-If you are rockin the 3.0 framework there is even a SQLite LINQ provider.

-For a comprehensive SQLite how-to (emphasizing command line, non-MS specific): SQLite tutorial

-And in case you missed it up top, the main SQLite project page is here.

Have a good idea for ways to use SQLite in. Net projects? Been hit in the groin with a football for even suggesting using something like this at your company? Have some award-winning successes or outlandish failures already with SQLite?Leave a funky-fresh comment!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.