Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import SQLite3
- class DBHelper {
- init() {
- db = openDataBase()
- createTable()
- }
- let dbPath: String = "myDb.sqlite"
- var db: OpaquePointer?
- func openDataBase() -> OpaquePointer? {
- let fileUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
- .appendingPathComponent(dbPath)
- var db: OpaquePointer? = nil
- if sqlite3_open(fileUrl.path, &db) != SQLITE_OK {
- print("Error opening data base!")
- return nil
- } else {
- // print("Successfully opened database at :\(dbPath)")
- return db
- }
- }
- func createTable() {
- let createTableString = "CREATE TABLE IF NOT EXISTS image(id INTEGER PRIMARY KEY AUTOINCREMENT,imageIdentifier TEXT,albumid INTEGER,imageName TEXT,imageDate TEXT,imageDescription TEXT,isImageFav INTEGER,totalImgViews INTEGER);"
- let createTableStringTwo = "CREATE TABLE IF NOT EXISTS album(id INTEGER PRIMARY KEY AUTOINCREMENT,albumName TEXT);"
- var createTableStatement: OpaquePointer? = nil
- if sqlite3_prepare(db, createTableString, -1, &createTableStatement, nil) == SQLITE_OK {
- if sqlite3_step(createTableStatement) == SQLITE_DONE {
- // print("Image Table Created")
- } else {
- print("Image table could not be created")
- }
- } else {
- print("Create Image Table statement could not be prepared!")
- }
- if sqlite3_prepare(db, createTableStringTwo, -1, &createTableStatement, nil) == SQLITE_OK {
- if sqlite3_step(createTableStatement) == SQLITE_DONE {
- // print("Album Table Created")
- } else {
- print("album table could not be created")
- }
- } else {
- print("Create Album Table statement could not be prepared!")
- }
- sqlite3_finalize(createTableStatement)
- }
- func insertAlbumData(albumName: String) {
- let albums = readAlbum()
- var isEmpty = false
- if albums.isEmpty {
- isEmpty = true
- }
- for alb in albums
- {
- if alb.albumName == albumName
- {
- return
- }
- }
- let insertStatementString = "INSERT INTO album (id, albumName) VALUES (?, ?);"
- var insertStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
- if isEmpty {
- sqlite3_bind_int(insertStatement, 1, 1)
- }
- sqlite3_bind_text(insertStatement, 2, (albumName as NSString).utf8String, -1, nil)
- if sqlite3_step(insertStatement) == SQLITE_DONE {
- print("Successfully inserted row.")
- } else {
- print("Could not insert row.")
- }
- } else {
- print("INSERT statement could not be prepared.")
- }
- sqlite3_finalize(insertStatement)
- }
- func insertIMGdata(imageIdentifier: String, albumId: Int, imageName: String, imageDate: String, imageDescription: String, isImageFav: Int, totalImgViews: Int) {
- let imgs = readImg()
- var isEmpty = false
- if imgs.isEmpty {
- isEmpty = true
- }
- for img in imgs {
- if img.imageIdentifier == imageIdentifier && img.albumid == albumId && img.imageDate == imageDate && img.imageName == imageName
- {
- return
- }
- }
- let insertStatementString = "INSERT INTO image (id, imageIdentifier, albumid, imageName, imageDate, imageDescription, isImageFav, totalImgViews) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"
- var insertStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
- if isEmpty {
- sqlite3_bind_int(insertStatement, 1, 1)
- }
- sqlite3_bind_text(insertStatement, 2, (imageIdentifier as NSString).utf8String, -1, nil)
- sqlite3_bind_int(insertStatement, 3, Int32(albumId))
- sqlite3_bind_text(insertStatement, 4, (imageName as NSString).utf8String, -1, nil)
- sqlite3_bind_text(insertStatement, 5, (imageDate as NSString).utf8String, -1, nil)
- sqlite3_bind_text(insertStatement, 6, (imageDescription as NSString).utf8String, -1, nil)
- sqlite3_bind_int(insertStatement, 7, Int32(isImageFav))
- sqlite3_bind_int(insertStatement, 8, Int32(totalImgViews))
- if sqlite3_step(insertStatement) == SQLITE_DONE {
- print("Successfully inserted row.")
- } else {
- print("Could not insert row.")
- }
- } else {
- print("INSERT statement could not be prepared.")
- }
- sqlite3_finalize(insertStatement)
- }
- // Read all album data
- func readAlbum() -> [AlbumModel] {
- let queryStatementString = "SELECT * FROM album;"
- var queryStatement: OpaquePointer? = nil
- var albums: [AlbumModel] = []
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let id = sqlite3_column_int(queryStatement, 0)
- let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- albums.append(AlbumModel(id: Int(id), albumName: albumName))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all album data in Reverse order
- func readAlbumReverse() -> [AlbumModel] {
- let queryStatementString = "SELECT * FROM album ORDER BY id DESC;"
- var queryStatement: OpaquePointer? = nil
- var albums: [AlbumModel] = []
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let id = sqlite3_column_int(queryStatement, 0)
- let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- albums.append(AlbumModel(id: Int(id), albumName: albumName))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read perticullar album data according to album's id
- func readAlbumbyId(id: Int) -> [AlbumModel] {
- let queryStatementString = "SELECT * FROM album WHERE id = \(id);"
- var queryStatement: OpaquePointer? = nil
- var albums: [AlbumModel] = []
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- if sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- albums.append(AlbumModel(id: Int(ida), albumName: albumName))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read perticullar image data according to image's id
- func readImgbyId(id: Int) -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image WHERE id = \(id);"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- if sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all images data according to image is favourite
- func readImgbyFav() -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image WHERE isImageFav = \(0);"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all image data according to image's image identifier in descending order
- func readImgbyDesc() -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image GROUP BY imageIdentifier;"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read perticullar image data according to image's album id and image identifier
- func readImgbyAlbIdIdentifier(Albid: Int, identifier: String) -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image WHERE albumid = \(Albid) AND imageIdentifier = \(identifier);"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- if sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all images data according to image'd album id
- func readImgbyAlbId(Albid: Int) -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image WHERE albumid = \(Albid);"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all image data according to image's image identifier in descending order by image's popularity
- func readImgbyPopularity() -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image GROUP BY imageIdentifier ORDER BY totalImgViews DESC;"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = [ImageModel]()
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let ida = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- // Read all image data
- func readImg() -> [ImageModel] {
- let queryStatementString = "SELECT * FROM image;"
- var queryStatement: OpaquePointer? = nil
- var albums: [ImageModel] = []
- if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
- while sqlite3_step(queryStatement) == SQLITE_ROW {
- let id = sqlite3_column_int(queryStatement, 0)
- let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
- let albumId = sqlite3_column_int(queryStatement, 2)
- let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
- let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
- let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
- let isImageFav = sqlite3_column_int(queryStatement, 6)
- let totalImgViews = sqlite3_column_int(queryStatement, 7)
- albums.append(ImageModel(id: Int(id), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
- }
- } else {
- print("SELECT statement could not be prepared")
- }
- sqlite3_finalize(queryStatement)
- return albums
- }
- func deleteAlbumByID(id:Int) {
- let deleteStatementStirng = "DELETE FROM album WHERE id = \(id);"
- var deleteStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
- sqlite3_bind_int(deleteStatement, 1, Int32(id))
- if sqlite3_step(deleteStatement) == SQLITE_DONE {
- print("Successfully deleted row.")
- } else {
- print("Could not delete row.")
- }
- } else {
- print("DELETE statement could not be prepared")
- }
- sqlite3_finalize(deleteStatement)
- }
- func deleteImgByID(id:Int) {
- let deleteStatementStirng = "DELETE FROM image WHERE id = ?;"
- var deleteStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
- sqlite3_bind_int(deleteStatement, 1, Int32(id))
- if sqlite3_step(deleteStatement) == SQLITE_DONE {
- print("Successfully deleted row.")
- } else {
- print("Could not delete row.")
- }
- } else {
- print("DELETE statement could not be prepared")
- }
- sqlite3_finalize(deleteStatement)
- }
- func deleteImgByIdandAlb(id:Int, albumId: Int) {
- let deleteStatementStirng = "DELETE FROM image WHERE (id = \(id) AND albumid = \(albumId));"
- var deleteStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
- sqlite3_bind_int(deleteStatement, 1, Int32(id))
- if sqlite3_step(deleteStatement) == SQLITE_DONE {
- print("Successfully deleted row.")
- } else {
- print("Could not delete row.")
- }
- } else {
- print("DELETE statement could not be prepared")
- }
- sqlite3_finalize(deleteStatement)
- }
- func updateAlbum( id: Int, albumName: String) {
- let updateStatementString = "UPDATE album SET albumName = '\(albumName)' WHERE id = '\(id)';"
- var updateStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, updateStatementString, -1, &updateStatement, nil) ==
- SQLITE_OK {
- if sqlite3_step(updateStatement) == SQLITE_DONE {
- print("\nSuccessfully updated row.")
- } else {
- print("\nCould not updateImg row.")
- }
- } else {
- print("\nUPDATE statement is not prepared")
- }
- sqlite3_finalize(updateStatement)
- }
- func updateImg(id: Int, imageIdentifier: String, albumId: Int, imageName: String, imageDate: String, imageDescription: String, isImageFav: Int, totalImgViews: Int) {
- let updateStatementString = "UPDATE image SET imageIdentifier = '\(imageIdentifier)', albumid = \(albumId), imageName = '\(imageName)', imageDate = '\(imageDate)', imageDescription = '\(imageDescription)', isImageFav = \(isImageFav), totalImgViews = \(totalImgViews) WHERE id = \(id);"
- var updateStatement: OpaquePointer? = nil
- if sqlite3_prepare_v2(db, updateStatementString, -1, &updateStatement, nil) ==
- SQLITE_OK {
- if sqlite3_step(updateStatement) == SQLITE_DONE {
- print("\nSuccessfully updated row.")
- } else {
- print("\nCould not updateImg row.")
- }
- } else {
- print("\nUPDATE statement is not prepared")
- }
- sqlite3_finalize(updateStatement)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement