"iOS development" uses SQLite database operations for FMDB encapsulation classes

Source: Internet
Author: User

Create, insert, UPDATE and delete: Use the Executeupdate method, while the query uses ExecuteQuery

1. Instantiating Fmdatabase
Paths:ios document Path, document is a writable and writable folder in iOS
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentdirectory = [Paths objectatindex:0];

DbPath: The database path, in document.
NSString *dbpath = [documentdirectory stringbyappendingpathcomponent:@ "test.db"];

To create a DB instance, this is explained below: If a "test.db" file does not exist in the path, SQLite will automatically create "Test.db"
Fmdatabase *db= [Fmdatabase Databasewithpath:dbpath];
if (![ DB Open]) {
NSLog (@ "Could not open db.");
return;
}


2. Create a table
Create a table named user, with two fields of type Name,integer for string type
[DB executeupdate:@ "CREATE TABLE User (Name text,age integer)"];


3. Insert
Insert data using the type in OC text corresponds to nsstring integer corresponding to NSNumber for shaping
[DB executeupdate:@ INSERT into User (name,age) VALUES (?,?) "@" Zhang San ", [NSNumber numberwithint:20]];


4. Update
Update data Change "Zhang San" to "John Doe"
[DB executeupdate:@] UPDATE User SET Name =? WHERE Name =? @ "John Doe", @ "Zhang San"];


5. Delete
Delete data
[DB executeupdate:@ "DELETE from User WHERE Name =?", @ "Zhang San"];


6. Enquiry
Returns the first result in a database that satisfies a condition
NSString *aa=[db stringforquery:@ "Select Name from User WHERE age =?", @ "20"];

Return all query Results
Fmresultset *rs=[db executequery:@ "select * from User"];

Rs=[db executequery:@ "SELECT * from User WHERE age =?", @ "20"];

while ([Rs next]) {
NSLog (@ "%@%@", [rs stringforcolumn:@ "Name"],[rs stringforcolumn:@ "Age"]);
}

[Rs Close];

From: http://blog.sina.com.cn/s/blog_680a331e0100qkz3.html

"iOS development" uses SQLite database operations for FMDB encapsulation classes

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.