UI 19 database exercises, ui19 database exercises
When you click "add to Favorites" for a movie, you can determine whether it is added to the favorites folder.
Delete and insert a favorite movie.
Create a DataBaseTool
# Import <Foundation/Foundation. h> # import <sqlite3.h> # import "MovieModel. h "typedef NS_ENUM (NSUInteger, SelectInTable) {inTable, NotInTable, SelectError, };@ interface DataBaseTool: NSObject {sqlite3 * dbPoint ;}+ (DataBaseTool *) Export DataBaseTool; // open the database //-(void) openDB; //-(void) createTable;-(SelectInTable) isSaveWithMovie :( MovieModel *) movie;-(void) insertDataWithMovie :( MovieModel *) movie;-(void) deleteDataWithMovie :( MovieModel *) movie;
Internal implementation:
# Import "DataBaseTool. h "@ implementation DataBaseTool + (DataBaseTool *) shareDataBaseTool {static DataBaseTool * dataTool OOl; static dispatch_once_t oneToKen; dispatch_once (& oneToKen, ^ {dataTool = [[DataBaseTool alloc] init] [dataTool openDB]; [dataTool createTable];}); return dataTool OOl;}-(void) openDB {NSArray * sandBox = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ); NSString * sandBoxPath = sandBox [0]; NSString * document = [sandBoxPath stringByAppendingPathComponent: @ "Favorite. sqlite "]; int result = sqlite3_open ([document UTF8String], & dbPoint); if (result = SQLITE_ OK) {NSLog (@" database opened successfully! ");} Else {NSLog (@" database shut down successfully! ") ;}}-(Void) createTable {NSString * sqlStr = @" create table if not exists movie (number integer primary key autoincrement, movieID text, movieName text, pic_url text) "; int result = sqlite3_exec (dbPoint, [sqlStr UTF8String], nil); if (result = SQLITE_ OK) {NSLog (@" movie table created successfully! ");} Else {NSLog (@" An error occurred while creating the movie table! % D ", result) ;}}- (SelectInTable) isSaveWithMovie :( MovieModel *) movie {NSString * sqlStr = [NSString stringWithFormat: @ "select * from movie where movieID = '% @'", movie. movieID]; sqlite3_stmt * stmt = nil; int result = sqlite3_prepare_v2 (dbPoint, [sqlStr UTF8String],-1, & stmt, nil); if (result = SQLITE_ OK) {NSLog (@ "query successful! "); If (sqlite3_step (stmt) = SQLITE_ROW) {return inTable;} else {return NotInTable ;}} else {NSLog (@" query failed! ");} Return SelectError;}-(void) insertDataWithMovie :( MovieModel *) movie {NSString * sqlStr = [NSString stringWithFormat: @" insert into movie (movieID, movieName, pic_url) values ('% @', '% @', '% @') ", movie. movieID, movie. movieName, movie. pic_url]; int result = sqlite3_exec (dbPoint, [sqlStr UTF8String], nil); if (result = SQLITE_ OK) {NSLog (@ "added successfully ");} else {NSLog (@ "failed to join! ") ;}}-(Void) deleteDataWithMovie :( MovieModel *) movie {NSString * sqlStr = [NSString stringWithFormat: @" delete from movie where movieID = '% @' ", movie. movieID]; int result = sqlite3_exec (dbPoint, [sqlStr UTF8String], nil); if (result = SQLITE_ OK) {NSLog (@ "deleted successfully! ");} Else {NSLog (@" deletion failed! ");}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.