Advertisement
srk72

DB Helper SQLite

May 20th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 23.47 KB | None | 0 0
  1. import Foundation
  2. import SQLite3
  3.  
  4. class DBHelper {
  5.    
  6.     init() {
  7.        
  8.         db = openDataBase()
  9.         createTable()
  10.     }
  11.    
  12.     let dbPath: String = "myDb.sqlite"
  13.     var db: OpaquePointer?
  14.    
  15.    
  16.     func openDataBase() -> OpaquePointer? {
  17.        
  18.         let fileUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
  19.             .appendingPathComponent(dbPath)
  20.        
  21.         var db: OpaquePointer? = nil
  22.        
  23.         if sqlite3_open(fileUrl.path, &db) != SQLITE_OK {
  24.             print("Error opening data base!")
  25.             return nil
  26.         } else {
  27.            // print("Successfully opened database at :\(dbPath)")
  28.             return db
  29.         }
  30.        
  31.     }
  32.    
  33.    
  34.     func createTable() {
  35.        
  36.         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);"
  37.        
  38.         let createTableStringTwo = "CREATE TABLE IF NOT EXISTS album(id INTEGER PRIMARY KEY AUTOINCREMENT,albumName TEXT);"
  39.        
  40.         var createTableStatement: OpaquePointer? = nil
  41.        
  42.         if sqlite3_prepare(db, createTableString, -1, &createTableStatement, nil) == SQLITE_OK {
  43.            
  44.             if sqlite3_step(createTableStatement) == SQLITE_DONE {
  45.                // print("Image Table Created")
  46.             } else {
  47.                 print("Image table could not be created")
  48.             }
  49.            
  50.         } else {
  51.             print("Create Image Table statement could not be prepared!")
  52.         }
  53.        
  54.         if sqlite3_prepare(db, createTableStringTwo, -1, &createTableStatement, nil) == SQLITE_OK {
  55.            
  56.             if sqlite3_step(createTableStatement) == SQLITE_DONE {
  57.               //  print("Album Table Created")
  58.             } else {
  59.                 print("album table could not be created")
  60.             }
  61.            
  62.         } else {
  63.             print("Create Album Table statement could not be prepared!")
  64.         }
  65.        
  66.        
  67.         sqlite3_finalize(createTableStatement)
  68.     }
  69.    
  70.    
  71.    
  72.     func insertAlbumData(albumName: String) {
  73.         let albums = readAlbum()
  74.         var isEmpty = false
  75.        
  76.         if albums.isEmpty {
  77.             isEmpty = true
  78.         }
  79.        
  80.         for alb in albums
  81.             {
  82.             if alb.albumName == albumName
  83.                 {
  84.                     return
  85.                 }
  86.             }
  87.        
  88.         let insertStatementString = "INSERT INTO album (id, albumName) VALUES (?, ?);"
  89.         var insertStatement: OpaquePointer? = nil
  90.        
  91.         if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
  92.             if isEmpty {
  93.                 sqlite3_bind_int(insertStatement, 1, 1)
  94.             }
  95.            
  96.             sqlite3_bind_text(insertStatement, 2, (albumName as NSString).utf8String, -1, nil)
  97.                    
  98.                 if sqlite3_step(insertStatement) == SQLITE_DONE {
  99.                     print("Successfully inserted row.")
  100.                 } else {
  101.                     print("Could not insert row.")
  102.                 }
  103.            
  104.             } else {
  105.                 print("INSERT statement could not be prepared.")
  106.             }
  107.         sqlite3_finalize(insertStatement)
  108.        
  109.     }
  110.    
  111.    
  112.    
  113.     func insertIMGdata(imageIdentifier: String, albumId: Int, imageName: String, imageDate: String, imageDescription: String, isImageFav: Int, totalImgViews: Int) {
  114.        
  115.         let imgs = readImg()
  116.         var isEmpty = false
  117.        
  118.         if imgs.isEmpty {
  119.             isEmpty = true
  120.         }
  121.        
  122.         for img in imgs {
  123.             if img.imageIdentifier == imageIdentifier && img.albumid == albumId && img.imageDate == imageDate && img.imageName == imageName
  124.                 {
  125.                     return
  126.                 }
  127.         }
  128.        
  129.         let insertStatementString = "INSERT INTO image (id, imageIdentifier, albumid, imageName, imageDate, imageDescription, isImageFav, totalImgViews) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"
  130.         var insertStatement: OpaquePointer? = nil
  131.        
  132.         if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
  133.            
  134.             if isEmpty {
  135.                 sqlite3_bind_int(insertStatement, 1, 1)
  136.             }
  137.            
  138.             sqlite3_bind_text(insertStatement, 2, (imageIdentifier as NSString).utf8String, -1, nil)
  139.             sqlite3_bind_int(insertStatement, 3, Int32(albumId))
  140.             sqlite3_bind_text(insertStatement, 4, (imageName as NSString).utf8String, -1, nil)
  141.             sqlite3_bind_text(insertStatement, 5, (imageDate as NSString).utf8String, -1, nil)
  142.             sqlite3_bind_text(insertStatement, 6, (imageDescription as NSString).utf8String, -1, nil)
  143.             sqlite3_bind_int(insertStatement, 7, Int32(isImageFav))
  144.             sqlite3_bind_int(insertStatement, 8, Int32(totalImgViews))
  145.            
  146.                 if sqlite3_step(insertStatement) == SQLITE_DONE {
  147.                     print("Successfully inserted row.")
  148.                 } else {
  149.                     print("Could not insert row.")
  150.                 }
  151.            
  152.             } else {
  153.                 print("INSERT statement could not be prepared.")
  154.             }
  155.         sqlite3_finalize(insertStatement)
  156.        
  157.     }
  158.    
  159.     // Read all album data
  160.     func readAlbum() -> [AlbumModel] {
  161.         let queryStatementString = "SELECT * FROM album;"
  162.         var queryStatement: OpaquePointer? = nil
  163.         var albums: [AlbumModel] = []
  164.  
  165.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  166.            
  167.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  168.                
  169.                 let id = sqlite3_column_int(queryStatement, 0)
  170.                 let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  171.                
  172.                 albums.append(AlbumModel(id: Int(id), albumName: albumName))
  173.             }
  174.         } else {
  175.             print("SELECT statement could not be prepared")
  176.         }
  177.        
  178.         sqlite3_finalize(queryStatement)
  179.         return albums
  180.        
  181.     }
  182.    
  183.     // Read all album data in Reverse order
  184.     func readAlbumReverse() -> [AlbumModel] {
  185.         let queryStatementString = "SELECT * FROM album ORDER BY id DESC;"
  186.         var queryStatement: OpaquePointer? = nil
  187.         var albums: [AlbumModel] = []
  188.  
  189.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  190.            
  191.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  192.                
  193.                 let id = sqlite3_column_int(queryStatement, 0)
  194.                 let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  195.                
  196.                 albums.append(AlbumModel(id: Int(id), albumName: albumName))
  197.             }
  198.         } else {
  199.             print("SELECT statement could not be prepared")
  200.         }
  201.        
  202.         sqlite3_finalize(queryStatement)
  203.         return albums
  204.        
  205.     }
  206.    
  207.     // Read perticullar album data according to album's id
  208.     func readAlbumbyId(id: Int) -> [AlbumModel] {
  209.         let queryStatementString = "SELECT * FROM album WHERE id = \(id);"
  210.         var queryStatement: OpaquePointer? = nil
  211.         var albums: [AlbumModel] = []
  212.  
  213.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  214.            
  215.             if sqlite3_step(queryStatement) == SQLITE_ROW {
  216.                
  217.                 let ida = sqlite3_column_int(queryStatement, 0)
  218.                 let albumName = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  219.                
  220.                 albums.append(AlbumModel(id: Int(ida), albumName: albumName))
  221.             }
  222.         } else {
  223.             print("SELECT statement could not be prepared")
  224.         }
  225.         sqlite3_finalize(queryStatement)
  226.         return albums
  227.     }
  228.    
  229.     // Read perticullar image data according to image's id
  230.     func readImgbyId(id: Int) -> [ImageModel] {
  231.         let queryStatementString = "SELECT * FROM image WHERE id = \(id);"
  232.         var queryStatement: OpaquePointer? = nil
  233.         var albums: [ImageModel] = [ImageModel]()
  234.  
  235.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  236.            
  237.             if sqlite3_step(queryStatement) == SQLITE_ROW {
  238.                
  239.                 let ida = sqlite3_column_int(queryStatement, 0)
  240.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  241.                 let albumId = sqlite3_column_int(queryStatement, 2)
  242.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  243.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  244.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  245.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  246.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  247.                
  248.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  249.             }
  250.         } else {
  251.             print("SELECT statement could not be prepared")
  252.         }
  253.         sqlite3_finalize(queryStatement)
  254.         return albums
  255.     }
  256.    
  257.     // Read all images data according to image is favourite
  258.     func readImgbyFav() -> [ImageModel] {
  259.         let queryStatementString = "SELECT * FROM image WHERE isImageFav = \(0);"
  260.         var queryStatement: OpaquePointer? = nil
  261.         var albums: [ImageModel] = [ImageModel]()
  262.  
  263.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  264.            
  265.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  266.                
  267.                 let ida = sqlite3_column_int(queryStatement, 0)
  268.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  269.                 let albumId = sqlite3_column_int(queryStatement, 2)
  270.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  271.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  272.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  273.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  274.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  275.                
  276.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  277.             }
  278.         } else {
  279.             print("SELECT statement could not be prepared")
  280.         }
  281.         sqlite3_finalize(queryStatement)
  282.         return albums
  283.     }
  284.    
  285.     // Read all image data according to image's image identifier in descending order
  286.     func readImgbyDesc() -> [ImageModel] {
  287.        
  288.         let queryStatementString = "SELECT * FROM image GROUP BY imageIdentifier;"
  289.         var queryStatement: OpaquePointer? = nil
  290.         var albums: [ImageModel] = [ImageModel]()
  291.  
  292.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  293.            
  294.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  295.                
  296.                 let ida = sqlite3_column_int(queryStatement, 0)
  297.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  298.                 let albumId = sqlite3_column_int(queryStatement, 2)
  299.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  300.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  301.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  302.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  303.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  304.                
  305.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  306.             }
  307.         } else {
  308.             print("SELECT statement could not be prepared")
  309.         }
  310.         sqlite3_finalize(queryStatement)
  311.         return albums
  312.     }
  313.    
  314.     // Read perticullar image data according to image's album id and image identifier
  315.     func readImgbyAlbIdIdentifier(Albid: Int, identifier: String) -> [ImageModel] {
  316.         let queryStatementString = "SELECT * FROM image WHERE albumid = \(Albid) AND imageIdentifier = \(identifier);"
  317.         var queryStatement: OpaquePointer? = nil
  318.         var albums: [ImageModel] = [ImageModel]()
  319.  
  320.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  321.            
  322.             if sqlite3_step(queryStatement) == SQLITE_ROW {
  323.                
  324.                 let ida = sqlite3_column_int(queryStatement, 0)
  325.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  326.                 let albumId = sqlite3_column_int(queryStatement, 2)
  327.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  328.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  329.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  330.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  331.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  332.                
  333.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  334.             }
  335.         } else {
  336.             print("SELECT statement could not be prepared")
  337.         }
  338.         sqlite3_finalize(queryStatement)
  339.         return albums
  340.     }
  341.    
  342.     // Read all images data according to image'd album id
  343.     func readImgbyAlbId(Albid: Int) -> [ImageModel] {
  344.         let queryStatementString = "SELECT * FROM image WHERE albumid = \(Albid);"
  345.         var queryStatement: OpaquePointer? = nil
  346.         var albums: [ImageModel] = [ImageModel]()
  347.  
  348.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  349.            
  350.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  351.                
  352.                 let ida = sqlite3_column_int(queryStatement, 0)
  353.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  354.                 let albumId = sqlite3_column_int(queryStatement, 2)
  355.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  356.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  357.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  358.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  359.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  360.                
  361.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  362.             }
  363.         } else {
  364.             print("SELECT statement could not be prepared")
  365.         }
  366.         sqlite3_finalize(queryStatement)
  367.         return albums
  368.     }
  369.    
  370.     // Read all image data according to image's image identifier in descending order by image's popularity
  371.     func readImgbyPopularity() -> [ImageModel] {
  372.         let queryStatementString = "SELECT * FROM image GROUP BY imageIdentifier ORDER BY totalImgViews DESC;"
  373.         var queryStatement: OpaquePointer? = nil
  374.         var albums: [ImageModel] = [ImageModel]()
  375.  
  376.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  377.            
  378.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  379.                
  380.                 let ida = sqlite3_column_int(queryStatement, 0)
  381.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  382.                 let albumId = sqlite3_column_int(queryStatement, 2)
  383.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  384.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  385.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  386.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  387.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  388.                
  389.                 albums.append(ImageModel(id: Int(ida), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  390.             }
  391.         } else {
  392.             print("SELECT statement could not be prepared")
  393.         }
  394.         sqlite3_finalize(queryStatement)
  395.         return albums
  396.     }
  397.    
  398.     // Read all image data
  399.     func readImg() -> [ImageModel] {
  400.         let queryStatementString = "SELECT * FROM image;"
  401.         var queryStatement: OpaquePointer? = nil
  402.         var albums: [ImageModel] = []
  403.  
  404.         if sqlite3_prepare(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
  405.            
  406.             while sqlite3_step(queryStatement) == SQLITE_ROW {
  407.                
  408.                 let id = sqlite3_column_int(queryStatement, 0)
  409.                 let imageIdentifier = String(describing: String(cString: sqlite3_column_text(queryStatement, 1)))
  410.                 let albumId = sqlite3_column_int(queryStatement, 2)
  411.                 let imageName = String(describing: String(cString: sqlite3_column_text(queryStatement, 3)))
  412.                 let imageDate = String(describing: String(cString: sqlite3_column_text(queryStatement, 4)))
  413.                 let imageDescription = String(describing: String(cString: sqlite3_column_text(queryStatement, 5)))
  414.                 let isImageFav = sqlite3_column_int(queryStatement, 6)
  415.                 let totalImgViews = sqlite3_column_int(queryStatement, 7)
  416.                
  417.                 albums.append(ImageModel(id: Int(id), imageIdentifier: imageIdentifier, albumid: Int(albumId), imageName: imageName, imageDate: imageDate, imageDescription: imageDescription, isImageFav: Int(isImageFav), totalImgViews: Int(totalImgViews)))
  418.             }
  419.         } else {
  420.             print("SELECT statement could not be prepared")
  421.         }
  422.         sqlite3_finalize(queryStatement)
  423.         return albums
  424.     }
  425.    
  426.    
  427.     func deleteAlbumByID(id:Int) {
  428.         let deleteStatementStirng = "DELETE FROM album WHERE id = \(id);"
  429.         var deleteStatement: OpaquePointer? = nil
  430.         if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
  431.            
  432.             sqlite3_bind_int(deleteStatement, 1, Int32(id))
  433.            
  434.             if sqlite3_step(deleteStatement) == SQLITE_DONE {
  435.                 print("Successfully deleted row.")
  436.             } else {
  437.                 print("Could not delete row.")
  438.             }
  439.         } else {
  440.             print("DELETE statement could not be prepared")
  441.         }
  442.         sqlite3_finalize(deleteStatement)
  443.     }
  444.    
  445.     func deleteImgByID(id:Int) {
  446.         let deleteStatementStirng = "DELETE FROM image WHERE id = ?;"
  447.         var deleteStatement: OpaquePointer? = nil
  448.         if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
  449.            
  450.             sqlite3_bind_int(deleteStatement, 1, Int32(id))
  451.            
  452.             if sqlite3_step(deleteStatement) == SQLITE_DONE {
  453.                 print("Successfully deleted row.")
  454.             } else {
  455.                 print("Could not delete row.")
  456.             }
  457.         } else {
  458.             print("DELETE statement could not be prepared")
  459.         }
  460.         sqlite3_finalize(deleteStatement)
  461.     }
  462.    
  463.    
  464.     func deleteImgByIdandAlb(id:Int, albumId: Int) {
  465.         let deleteStatementStirng = "DELETE FROM image WHERE (id = \(id) AND albumid = \(albumId));"
  466.         var deleteStatement: OpaquePointer? = nil
  467.         if sqlite3_prepare_v2(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {
  468.            
  469.             sqlite3_bind_int(deleteStatement, 1, Int32(id))
  470.            
  471.             if sqlite3_step(deleteStatement) == SQLITE_DONE {
  472.                 print("Successfully deleted row.")
  473.             } else {
  474.                 print("Could not delete row.")
  475.             }
  476.         } else {
  477.             print("DELETE statement could not be prepared")
  478.         }
  479.         sqlite3_finalize(deleteStatement)
  480.     }
  481.    
  482.    
  483.     func updateAlbum( id: Int, albumName: String) {
  484.        
  485.         let updateStatementString = "UPDATE album SET albumName = '\(albumName)' WHERE id = '\(id)';"
  486.         var updateStatement: OpaquePointer? = nil
  487.        
  488.         if sqlite3_prepare_v2(db, updateStatementString, -1, &updateStatement, nil) ==
  489.               SQLITE_OK {
  490.             if sqlite3_step(updateStatement) == SQLITE_DONE {
  491.               print("\nSuccessfully updated row.")
  492.             } else {
  493.               print("\nCould not updateImg row.")
  494.             }
  495.         } else {
  496.             print("\nUPDATE statement is not prepared")
  497.         }
  498.         sqlite3_finalize(updateStatement)
  499.     }
  500.    
  501.    
  502.     func updateImg(id: Int, imageIdentifier: String, albumId: Int, imageName: String, imageDate: String, imageDescription: String, isImageFav: Int, totalImgViews: Int) {
  503.        
  504.         let updateStatementString = "UPDATE image SET imageIdentifier = '\(imageIdentifier)', albumid = \(albumId), imageName = '\(imageName)', imageDate = '\(imageDate)', imageDescription = '\(imageDescription)', isImageFav = \(isImageFav), totalImgViews = \(totalImgViews) WHERE id = \(id);"
  505.         var updateStatement: OpaquePointer? = nil
  506.        
  507.         if sqlite3_prepare_v2(db, updateStatementString, -1, &updateStatement, nil) ==
  508.               SQLITE_OK {
  509.             if sqlite3_step(updateStatement) == SQLITE_DONE {
  510.               print("\nSuccessfully updated row.")
  511.             } else {
  512.               print("\nCould not updateImg row.")
  513.             }
  514.         } else {
  515.             print("\nUPDATE statement is not prepared")
  516.         }
  517.        
  518.         sqlite3_finalize(updateStatement)
  519.     }
  520.    
  521. }
  522.  
  523.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement