sqlite简单实用(整理)

前端之家收集整理的这篇文章主要介绍了sqlite简单实用(整理)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#define DBNAME @"BOOKCOLLECT.sqlite"
#define TABNAME @"bookcollect"
#define BOOKNAME @"bookname"
#define BOOKCHAPTER @"book"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong,nonatomic) UIWindow *window;
@property(nonatomic,assign)sqlite3*database;

-(BOOL)isopenDB;
@end
-(BOOL)isopenDB{
    NSString*sqlitepath=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),DBNAME];
    NSLog(@"%@",sqlitepath);
    if (sqlite3_open([sqlitepath UTF8String],&(_database))==sqlITE_OK) {
        return YES;
    }
    return NO;
}
//创建数据库
-(void)Createbookcollectsqlite{
    if ([self isopenDB]) {
     NSString* createTablesql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID INTEGER PRIMARY KEY AUTOINCREMENT,bookname TEXT,book TEXT,bookId TEXT,chapterId TEXT)",TABNAME];
        char *errmsg=NULL;
        if (sqlite3_exec(_database,[createTablesql UTF8String],NULL,&errmsg)==sqlITE_OK) {
            NSLog(@"数据库创建成功");
        }else{
            NSLog(@"数据库创建失败");
        }
        sqlite3_close(_database);
    }
}

//执行sql语句,收藏-------------------------------

-(void)collectToggle:(UIBarButtonItem*)sender
{
    
    if (!isCollected) {
        NSUserDefaults*defaults=[NSUserDefaults standardUserDefaults];
        
        NSString *title=[defaults objectForKey:@"Booktitle"];
        
        NSString*bookchapter=[defaults objectForKey:@"bookchapter"];
        
        AppDelegate*delegate=[UIApplication sharedApplication].delegate;
        
        NSString* bookId = [[NSUserDefaults standardUserDefaults] objectForKey:@"bookIndex"] ;
        NSString *chapterId =[[NSUserDefaults standardUserDefaults] objectForKey:@"bookchapterIndex"];
        NSString *querysql=[NSString stringWithFormat:@"SELECT * FROM bookcollect WHERE bookname='%@'and book='%@'and bookId='%@'and chapterId='%@'",title,bookchapter,bookId,chapterId];
        if([delegate isopenDB]) {
            sqlite3_stmt *stmt;
            if (sqlite3_prepare_v2(delegate.database,[querysql UTF8String],-1,&stmt,NULL)==sqlITE_OK) {
                NSLog(@"开始查询");
                while (sqlite3_step(stmt)==sqlITE_ROW) {
                    char *bookname=(char*)sqlite3_column_text(stmt,1);
                    char*book=(char*)sqlite3_column_text(stmt,2);
                    NSLog(@"%s",bookname);
                    NSLog(@"%s",book);
                    if(bookname&&book) {
                        return;
                    }
                }
                NSString*insertsql=[NSString stringWithFormat:@"INSERT INTO %@ (bookname,book,chapterId) VALUES ('%@','%@','%@')",TABNAME,chapterId];
                char*errmsg=NULL;
                if(sqlite3_exec(delegate.database,[insertsql UTF8String],&errmsg)==sqlITE_OK)
                {
                    NSLog(@"插入数据成功");
                    [self alterShow:@"ブックマーク登録しました"];
                    [sender setTintColor:[UIColor grayColor]];
                    isCollected = YES;
                    [[PFCollectionCount sharedInstance] bookCollectedCount:[NSString stringWithFormat:@"book%d",[bookId intValue]] withChapterName:bookchapter chapterIndex:[chapterId intValue]];
                }else{
                    NSLog(@"插入数据失败%s",errmsg);
                }
            }
            sqlite3_finalize(stmt);
            sqlite3_close(delegate.database);
        }
    }
    else{
        NSUserDefaults*defaults=[NSUserDefaults standardUserDefaults];
        
        NSString *deletesql=[NSString stringWithFormat:@"DELETE FROM bookcollect WHERE bookname=\"%@\"and book=\"%@\"",[defaults objectForKey:@"Booktitle"],[defaults objectForKey:@"bookchapter"]];
        AppDelegate*delegate=[UIApplication sharedApplication].delegate;
        
        if ([delegate isopenDB]) {
            char *errmsg;
            if (sqlite3_exec(delegate.database,[deletesql UTF8String],&errmsg)!=sqlITE_OK) {
                
                NSLog(@"删除失败");
            }else{
                [self alterShow:@"ブックマーク解消しました"];
                NSLog(@"删除成功");
                [sender setTintColor:[UIColor blackColor]];
                isCollected = NO;
            }
            sqlite3_close(delegate.database);
        }
    }

}
原文链接:https://www.f2er.com/sqlite/200882.html

猜你在找的Sqlite相关文章