IOS growth path-database data addition and query

Source: Internet
Author: User
/* =--= Add data to the sqlite3 database ===--= * // 1. define a sqlite3 pointer sqlite3 * dB; // 2. the return value int dbrc for receiving successful read/write database operations; // 3. const char * dbfilepathutf8 = [dbfilepath utf8string]; // 4. open the database dbrc = sqlite3_open (dbfilepathutf8, & dB); // determine whether the IF (dbrc = sqlite_ OK) {nslog (@ "");} // 5. define the SQL statement execution pointer sqlite3_stmt * DBPs; // 6. define the SQL statement nsstring * insertstatementns = [nsstring s Tringwithformat: @ "insert into \" Shopping \ "\ (item, price, groupid, dateadd) \ values (\" % @ \ ", % @, % d, datetime ('now ') ", @" lll ", @" 350 ", 2]; // 7. convert the SQL statement of the defined nsstring to the C-style string const char * insertstatement = [insertstatementns utf8string] of utf8; // 8. SQL statement pre-execution. (Determine whether the SQL statement meets the syntax) dbrc = sqlite3_prepare_v2 (dB, insertstatement,-1, & DBPs, null); // 9. execute the SQL statement dbrc = sqlite3_step (DBPs); nslog (@ "dbrc = % d", dbrc ); /* =--= read data in the database =--= * // 1. define a query statement nsstring * querystatementns = @ "select key, item, price, groupid, dateadd from shopping"; // 2. convert the defined nsstring query statement to the utf8 C string const char * querystatement = [querystatementns utf8string]; // 3. SQL statement preprocessing dbrc = sqlite3_prepare_v2 (dB, querystatement,-1, & DBPs, null); nslog (@ "prepared statement = % d", dbrc); // 4. obtain the value. Use a matched method to obtain the value of the corresponding type and convert the while (dbrc = sqlite3_step (DBPs) = sqlite_row) type) {// obtain the primary key value. For int type, use the optional int primarykeyvaluei = sqlite3_column_int (DBPs, 0); // convert the value to nsnumber * primarykeyvalue = [[nsnumber alloc] initwithint: primarykeyvaluei]; nslog (@ "primarykeyvalue = % @", primarykeyvalue); // use sqlite3_column_text nsstring * itemvalue = [[nsstring alloc] initwithuf8string: (char *) sqlite3_column_text (DBPs, 1)]; nslog (@ "itemvalue = % @", itemvalue); // for the double type of price value, use repeated pricevalued = sqlite3_column_double (DBPs, 2 ); nslog (@ "pricevalued = % lf", pricevalued); // Conversion Type nsnumber * pricevalue = [[nsnumber alloc] initwithdouble: pricevalued]; nslog (@ "pricevalue = % @", pricevalue); // obtain the guoupid value int groupvaluei = sqlite3_column_int (DBPs, 3); nslog (@ "groupvaluei = % d ", groupvaluei); nsnumber * groupvalue = [[nsnumber alloc] initwithint: groupvaluei]; nslog (@ "groupvalue = % @", groupvalue ); // obtain the dateadd value nsstring * datevalues = [[nsstring alloc] initwithuf8string :( char *) sqlite3_column_text (DBPs, 4)]; nslog (@ "datevalues = % @", datevalues);} // 10. release sqlite3_finalize (DBPs); // 11. close the database sqlite3_close (db );

Related Article

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.