stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.67k stars 1.56k forks source link

Converting an INNER JOIN query from SQLite to SQLite.Swift #967

Closed Quailcreek closed 3 years ago

Quailcreek commented 4 years ago

I need some help. I started this app using LiveCode and SQLite and I've since switched over to Xcode and SQLit.Swift. I'm using the existing database. I'm having a little trouble converting an inner join of some of the tables.

Right now I'm dealing with 3 tables: CrossRef, My_Data and My_Routines

My_Data contains DataID and Data_Name and My_Routines contain RoutineID and Routine_Name.

The CrossRef table was created like this. It only has 2 fields, DataID and RoutineID

CREATE TABLE "CrossRef" ("DataID INTEGER NOT NULL, "RoutineID" INTEGER NOT NULL, FOREIGN KEY("RoutineID") REFERENCES "My_Routines", PRIMARY KEY("DataID","RoutineID"), FOREIGN KEY("DataID") REFERENCES "My_Data"

I'm trying to re-wright the following line so I can use it with SQLite.Swift. I've included the Expressions for the table and the fields.

SELECT My_Data.Data_Name FROM CrossRef INNER JOIN My_Data USING (DataID) WHERE CrossRef.RoutineID = gRoutineID

let crossRefTable = Table("CrossRef")
let crossRefRoutineID = Expression<Int64>("RoutineID")
let crossRefDataID = Expression<Int64>("DataID")

let theDataTable = Table("My_Data")
let dataID = Expression<Int64>("DataID")
let data_Name = Expression<String>("Data_Name")

let theRoutineTable = Table("My_Routines")
let routineID = Expression<Int64>("RoutineID")`

I've read through the SQLite.Swift documentation online but I'm afraid it's not helping me much.

Any help would be appreciated.

Thanks, Tom

nathanfallet commented 3 years ago

Please ask on StackOverflow for this kind of questions (we are tracking bugs on GitHub)