stephencelis / SQLite.swift

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

how to use unique? #992

Closed mstfabbstbr closed 3 years ago

mstfabbstbr commented 4 years ago

i don't want to let user, insert a duplicate value in table.. how to use unique?

zachwick commented 4 years ago

@mstfabbstbr The documentation for the column function shows how to use the unique parameter to create a constraint on the column.

If you need to create a unique constraint across multiple columns, then you'll want to look at the documentation for table constraints.

mstfabbstbr commented 4 years ago

Hey, thanks a lot for this. That helps me a lot.. thank you.

On Tue, Apr 7, 2020 at 5:52 AM zach wick notifications@github.com wrote:

@mstfabbstbr https://github.com/mstfabbstbr The documentation https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#column-constraints for the column function shows how to use the unique parameter to create a constraint on the column.

If you need to create a unique constraint across multiple columns, then you'll want to look at the documentation https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#table-constraints for table constraints.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stephencelis/SQLite.swift/issues/992#issuecomment-610120347, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM44KCVQFZMIRXUBQXEGMNDRLJ547ANCNFSM4LVYFNNQ .

mstfabbstbr commented 4 years ago

hey, thanks again for your email, i’ve one more question. when you create a database, it’s create in whole app, or just in that viewcontroller?

because i wanna use one of table i created in another controller, it’s possible?

On Farvardin 19, 1399 AP, at 05:52, zach wick notifications@github.com wrote:

@mstfabbstbr https://github.com/mstfabbstbr The documentation https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#column-constraints for the column function shows how to use the unique parameter to create a constraint on the column.

If you need to create a unique constraint across multiple columns, then you'll want to look at the documentation https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#table-constraints for table constraints.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stephencelis/SQLite.swift/issues/992#issuecomment-610120347, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM44KCVQFZMIRXUBQXEGMNDRLJ547ANCNFSM4LVYFNNQ.

TenMaKo commented 4 years ago

As stated in documentation, when you do let db = try Connection("\(path)/db.sqlite3") for the 1st time, it creates a database file at path "(path)/db.sqlite3" so it's not a matter of controller, it's about accessing this file. So yes you can access your database from any controller, you just need to do the same thing again in order to access the same file.

But maybe you should document yourself on how to implement a data access layer before jumping into code, it will help you a lot. Take your time to really understand this and take a look at the proposed example at the end of the section.