Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite join in embedded database tables

I am currently writing a Windows Store Application for Windows 8 and I am using SQLite for the persistence on the embedded database in a class library for windows apps. I am trying to join data from two different tables in my embedded database but SQLite keeps throwing a not supported exception from their GenerateCommand method.

I do currently have data in both tables and they both have a questionId in each table to join on. I've tried two different methods which both throw the same error.

The first method:

    var q = (from gameTable in db.Table<Model.GameSaved>()
                 join qTable in db.Table<Questions>() on gameTable.QuestionId equals qTable.QuestionId
                 select qTable
                ).First();

The second method:

    var q =
                (from question in db.Table<Model.GameSaved>()
                 select question
                ).Join(db.Table<Questions>(), 
                       game => game.QuestionId, 
                       questionObject => questionObject.QuestionId,
                       (game,questionObject) => questionObject)
                 .First();

I'm not exactly sure what I'm missing here but it has to be something simple and obvious.

like image 644
Liz Miner Avatar asked Apr 13 '13 21:04

Liz Miner


People also ask

How do I join two tables in SQLite?

Syntax. The syntax for the SQLite CROSS JOIN is: SELECT columns FROM table1 CROSS JOIN table2; NOTE: Unlike an INNER or OUTER join, a CROSS JOIN has no condition to join the 2 tables.

Does SQLite support natural join?

In SQLite, the NATURAL JOIN is such a join that performs the same task as an INNER or LEFT JOIN, in which the ON or USING clause refers to all columns that the tables to be joined have in common. A natural join joins two tables by their common column names.

Does SQLite support join?

SQLite supports different types of SQL Joins, like INNER JOIN, LEFT OUTER JOIN, and CROSS JOIN. Each type of JOIN is used for a different situation as we will see in this tutorial.

Which join is not supported in SQLite?

Unfortunately, SQLite does not support the RIGHT JOIN clause and also the FULL OUTER JOIN clause. However, you can easily emulate the FULL OUTER JOIN by using the LEFT JOIN clause.


1 Answers

You are not missing anything. Joins via linq are not supported by Sqlite-net at this time. You can work around this by providing your own SQL and using the Query method. Your first query from above would look something like:

var q = db.Query<Questions>(
    "select Q.* from Questions Q inner join GameSaved G"
    + " on Q.QuestionId = G.QuestionId"
).First();

If you are so inclined, the Query method also supports parameters. From the Sqlite-net readme:

db.Query<Val>(
    "select 'Price' as 'Money', 'Time' as 'Date' from Valuation where StockId = ?",
     stock.Id);
like image 196
chue x Avatar answered Oct 19 '22 14:10

chue x