The use of WCDB for iOS development data storage

Source: Internet
Author: User
Tags create database

A. Class field bindings

In Wcdb, ORM (Object relational Mapping) refers to a OBJC class that maps to tables and indexes of a database, and maps the property of a class to a field in a database table;

This process. Through ORM, we can achieve database operation directly through object, eliminating the purpose of assembling process.

WCDB through the built-in macros to achieve ORM functionality. As follows:

Create a new model:

. h Files

#import <Foundation/Foundation.h>
#import <WCDB/WCDB.h>//To import wcdb
//message.h
// Inherit wcttablecoding
@interface message:nsobject

/** information id*/
@property (nonatomic, assign) Nsinteger MessageID;
/** Information title *
/@property (nonatomic, strong) NSString *title;
/** Information Creation Time * * *
@property (nonatomic, assign) Nsinteger createtime;
/** Information Content * * *
@property (nonatomic, strong) NSString *content;
/** Information words *
/@property (nonatomic, assign) Nsinteger count;

Implement Wcttablecoding Agent
Wcdb_property (MessageID)
Wcdb_property (title)
Wcdb_property (createtime)
wcdb_property (content)
Wcdb_property (count)

@end

. mm file (you need to change the. m file to. mm file, because WCDB is based on objective-c++)

MESSAGE.MM
#import "Message.h"
@implementation message

//class
using Wcdb_implementatio macros to bind to a database table in a class file definition Wcdb_implementation (message)
//Use Wcdb_synthesize macros to define fields in the class file that need to be bound to a database table
wcdb_synthesize (message, MessageID)
wcdb_synthesize (message, title)
Wcdb_synthesize (Message, Createtime)
wcdb_synthesize (message, content)
wcdb_synthesize (message, Count)
//used to define a primary key
Wcdb_primary (Message, MessageID)

@end

The process of ORM-bind an existing OBJC class is as follows: This class is defined to follow the Wcttablecoding protocol. Can be defined on a class declaration or through a file template in category. Use the WCDB_PROPERTY macro to declare a field in a header file that you want to bind to a database table. Use the Wcdb_implementatio macro to define a class that is bound to a database table in a class file. Use the Wcdb_synthesize macro to define a field in the class file that you want to bind to a database table.

A simple few lines of code complete the process of binding the class and required fields to the database table. These three macros are similar to the definition of a OBJC class in name and usage habits, making it easy to remember.

In addition, WCDB provides a number of optional macros for defining database indexes, constraints, and so on, such as: Wcdb_primary used to define a primary key Wcdb_index to define an index wcdb_unique to define a unique constraint wcdb_not_null to define a Non-empty constraint ...

For more on ORM definitions, refer to: ORM Tutorial

Two. Change and check (CRUD)

Thanks to an ORM definition, WCDB can be directly used to perform an incremental search (CRUD) operation via object. Developers can use the Wctdatabase and wcttable two classes for general additions and deletions to check operations.

1. First create a Wctdatabase

Get the sandbox address of the database file
+ (NSString *) Wcdbfilepath
{
    Nsarray *filepath = Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES);
    NSString *documentpath = [FilePath objectatindex:0];
    NSString *dbfilepath = [documentpath stringbyappendingpathcomponent:@ "message.db"];
    return dbfilepath;
}

1. Create DATABASE Operations
+ (BOOL) creatdatabase
{db
    = [[Wctdatabase alloc] initwithpath:[self Wcdbfilepath]];
    if (![ Database Istableexists:table_wcdb_name]) {
        BOOL result = [Database Createtableandindexesofname:table_wcdb_name WithClass:Message.class];
        return result;
    }
    Return YES
}

2. Increase

#pragma mark-Add
+ (BOOL) InsertData: (message *) message
{
    if (message = = nil) {return
        NO;
    }
    if (database = = nil) {
        [self creatdatabase];
    }
    BOOL result = [Database Insertobject:message into:table_wcdb_name];
    return result;
}
+ (BOOL) Insertdatas: (nsarray<message *> *) messages
{
    if (database = = nil) {
        [self creatdatabase];
    }
    BOOL result = [Database Insertobjects:messages into:table_wcdb_name];
    Return YES
}

3. By deleting

#pragma mark-censored
+ (BOOL) Deletedatawithid: (Nsinteger) MessageID
{
    if (database = = nil) {
        [self Creatdatabase];
    }
    BOOL result = [Database deleteobjectsfromtable:table_wcdb_name where:Message.messageId = = MessageID];
    return result;
}
+ (BOOL) deletealldata
{
    if (database = = nil) {
        [self creatdatabase];
    }
    BOOL result = [Database Deleteallobjectsfromtable:table_wcdb_name];
    return result;
}

4. Change

 #pragma mark-change + (BOOL) UpdateData: (NSString *) content Byid: (Nsinteger) MessageID {if (database = =
    Nil) {[Self creatdatabase];
    *message = [[Message alloc] init];
    message.content = content; BOOL result = [Database Updaterowsintable:table_wcdb_name onProperties:Message.content withobject:message where:
    Message.messageid = = MessageID];
return result;
    } + (BOOL) UpdateData: (message *) message {if (database = = nil) {[Self creatdatabase]; BOOL result = [Database updaterowsintable:table_wcdb_name onProperties:Message.content withobject:message where:mess
    Age.messageid = = Message.messageid]; [Database Updaterowsintable:table_wcdb_name onProperties:Message.createTime withobject:message where:
    Message.messageid = = Message.messageid]; [Database updaterowsintable:table_wcdb_name onProperties:Message.title withobject:message where:Message.messageId =
    = Message.messageid];
return result; }

5. Check

#pragma mark-check
+ (nsarray<message *> *) getalldata
{
    if (database = = nil) {
        [self creatdatabase];< c4/>} return
    [Database GetAllObjectsOfClass:Message.class fromtable:table_wcdb_name];
}
+ (nsarray<message *> *) GetData: (Nsinteger) MessageID
{
    if (database = = nil) {
        [self creatdatabase]; 
    }
    return [Database getObjectsOfClass:Message.class fromtable:table_wcdb_name where:Message.messageId = = MessageID];
}
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.