UI 19 database exercises, ui19 database exercises

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.