How to refer existing database form another ViewController












0















in my registerpageViewController, I have the following code:



class registerpageViewController: UIViewController {

var database: Connection!

let usersTable = Table("users")
let id = Expression<Int>("id")
let fname = Expression<String>("fname")
let lname = Expression<String>("lname")
let email = Expression<String>("email")
let password = Expression<String>("password")

override func viewDidLoad() {
super.viewDidLoad()
do{
let documentDirectory = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask, appropriateFor: nil,create: true )
let fileUrl = documentDirectory.appendingPathComponent("users").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
self.database = database
}
catch{
print("error")
}
}


I want to use do some operation in another UIViewController over the database. so how can I refer it in my new view controller?










share|improve this question

























  • why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

    – Ratul Sharker
    Nov 24 '18 at 10:57











  • Not directly relevant, but try to name your classes in UpperCamelCase.

    – Damon
    Nov 24 '18 at 10:59











  • @RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 11:12











  • As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

    – Ratul Sharker
    Nov 24 '18 at 11:14






  • 1





    @RatulSharker bhai it works, thanks a lot .

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 13:35
















0















in my registerpageViewController, I have the following code:



class registerpageViewController: UIViewController {

var database: Connection!

let usersTable = Table("users")
let id = Expression<Int>("id")
let fname = Expression<String>("fname")
let lname = Expression<String>("lname")
let email = Expression<String>("email")
let password = Expression<String>("password")

override func viewDidLoad() {
super.viewDidLoad()
do{
let documentDirectory = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask, appropriateFor: nil,create: true )
let fileUrl = documentDirectory.appendingPathComponent("users").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
self.database = database
}
catch{
print("error")
}
}


I want to use do some operation in another UIViewController over the database. so how can I refer it in my new view controller?










share|improve this question

























  • why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

    – Ratul Sharker
    Nov 24 '18 at 10:57











  • Not directly relevant, but try to name your classes in UpperCamelCase.

    – Damon
    Nov 24 '18 at 10:59











  • @RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 11:12











  • As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

    – Ratul Sharker
    Nov 24 '18 at 11:14






  • 1





    @RatulSharker bhai it works, thanks a lot .

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 13:35














0












0








0








in my registerpageViewController, I have the following code:



class registerpageViewController: UIViewController {

var database: Connection!

let usersTable = Table("users")
let id = Expression<Int>("id")
let fname = Expression<String>("fname")
let lname = Expression<String>("lname")
let email = Expression<String>("email")
let password = Expression<String>("password")

override func viewDidLoad() {
super.viewDidLoad()
do{
let documentDirectory = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask, appropriateFor: nil,create: true )
let fileUrl = documentDirectory.appendingPathComponent("users").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
self.database = database
}
catch{
print("error")
}
}


I want to use do some operation in another UIViewController over the database. so how can I refer it in my new view controller?










share|improve this question
















in my registerpageViewController, I have the following code:



class registerpageViewController: UIViewController {

var database: Connection!

let usersTable = Table("users")
let id = Expression<Int>("id")
let fname = Expression<String>("fname")
let lname = Expression<String>("lname")
let email = Expression<String>("email")
let password = Expression<String>("password")

override func viewDidLoad() {
super.viewDidLoad()
do{
let documentDirectory = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask, appropriateFor: nil,create: true )
let fileUrl = documentDirectory.appendingPathComponent("users").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
self.database = database
}
catch{
print("error")
}
}


I want to use do some operation in another UIViewController over the database. so how can I refer it in my new view controller?







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 13:20









wvteijlingen

8,68612544




8,68612544










asked Nov 24 '18 at 10:48









Tareq Mahmud HridoyTareq Mahmud Hridoy

12




12













  • why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

    – Ratul Sharker
    Nov 24 '18 at 10:57











  • Not directly relevant, but try to name your classes in UpperCamelCase.

    – Damon
    Nov 24 '18 at 10:59











  • @RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 11:12











  • As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

    – Ratul Sharker
    Nov 24 '18 at 11:14






  • 1





    @RatulSharker bhai it works, thanks a lot .

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 13:35



















  • why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

    – Ratul Sharker
    Nov 24 '18 at 10:57











  • Not directly relevant, but try to name your classes in UpperCamelCase.

    – Damon
    Nov 24 '18 at 10:59











  • @RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 11:12











  • As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

    – Ratul Sharker
    Nov 24 '18 at 11:14






  • 1





    @RatulSharker bhai it works, thanks a lot .

    – Tareq Mahmud Hridoy
    Nov 24 '18 at 13:35

















