|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe objective of this article is to demonstrate functionality in the
The project file available above for download contains samples that run against the famous Northwind database on SQLite, SQL Server and Microsoft Access. For implementation details, please see the following articles 1, 2, 3. A previous article on the usage of In this article, I will focus on using SQLite and set operator functionality such as Introduction to SQLiteAccording to the blurb on the site: SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. It is used in countless desktop computer applications as well as consumer electronic devices including cellphones, PDAs, and MP3 players. The source code for SQLite is in the public domain. Wikipedia states: SQLite is known to be embedded in: In my (brief) experience with SQLite, I have found it extremely easy to set up and use and it is therefore the first non-Microsoft RDMS against which NoteSQLite is a C application and you will need to download an ADO.NET wrapper around the core SQLite library. SQLite.NET is an excellent choice and is used here. You may also want to download the SQLite Database browser which provides a GUI for SQLite. Setting Up SQLiteOnce you've downloaded and installed the above you'll be ready to run the examples in the download as the following have already been done:
Set OperatorsALLThe following query will provide a list of from c in customers
where (from o in c.Orders
select o).All(o => o.ShipCity == c.City)
select new { c.CustomerID, c.ContactName };
This will produce the following SQL statement: SELECT t0.CustomerID, t0.ContactName
FROM Customers AS t0
WHERE
( SELECT COUNT(*)
FROM Orders AS t1
WHERE ((t1.CustomerID = t0.CustomerID) AND NOT ((t1.ShipCity = t0.City)))
) = 0
That's quite a mouthful, but what we are saying essentially is that we want only those The query will produce the following results: CustomerID=ALFKI ContactName=Maria Anders
CustomerID=ANATR ContactName=Ana Trujillo
CustomerID=ANTON ContactName=Antonio Moreno
CustomerID=BERGS ContactName=Christina Berglund
CustomerID=BLAUS ContactName=Hanna Moos
CustomerID=BLONP ContactName=Frédérique Citeaux
------------------------------------------------------------
ANYThe following query will provide a list of from customer in customers
where !customer.Orders.Any()
select new { customer.CustomerID, customer.ContactName };
This will produce the following SQL statement: SELECT t0.CustomerID, t0.ContactName
FROM Customers AS t0
WHERE NOT (
( SELECT COUNT(*)
FROM Orders AS t1
WHERE (t1.CustomerID = t0.CustomerID)
) > 0
)
Here, we once again translate the contrapositive of the The query will produce the following results: CustomerID=FISSA ContactName=Diego Roel
CustomerID=PARIS ContactName=Marie Bertrand
------------------------------------------------------------
UNIONThe following query will provide a list of from c in customers.Where(d => d.City == "London")
select new { ContactName = c.ContactName })
.Union(from e in employees.Where(f => f.City == "London")
select new { ContactName = e.LastName })
This will produce the following SQL statement: SELECT t2.ContactName
FROM Customers AS t2
WHERE (t2.City = @p0)
UNION
SELECT t2.LastName
FROM Employees AS t2
WHERE (t2.City = @p1)
The query will produce the following results: ContactName=Ann Devon
ContactName=Buchanan
ContactName=Dodsworth
ContactName=Elizabeth Brown
ContactName=Hari Kumar
ContactName=King
------------------------------------------------------------
Point of InterestWhereas core functionality in RDMSs exposed through SQL tends to be very similar from database to database, more advanced functionality is often accessed in very different ways depending on which product you use. For example, the following query... from order in orders
where order.OrderDate.Value.Year > DateTime.Parse("1/1/1997").Year &&
order.CustomerID.StartsWith("B")
select new { order.CustomerID, order.OrderID, order.OrderDate };
... will translate to the following statement for SQL server: SELECT t0.CustomerID, t0.OrderID, t0.OrderDate
FROM Orders AS t0
WHERE ((datePart(yyyy, t0.OrderDate) > @p1) AND t0.CustomerID Like (@p0 + '%'))
On SQLite however, the translation will be: SELECT t0.CustomerID, t0.OrderID, t0.OrderDate
FROM Orders AS t0
WHERE ((round(strftime('%Y', t0.OrderDate)) > @p1) _
AND Like (@p0 || '%', t0.CustomerID))
The mechanism by which That's it for now. Cheers! Notes
History
|
||||||||||||||||||||||