Use of IOS SQLite3

Source: Internet
Author: User

1. Create a modifiable database file

//The content within the app package is not writable, so you need to copy a copy of the database from the application package to the resource path- (void) createeditabledatabase{BOOL success; Nsfilemanager*manager =[Nsfilemanager Defaultmanager]; Nserror*error; Nsarray*paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentdir = [Paths Objectatindex:0]; NSString*writabledb = [Documentdir stringbyappendingpathcomponent:@"catalog.db"]; Success=[manager Fileexistsatpath:writabledb]; if(Success) {NSLog (@"already exists"); return; } nsstring*defaultpath = [[[NSBundle Mainbundle] ResourcePath] stringByAppendingPathComponent:@"catalog.db"]; Success= [Manager Copyitematpath:defaultpath topath:writabledb error:&ERROR]; if(!success) {NSAssert1 (0,@"Failed to create writable database file: '%@ '.", [Error localizeddescription]); }ElseNSLog (@"Successful Write");}

2. Initializing the database

- (void) initdatabase{NSString*path = [[NSBundle mainbundle] Pathforresource:@"Catalog"OfType:@"DB"]; //NSLog (@ "bundle =%@\npath =%@", [NSBundle mainbundle],path);    if(Sqlite3_open ([path utf8string],&database) = =SQLITE_OK) {NSLog (@"Opening Database"); }Else{sqlite3_close (database); NSAssert1 (0,@"Failed to open database: '%s '.", Sqlite3_errmsg (database)); }}

3. Enquiry

//Execute SQL statement-(Nsmutablearray *) getallproducts{Nsmutablearray*products = [NsmutablearrayNew]; //const char *sql = "Select Product.id,product. Name,manufacturer.name,product.details,product.price,product.quantityonhand,country.country,product.image from Product,manufacturer,country WHERE Manufacturer.manufacturerid = Product.manufacturerid and Product.countryoforiginid =? ";//mate Sqlite3_bind_int (stmt,1,2) can query using parameterized SQL statements        Const Char*sql ="SELECT product.id,product. Name,manufacturer.name,product.details,product.price,product.quantityonhand,country.country,product.image from Product,manufacturer,country WHERE Manufacturer.manufacturerid = Product.manufacturerid and Product.countryoforiginid = Country.countryid"; NSLog (@"\nsql =%s", SQL); Sqlite3_stmt*stmt; intSqlresult = SQLITE3_PREPARE_V2 (database,sql,-1,&stmt,null); //Sqlite3_bind_int (stmt,1,2);//SQL statement parameter query, statement, parameter index, parameter value        if(Sqlresult = =SQLITE_OK) {NSLog (@"Ready to print sth"); intTime =0;  while(Sqlite3_step (stmt) = =Sqlite_row) {Product*product = [ProductNew]; Char*name = (Char*) Sqlite3_column_text (stmt,1); Char*manufacturer = (Char*) Sqlite3_column_text (stmt,2); Char*details = (Char*) Sqlite3_column_text (stmt,3); Char*countryoforigin = (Char*) Sqlite3_column_text (stmt,6); Char*image = (Char*) Sqlite3_column_text (stmt,7); NSLog (@"%d,name =%s \ n", time++, name); Product.id= Sqlite3_column_int (stmt,0); Product.name= (name)? [NSString Stringwithutf8string:name]:@""; Product.manufacturer= (manufacturer)? [NSString Stringwithutf8string:manufacturer]:@""; Product.details= (details)? [NSString Stringwithutf8string:details]:@""; Product.price= Sqlite3_column_double (stmt,4); Product.quantity= Sqlite3_column_int (stmt,5); Product.countryoforigin= (countryoforigin)? [NSString Stringwithutf8string:countryoforigin]:@""; Product.image= (image)? [NSString Stringwithutf8string:image]:@"";        [Products Addobject:product];    } sqlite3_finalize (stmt); }Else{NSLog (@"Read failed:%d", Sqlresult); }        returnProducts ;}

4. Close the database

-(void) closedatabase{    if (sqlite3_close (database)! = Sqlite_ok) {        NSAssert1 (0@ "error:failed to close database: '%s '). " , Sqlite3_errmsg (Database));}    }

Use of IOS SQLite3

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.