<span id="Label3"></p><p><p>Several recommendations for database operations:</p></p> <ol> <ol> <li><p>Note the spelling of the string for the SQL execution Statement. (be sure to pay attention to spelling and suffer t_t)</p></li> <li><p>The usual functions of sqlite in OBJ-C.</p><p>(not a few,<strong>sqlite3_open</strong>, sqlite3_close,sqlite3_exec,sqlite3_perpare_v2,sqlite3_step,sqlite3_column_*, ...) ) (an additional two objects: Sqlite,sqlite_stmt)</p></li> <li><p>SQLite syntax. (common database operations are the Same)</p></li> <li><p>If you make a bigger project, the database operation is as encapsulated as Possible.</p></li> <li><p>Use your Search. (no less Detours)</p></li> </ol> </ol><p><p></p></p><p><p>Don't say much nonsense, the code adds comments:</p></p><p><p>Database path</p></p><p><p>Nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES);</p></p><p><p>NSString *documentsdir = [paths objectatindex:0];</p></p><p><p>NSString *filepath = [documentsdir stringbyappendingpathcomponent:@ "test3.sqlite"];</p></p><p><p></p></p><p><p>Open Database</p></p><p><p>If (sqlite3_open ([filePath utf8string], &database)! = Sqlite_ok)</p></p><p><p>{</p></p><p><p>NSLog (@ "failed to open database");</p></p><p><p>}</p></p><p><p></p></p><p><p>Data execution operation encapsulation, do not want to encapsulate directly with sqlite3_exec function</p></p><p><p>Sqlite3_exec returns an enumeration value (shaping) that can perform operations such as creating tables, inserting, deleting, updating, etc.</p></p><p><p>-(int) execsql: (nsstring*) SQL</p></p><p><p>{</p></p><p><p>Char *errmsg;</p></p><p><p>If (sqlite3_exec (database, [sql utf8string], nil, nil, &errmsg)! = sqlite_ok) {</p></p><p><p>NSLog (@ "database Operation failed! ");</p></p><p><p>NSLog (@ "%s", errmsg);</p></p><p><p>Return sqlite_error;</p></p><p><p>}</p></p><p><p>Return sqlite_ok;</p></p><p><p>}</p></p><p><p></p></p><p><p>Call the encapsulated method to create the database</p></p><p><p>NSString *createtablehistory = @ "create table if not exists atable (id integer primary key autoincrement, acolumn nvarchar ( Not null); ";</p></p><p><p>[self execsql:createtablehistory];</p></p><p><p></p></p><p><p>Database query, (to be encapsulated)</p></p><p><p>Thing = [[nsmutablearray alloc] init];</p></p><p><p>NSString *selectsql = @ "select * from atable";</p></p><p><p>Sqlite3_stmt *statement;</p></p><p><p>If (sqlite3_prepare_v2 (database, [selectsql utf8string],-1, &statement, Nil) = = Sqlite_ok) {</p></p><p><p>While (sqlite3_step (statement) = = Sqlite_row) {</p></p><p><p>Nvarchar type query comes out is char*, assign value to nsstring words need to convert,</p></p><p><p>Note that this is the 1th column, and the number of columns starts from column No. 0.</p></p><p><p>If you want to query multiple fields, such as student information, you can package the student into a class and then put the case of that class into the array</p></p><p><p>[thing addobject:[nsstring stringwithutf8string: (char*) sqlite3_column_text (statement, 1)];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Else</p></p><p><p>{</p></p><p><p>NSLog (@ "data Query failed! ");</p></p><p><p>}</p></p><p><p></p></p><p><p>Last when the page closes, or the program closes remember to close the database</p></p><p><p>Sqlite3_close (database);</p></p><p><p>iOS database SQLite basic operations</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