iOS Learning note 16-sqlite app

Source: Internet
Author: User
<span id="Label3"></p><p><p>/**</p></p><p><p>*</p></p><p><p></p></p><p><p>To apply a database step:</p></p><p><p>1,sqlite3_open: Open or create a database and do a connection operation</p></p><p><p>2, create a database with SQL statements</p></p><p><p>3, with no return value, execute the database statement with the EXEC statement operation</p></p><p><p>4, with a return value, query function with prepare statement, result set traversal results, traversal statement sqlite_step (stmt) = = Sqlite_row</p></p><p><p>5. sqlite_column_xxx The field in the record into a sentence (result set, index value of the Field)</p></p><p><p>**/</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>First refer to the Sqlite3 database file</p></p><p><p></p></p><p><p>Fetch to Folder path</p></p><p><p>NSString *document = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) firstObject];</p></p><p><p>NSLog (@ "%@", document);</p></p><p><p></p></p><p><p>The path to create the database</p></p><p><p>NSString *dbfilepath = [document stringbyappendingstring:@ "/db.sqlite"];</p></p><p><p></p></p><p><p>/**</p></p><p><p>* CREATE Database</p></p><p><p>**/</p></p><p><p>Sqlite3 *db;//connection to the underlying database</p></p><p><p>If (sqlite3_open ([dbfilepath utf8string], &db) = = Sqlite_ok) {//open database successfully Select the name of the Macro//here indicates the success of the creation</p></p><p><p>NSLog (@ "create Database success");</p></p><p><p>}</p></p><p><p>/**</p></p><p><p>* Add data to the database</p></p><p><p>**/</p></p><p><p>NSString *createtablesql = @ "create table Stu3 (id integer PRIMARY key autoincrement,name text,password text)";//create Table's SQ statement</p></p><p><p>If (sqlite3_exec (db, [createtablesql utf8string], null, null, Null) = = Sqlite_ok)</p></p><p><p>{</p></p><p><p>NSLog (@ "create Table succeeded");</p></p><p><p>}//executing statements that create a table</p></p><p><p></p></p><p><p>/**</p></p><p><p>* Insert Data</p></p><p><p>**/</p></p><p><p>NSString *insertsql = @ "insert into Stu3 (name,password) values (' hahah ', ' 567 ')";//string to insert data</p></p><p><p>If (sqlite3_exec (db, [insertsql utf8string], null, null, Null) = = Sqlite_ok) {</p></p><p><p>NSLog (@ "execute INSERT statement success");</p></p><p><p>}</p></p><p><p></p></p><p><p>/**</p></p><p><p>* Insert more than one data</p></p><p><p>User name Dsn1-10</p></p><p><p>Password six digits</p></p><p><p>**/</p></p><p><p>for (int i = 0; i<10; i++) {</p></p><p><p>Insertsql = [nsstring stringwithformat:@ "insert into Stu3 (name,password) values (' dsn%d ', '%06d ') ', i+1,arc4random ()% 100000];</p></p><p><p>NSLog (@ "%@", insertsql);</p></p><p><p></p></p><p><p></p></p><p><p>If (sqlite3_exec (db, [insertsql utf8string], null, null, null) ==sqlite_ok) {</p></p><p><p>NSLog (@ "insert Multiple Data success");</p></p><p><p>}</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>/**</p></p><p><p>* Query Statement</p></p><p><p>**/</p></p><p><p>NSString *selectsql = @ "select *from Stu3";</p></p><p><p>SQLITE3_STMT *stmt;//the Fourth parameter returns a result set</p></p><p><p>Sqlite3_prepare_v2 (db, [selectsql utf8string],-1, &stmt, NULL);//execute SQL statement with return value</p></p><p><p>Iterate the result set//take out a row of the result Set//the macro indicates that the query is made if the row exists</p></p><p><p>While (sqlite3_step (stmt) = = Sqlite_row) {</p></p><p><p>int stu3id = Sqlite3_column_int (stmt, 0);</p></p><p><p>Char *stu3name = (char *) sqlite3_column_text (stmt, 1);</p></p><p><p>Char *stu3password = (char *) sqlite3_column_text (stmt, 2);</p></p><p><p></p></p><p><p>NSLog (@ "id=%d,name=%s,password=%s", stu3id,stu3name,stu3password);</p></p><p><p>}</p></p><p><p></p></p><p><p>Results:</p></p><p><p></p></p><p><p></p></p><p><p>iOS Learning note 16-sqlite app</p></p></span>

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.