why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

– Ratul Sharker
Nov 24 '18 at 10:57





why dont you use the same code over the another viewcontroller or create a singletone DBManager class who manage all the DB related stuffs ?

– Ratul Sharker
Nov 24 '18 at 10:57













Not directly relevant, but try to name your classes in UpperCamelCase.

– Damon
Nov 24 '18 at 10:59





Not directly relevant, but try to name your classes in UpperCamelCase.

– Damon
Nov 24 '18 at 10:59













@RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

– Tareq Mahmud Hridoy
Nov 24 '18 at 11:12





@RatulSharker if i use same code in new ViewController , will it access same DB? i don't know about singletone DBmanager

– Tareq Mahmud Hridoy
Nov 24 '18 at 11:12













As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

– Ratul Sharker
Nov 24 '18 at 11:14





As long as the db path is same it will access the same db. But it would be more managable if you want to introduce a singletone class for managing db related tasks.

– Ratul Sharker
Nov 24 '18 at 11:14




1




1





@RatulSharker bhai it works, thanks a lot .

– Tareq Mahmud Hridoy
Nov 24 '18 at 13:35





@RatulSharker bhai it works, thanks a lot .

– Tareq Mahmud Hridoy
Nov 24 '18 at 13:35












1 Answer
1






active

oldest

votes


















0














Use UserDefault to save your database in UserDefault and can access that database in the app anywhere.



Follow this link to check UserDefault: https://learnappmaking.com/userdefaults-swift-setting-getting-data-how-to/






share|improve this answer
























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Matthias
    Nov 24 '18 at 13:15











  • @Matthias Thank you for your suggestion !! Will follow your point.

    – iDev750
    Nov 24 '18 at 13:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457364%2fhow-to-refer-existing-database-form-another-viewcontroller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Use UserDefault to save your database in UserDefault and can access that database in the app anywhere.



Follow this link to check UserDefault: https://learnappmaking.com/userdefaults-swift-setting-getting-data-how-to/






share|improve this answer
























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Matthias
    Nov 24 '18 at 13:15











  • @Matthias Thank you for your suggestion !! Will follow your point.

    – iDev750
    Nov 24 '18 at 13:25
















0














Use UserDefault to save your database in UserDefault and can access that database in the app anywhere.



Follow this link to check UserDefault: https://learnappmaking.com/userdefaults-swift-setting-getting-data-how-to/






share|improve this answer
























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Matthias
    Nov 24 '18 at 13:15











  • @Matthias Thank you for your suggestion !! Will follow your point.

    – iDev750
    Nov 24 '18 at 13:25














0












0








0







Use UserDefault to save your database in UserDefault and can access that database in the app anywhere.



Follow this link to check UserDefault: https://learnappmaking.com/userdefaults-swift-setting-getting-data-how-to/






share|improve this answer













Use UserDefault to save your database in UserDefault and can access that database in the app anywhere.



Follow this link to check UserDefault: https://learnappmaking.com/userdefaults-swift-setting-getting-data-how-to/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 11:31









iDev750iDev750

5961316




5961316













  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Matthias
    Nov 24 '18 at 13:15











  • @Matthias Thank you for your suggestion !! Will follow your point.

    – iDev750
    Nov 24 '18 at 13:25



















  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Matthias
    Nov 24 '18 at 13:15











  • @Matthias Thank you for your suggestion !! Will follow your point.

    – iDev750
    Nov 24 '18 at 13:25

















While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

– Matthias
Nov 24 '18 at 13:15





While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

– Matthias
Nov 24 '18 at 13:15













@Matthias Thank you for your suggestion !! Will follow your point.

– iDev750
Nov 24 '18 at 13:25





@Matthias Thank you for your suggestion !! Will follow your point.

– iDev750
Nov 24 '18 at 13:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457364%2fhow-to-refer-existing-database-form-another-viewcontroller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin