Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression fails when Field Contains a Number #46

Closed
ericcgu opened this issue Jan 20, 2015 · 3 comments
Closed

Expression fails when Field Contains a Number #46

ericcgu opened this issue Jan 20, 2015 · 3 comments
Labels

Comments

@ericcgu
Copy link

ericcgu commented Jan 20, 2015

After numerous testing, I believe that Query fails when Column heading contains a Number:

Here is my repo: If anyone can shed some light, I would appreciate it:
https://github.com/ericcgu/RWEGSeed/blob/NumericalColumnNames/Seed/RWEGFrequencyByLocationTableViewController.swift

    //Success
    let firstValue = Expression<String>("ON")

    //Failure
    let firstValue = Expression<String>("ON_1950")

    //Failure
    let secondValue = Expression<String>("19500301_OFF")
    let thirdValue = Expression<String>("19500301_TOTAL")

As a workaround, I tried using a literal to avoid expressions but this did not work:

 //Success
 var query = "SELECT * FROM STOCKS_CORN_LOCATION"
    let statement = db.prepare(query)
    while ((statement.next()) != nil){
      let row = statement.columnNames
      println(row)
    }

 //Failure
 var query = "SELECT 2011301_ON FROM STOCKS_CORN_LOCATION"
    let statement = db.prepare(query)
    while ((statement.next()) != nil){
      let row = statement.columnNames
      println(row)
    }
@ericcgu ericcgu changed the title Query Fails when Field leads with a Number Query Fails when Field Contains a Number Jan 20, 2015
@ericcgu ericcgu changed the title Query Fails when Field Contains a Number Expression fails when Field Contains a Number Jan 20, 2015
@stephencelis
Copy link
Owner

When was the last time you updated your SQLite.swift submodule? I can get the following to run just fine on master:

let db = Database()
db.trace(println)
let users = db["users"]
let field = Expression<String>("19500301_OFF")

db.create(table: users) { $0.column(field) }
users.insert(field <- "Hello")!
for user in users.select(field) {
    println(user[field])
}

Console output:

CREATE TABLE "users" ("19500301_OFF" TEXT NOT NULL)
INSERT INTO "users" ("19500301_OFF") VALUES ('Hello')
SELECT "19500301_OFF" FROM "users"
Hello

@stephencelis
Copy link
Owner

Ah, you know what, I think your issue is in your raw SQL syntax:

SELECT 2011301_ON FROM STOCKS_CORN_LOCATION

The above is invalid. You must quote identifiers (generally using ") that contain spaces or begin with numbers. SQLite.swift automatically quotes expressions for you, so as long as you're using the type-safe interface, things should just work. If you're writing raw SQL, however, you'll need to be careful to quote things yourself wherever necessary.

@ericcgu
Copy link
Author

ericcgu commented Jan 21, 2015

Thank you Stephen. I appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants