-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Numeric Type Column #331
Comments
Thanks @doop24. Any chance you can put this into a complete snippet that we can run in a Playground? It will allow us to get to this faster and give us both something concrete that we can point to (though your snippets are an excellent start, thank you!). E.g. something like this (but demonstrating your problem obviously): import SQLite
let db = try! Connection()
db.trace { print($0) }
let emails = Table("emails")
let to = Expression<String>("to")
let subject = Expression<String?>("subject")
try! db.run(emails.create(ifNotExists: true) {t in t.column(to); t.column(subject)} )
try! db.run(emails.insert(to <- "[email protected]", subject <- "Hello, world!" ))
try! db.run(emails.insert(to <- "[email protected]", subject <- "RE: Hello, world!" ))
try! db.run(emails.insert(to <- "[email protected]", subject <- "Wookies" ))
for user in try db.prepare(emails) {
print("id: \(user[to])")
} |
Thanks, mikemee. My sample db file is here. This db has one table. (name: mytbl1) source: let db2 = try! Connection(dbPath)
db2.trace { print($0) }
let tbl2 = Table("mytbl1")
let x2 = Expression<Double>("x")
for row in try! db2.prepare(tbl2) {
print("x: \(row[x2])”)
}
error: But update x value 2000 to 2000.1, This code will succeed without error. |
Great, thanks! I'll try and find some time to put this into a self-contained Playground and then set up a test, and then work with Stephen to fix it. |
Thanks, mikemee. |
Here's a standalone repro case. @stephencelis I haven't dug into the code enough yet to quickly debug this... Anyone want to show their Swift skills and provide a PR? (Preferably include a test case, but I'll add that in a pinch). let db2 = try! Connection()
db2.trace { print($0) }
let table = "mytbl1"
let column = "x"
try! db2.run("create table \(table) (\(column) NUMERIC) ")
try! db2.run("insert into \(table) (\(column)) values (2000.1)")
let tbl2 = Table(table)
let x2 = Expression<Double>(column)
// works
for row in try! db2.prepare(tbl2) {
print("x: \(row[x2])")
}
try! db2.run("delete from \(table)")
try! db2.run("insert into \(table) (\(column)) values (2000)")
// fails with value 2000 instead of 2000.1
for row in try! db2.prepare(tbl2) {
print("x: \(row[x2])")
} This produces the output:
and stops on the public func get<V: Value>(column: Expression<V>) -> V {
return get(Expression<V?>(column))!
} |
It is not just with numeric data. I am also facing the same issue even with string data columns when trying to extract the data from a row array where column value is nil. i am new to swift. Is there a way to check for "nil" in the column value? |
In this example, 2000.1 is a Later, in
In this case, if the type is You can get similarly bad behavior by declaring the column type to be I think the right thing to do is attempt to transform the data type in |
It's not done, but this is what I'm thinking. I really don't like how verbose it is, but I can't figure out a tighter way to make the compiler happy. |
This is a problem with using the typed helpers only some of the time. If the table had been created using @hiltonc I wonder if there's a way to tighten that logic up, but as you can probably tell looking through the code base as is, sometimes a lot of repetition is needed :( I also wonder if the main issue here is bad error messaging. If we avoid the |
I guess it depends on what use cases SQLite.swift should support. I agree, if you use SQLite.swift all the way, you won't run into trouble. But sometimes you want to use SQLite.swift to work with a database that was created somewhere else. For example, I use it in an app which consumes SQLite databases that are generated by code written in a different language altogether. Definitely your call. I haven't run into this particular problem my self, I just want to help out and this problem looked interesting. |
If you have the opportunity to alter the schema of the database, you can work around this limitation by turning the NUMERIC column into a REAL column. SQLite documents how to perform complex schema changes that go beyond the simple Basically, you temporarily disable foreign keys, and recreate your whole table in a safe transaction: CREATE TABLE new_test (x: REAL);
INSERT INTO new_test SELECT x FROM test;
DROP TABLE test;
ALTER TABLE new_test RENAME TO test; If the initial INSERT INTO new_test SELECT CAST(x AS REAL) FROM test; |
Any update on this? Is Decimals supported in SQLite.swift? |
update my table.
update is success.
but select updated table is failure
error occurred.
please help!
The text was updated successfully, but these errors were encountered: