Encapsulation of SQL statements

Source: Internet
Author: User

//

Sqlmanager.m

0427Ibook

//

Created by jerehedu on 15/4/29.

Copyright (c) 2015 jerehedu. All rights reserved.

//

#import "SqlManager.h"

#import "Book.h"

@implementation Sqlmanager

/**

* Get Links

* @return Linked objects

*/

+ (Sqlite3 *) getconn{

0 defining links

Static Sqlite3 * SQLite;

if (Sqlite==nil) {

1> get the Sandbox documents path

NSString * PATH=KPATH;

NSLog (@ "%@", Kpath);

Path =[path stringbyappendingpathcomponent:@ "Book.sqlite"];

2> If the file exists, open the link directly if it does not exist and open

if (Sqlite_ok==sqlite3_open (path. Utf8string, &sqlite)) {

NSLog (@ "create success");

}else{

NSLog (@ "Create failed");

}

}

return sqlite;

}

/**

* EXECUTE statement

*

* SQL executed @param sql

* Tips for @param tips

*

* @return is successful

*/

+ (BOOL) EXEC: (NSString *) SQL Withtips: (NSString *) tips{

1 Getting Links

Sqlite3 * Sqlite=[sqlmanager Getconn];

2 EXECUTE statement

Char *errmsg;

if (sqlite_ok==sqlite3_exec (SQLITE, SQL. Utf8string, nil, nil, &errmsg)) {

NSLog (@ "%@ success", tips);

return YES;

}else{

NSLog (@ "%@ failed, Reason%s", tips,errmsg);

return NO;

}

}

/**

* Check out people information

*

* @param SQL < #sql description#>

*

* @return < #return value description#>

*/

+ (Nsarray *) Getallbookwithsql: (NSString *) sql{

1 Getting Links

Sqlite3 * Sqlite=[sqlmanager Getconn];

2 Querying data

Nsmutablearray *array=[nsmutablearray Array];

Sqlite3_stmt *stmt;

if (sqlite_ok== sqlite3_prepare_v2 (SQLITE, SQL. Utf8string,-1, &stmt, nil)) {

Iterate through the current result set and take out the data

while (Sqlite3_step (stmt) ==sqlite_row) {

Extracting result set data

int bookid= sqlite3_column_int (stmt, 0);

Const unsigned char * bookname= sqlite3_column_text (stmt, 1);

Const unsigned char * summary= sqlite3_column_text (stmt, 2);

int readnum= sqlite3_column_int (stmt, 3);

int doller= sqlite3_column_int (stmt, 5);

Const unsigned char * writer= sqlite3_column_text (stmt, 4);

Const unsigned char * bookpic= sqlite3_column_text (stmt, 6);

Putting the result set data into an array

Book *book=[[book alloc] init];

Book.book_id=bookid;

Book.book_name=[nsstring stringwithutf8string: (const char *) BookName];

Book.summary=[nsstring stringwithutf8string: (const char *) summary];

Book.book_pic=[nsstring stringwithutf8string: (const char *) BOOKPIC];

Book.writer=[nsstring stringwithutf8string: (const char *) writer];

Book.doller=doller;

Book.read_num=readnum;

[Array Addobject:book];

}

}else{

NSLog (@ "Query failed");

}

return array;

}

/**

* Judgment is not there

*

* @param SQL < #sql description#>

*

* @return is present

*/

+ (BOOL) Isexistwithsql: (NSString *) sql{

1 Getting Links

Sqlite3 * Sqlite=[sqlmanager Getconn];

2 Querying data

Sqlite3_stmt *stmt;

if (sqlite_ok== sqlite3_prepare_v2 (SQLITE, SQL. Utf8string,-1, &stmt, nil)) {

Iterate through the current result set and take out the data

while (Sqlite3_step (stmt) ==sqlite_row) {

Extracting result set data

int count= sqlite3_column_int (stmt, 0);

Return count>0;

}

}else{

NSLog (@ "Query failed");

}

Sqlite3_finalize (stmt);

return NO;

}

/**

* Get account information

*

* @return < #return value description#>

*/

+ (NSString *) getaccount{

Writes the logged in person information to the sandbox

NSString *path=[kpath stringbyappendingpathcomponent:@ "User.tt"];

Nsfilemanager * Manager =[nsfilemanager Defaultmanager];

NSData * Data=[manager Contentsatpath:path];

NSString * account=[[nsstring alloc] Initwithdata:data encoding:nsutf8stringencoding];

return account;

}

+ (void) closeconn{

Sqlite3 * Sqlite=[sqlmanager Getconn];

Sqlite3_close (SQLite);

Sqlite=nil;

}

@end

Encapsulation of SQL statements

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.