Create a sms.db database both sides create a message table, insert the data, and then read the data

Source: Internet
Author: User

Fmdb third-party libraries

Import Header File

#import "FMDatabase.h"
#import "FMResultSet.h"

Fmdatabase *_database;//Database objects

-  (void) readdata{    //1. Gets the path to the database file     nsarray *path= Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask, yes);    Nsstring *documentpath=[path objectatindex:0];   nsstring *dbpath=[documentpath  stringbyappendingpathcomponent:@ "Sms.db"];    //2. Create database     _database=[[fmdatabase alloc]initwithpath:dbpath];    //3.open//First   Database file if it does not exist then   will create and open         //if it exists   then open it directly     if   ([_database open])  {        nslog (@ "Database open successfully");         //Creating a new table does not exist,         nsstring  *[email protected] "Create table if not exists message (Serial integer  primary key autoincrement,guid integer,text string) ";         // In addition to querying everything else with executeupdate        bool issuccess=[_database  executeupdate:sql];        if  (!isSuccess)  {             nslog (@ "creattable error:%@", _ Database.lasterrormessage);                    }        //Inserting Data          nsstring *[email protected] "insert into message  (serial , Guid,text)  values (?,?,?) ";         bool issuccess2=[_database executeupdate:sql2,@ "111" , @ "119", @ "111"];        if  (!ISSUCCESS2)  {              nslog (@ "Insert failed");         }else{             nslog (@ "Insert succeeded");         }    }    //Lookup Table allthequstions     fmresultset *resultset=[_database executequery:@ "select * from  Message "];    //read data by line     while  ([Resultset next])  {         //corresponding fields to read data          nsstring *serial=[resultset stringforcolumn:@ "Serial"];         nsstring *guid=[resultset stringforcolumn:@ "GUID"];         nsstring *text=[resultset stringforcolumn:@ "Text"];         nslog (@ "serial:% @guid:%@,text:%@", serial,guid,text);     }    [_database close];} 


Create a sms.db database both sides create a message table, insert the data, and then read the data

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.