-3

Are there any good sqlite.swift example projects that I can work through to learn sqlite.swift? I'm knew to swift and looking for a good wrapper for database access and would like to find a good tutorial to help with some hands on learning.

thanks

lsg13
  • 1
  • 1

1 Answers1

0

You can see the below example to use sqlite.swift or you can also have a look at github page of sqlite.swift

// Table name
let trending = Table("trending") 

// Fields of table
let id = Expression<String>("id")
let title = Expression<String>("title")
let desc = Expression<String>("description”)
let imageUrl = Expression<String>("imageUrl”)

let db: Connection

init(){
    //Creating connection to database
    let dbPath = path_to_db
    db = try! Connection(dbPath)
    
}

func createTable(){
      try! db.run(trending.create { t in
                    t.column(id, primaryKey: true)
                    t.column(title)
                    t.column(desc)
                    t.column(imageUrl)
             })
}


func insertDate() {
    
   let insert = trending.insert(title <- “title”, desc <- “description” ,  imageUrl <- “https://image.com/image.jpg”)
   try! db.run(insert)
    
        
}

func fetchData(){
       for item in try! db.prepare(trending) {
        print("id: \(item[id]), title: \(item[ title])”)
    }
 }

}

MrinmoyMk
  • 551
  • 7
  • 21