<span id="Label3"></p><p><p><span style="font-family: 宋体; font-size: 14px;"> <span style="font-size: 16px;">today summarizes the basic use of the database method:</span></span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-size: 16px;"> <span style="color: #ff0000;">The database used by iOS is generally</span> </span></span> <span style="line-height: 1.5; font-size: 16px; color: #ff0000;">sqlite3, You must first import the database framework before using the database, or you will get an error, then introduce the header file</span> <span style="line-height: 1.5; font-size: 16px;"><span style="background-color: #ff0000;">#import <sqlite3.h></span></span> <span style="line-height: 1.5; font-size: 16px; color: #ff0000;"><span style="background-color: #ff0000;"> </span></span></p></p><p><p><span style="line-height: 1.5; font-size: 16px; color: #ff0000;"><span style="background-color: #ff0000;"><span style="background-color: #ffffff;">Create a model class student in the project, a database tool class Databasetool</span></span></span></p></p><p><p><span style="line-height: 1.5; font-size: 16px;"><span style="background-color: #ff0000;"><span style="background-color: #ffffff;">Define several properties in Student.h:</span></span></span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">#import <Foundation/Foundation.h></span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">@interface Student:nsobject</span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">@property (nonatomic,copy) NSString *name;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">@property (nonatomic,copy) NSString *hobby;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">@property (nonatomic,assign) Nsinteger age;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px; font-family: ‘Microsoft YaHei‘;">@end</span></p></p><p><p></p></p><p><p><span style="line-height: 1.5; font-size: 16px;"><span style="background-color: #ff0000;"><span style="background-color: #ffffff;">Declaration of the database operation method in DataBaseTool.h:<br></span></span></span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">#import <Foundation/Foundation.h></span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">#import <sqlite3.h></span></p></p><p><p><span style="font-size: 16px;">#import "Student.h"</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">@interface Databasetool:nsobject</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">The address used to hold the database object</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Sqlite3 *dbpoint;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">To ensure that the current database is unique in the project, we create a database tool object in a single-instance way</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">+ (databasetool *) sharedatabasetool;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Open Database</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) opendb;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Create a table for the database, table</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) createtable;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Insert a student information</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) insertstu: (Student *) stu;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Update a Student's information</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) updatestu: (Student *) stu;</span></p></p><p><p><span style="font-size: 16px;">Delete operation</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) deletedatestu: (Student *) stu;</span></p></p><p><p><span style="font-size: 16px;">Query operations</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(nsmutablearray *) selectallstu;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Close the database</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) closedb;</span></p></p><p><p></p></p><p><p><span style="line-height: 1.5; font-size: 16px;"><span style="background-color: #ff0000;"><span style="background-color: #ffffff;">@end</span></span></span></p></p><p><p><span style="line-height: 1.5; font-size: 16px;"><span style="background-color: #ff0000;"><span style="background-color: #ffffff;">DATABASETOOL.M in the implementation method:</span></span></span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 16px;">#import "dataBaseTool.h"</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">@implementation Databasetool</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">+ (databasetool *) sharedatabasetool{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Static Databasetool *tool;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Static dispatch_once_t onetoken;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Dispatch_once (&onetoken, ^{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Tool=[[databasetool alloc] init];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">});</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Return tool;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) opendb{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">The database file is also stored in the sandbox documents file, so find the sandbox path first</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Nsarray *sandbox=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *sandboxpath=sandbox[0];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Stitching the file path, if the system according to the file path to find the corresponding file will open the database directly, if not it will create a corresponding database</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *documentpath=[sandboxpath stringbyappendingpathcomponent:@ "student.sqlite"];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int Result=sqlite3_open ([documentpath utf8string], &dbpoint);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "database Open successfully");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "%@", documentpath);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "database Open failed");</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) createtable{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Primary key is the primary key meaning, the main living table data is unique, cannot be duplicated, can uniquely identify a piece of data, is generally an integer</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">AutoIncrement self-increment, in order to let the primary key does not repeat, will let the primary key adopt the Self-increment way</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If not exists if no table is created, prevents duplicate creation before overwriting data</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Database problem 90% is a SQL statement problem, so ensure that the statement is not a problem, and then put in the project to use</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *[email protected] "create table if not exists Stu (number integer primary key autoincrement,name text,age integer , Hobby Text) ";</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Execute this SQL statement</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int result=sqlite3_exec (dbpoint, [sqlstr utf8string], nil, nil, nil);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "table Creation succeeded");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "table Creation failed");</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) insertstu: (Student *) stu{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *sqlstr=[nsstring stringwithformat:@ "insert into Stu (name,age,hobby) values ('%@ ', '%ld ', '%@ ')", stu.name, Stu.age,stu.hobby</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Execute SQL statement</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int result=sqlite3_exec (dbpoint, [sqlstr utf8string], nil, nil, nil);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "add Student success");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "add Student failed");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) updatestu: (Student *) stu{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *sqlstr= [nsstring stringwithformat:@ "update Stu set hobby= '%@ ', age=%ld where name= '%@ '", stu.hobby,stu.age, stu.name];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Execute SQL statement</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int result=sqlite3_exec (dbpoint, [sqlstr utf8string], nil, nil, nil);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "update succeeded");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "update failed");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "%d", result);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) deletedatestu: (Student *) stu{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *sqlstr=[nsstring stringwithformat:@ "delete from Stu where name= '%@ '", stu.name];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Execute SQL statement</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int result=sqlite3_exec (dbpoint, [sqlstr utf8string], nil, nil, nil);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "delete succeeded");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "delete failed");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(nsmutablearray *) selectallstu{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Query logic</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">1. First read all data from a table in a local database</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">2. Then read by article, assign the model to the value</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">3. Put the already assigned model into the array and return</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *[email protected] "select * from stu";</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">In the statement * is a wildcard meaning, through a * equivalent to replace all the field names in the table</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">next, you need to define a follow pointer that iterates through each row of data in the database Table.</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Third Parameter: query statement word limit, 1 is no limit</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Sqlite3_stmt *stmt=nil;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int result=sqlite3_prepare_v2 (dbpoint, [sqlstr utf8string],-1, &stmt, nil);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">This method is equivalent to associating the database with the following pointers and completing the query function together.</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Initialize an array to fit the student</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Nsmutablearray *stuarr=[nsmutablearray array];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "query success");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Start traversing every row of data in the query database</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">While (sqlite3_step (stmt) ==sqlite_row) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Let the follow pointer traverse the query and stop the loop if there are no rows</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If the condition is met, the column-wise read content</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">The second parameter represents the current column of data in the first column of the table</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Const unsigned char *name=sqlite3_column_text (stmt, 1);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int Age=sqlite3_column_int (stmt, 2);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Const unsigned char *hobby=sqlite3_column_text (stmt,3);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Convert joins data to type</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Nsinteger stuage=age;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *stuname=[nsstring stringwithutf8string: (const Char *) name];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSString *stuhobby=[nsstring stringwithutf8string: (const Char *) hobby];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Assigns a value to an object and then places the object in an array</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Student *stu=[[student alloc] init];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">stu.name=stuname;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">stu.hobby=stuhobby;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">stu.age=stuage;</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">[stuarr addobject:stu];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">[stu release];</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "query failed");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "%d", result);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">Return stuarr;</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">-(void) closedb{<span style="line-height: 1.5;"> </span></span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">int Result=sqlite3_close (dbpoint);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">If (result==sqlite_ok) {</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "database shutdown succeeded");</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">NSLog (@ "%@", documentpath);</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">}else{</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "database shutdown failed");</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">@end</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p></p></p><p><p>Basic use of iOS database</p></p></span>
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