IOS-FMDB- database usage – (executeUpdate:, withArgumentsInArray:, executeUpdate:)

FMDB- database initial use

Preface

Recently, the company is not very busy, take the time to summarize the storage of the database in the L chat module, and share it with you.

The first choice of coreData to store, later found that the use of coreData is not easy, including behind the dynamic add fields modify the database structure, need to be adapted online and offline different data structure, then adaptation spent a lot of time to finish, then that version iteration and maintenance is not good, finally give up coreData, select FMDB.

In this article, we implement data tables built from scratch, as well as the bug used in our work and the data security we need to take into account. For the introduction of FMDB is not to say, a lot of online search.

The talent school sparse shallow, if considered incomplete and insufficient, hope the generous with your criticism, thank you!

Cut the crap and get to the point.

1, pod management integration FMDB

This operation is not much said, the terminal command is ok.

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)

2, packaging FMDB tool class, to achieve the creation of data tables, and complete the basic additions and deletions of the search function.

JWDFMDBChatMessageData.h

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)

Create a global, create, open, close, whether the data table in the link, each time to write data, to ensure that the database has been opened, link state, when the program exits, or do not use the database, close the database in a timely manner, to ensure the security of the database. Have something to do with.

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)

The operation of the database has nothing to do with, additions and deletions to change the search, its core is the realization of SQL statements, if you need other needs, just SQL statement is different. Such as the need to query, chat message state read and unread messages, the success or failure of the state to obtain the last record message is displayed in the dynamic changes; a message of each state and so on.

So learn the basic crud, the rest is learned without teacher. Well, to say that.H begins to walk into.M is a concrete implementation.

JWDFMDBChatMessageData.m

Two point one

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
creates singleton

In the init method, determine whether the specified path already exists or not, to create and manipulate tables.

When you create a table, once you create a table that fails, exit immediately to prevent unnecessary trouble and improve code robustness.

At the same time, create and modify the version number of the data table, which beginners may not take into account, but just implement the function. In fact, this is for future iterative maintenance to lay a good foundation. If you need to modify the structure of the data table, then the different version numbers will be used, for differentiation, and also for good maintenance. Here’s how to dynamically modify the data table structure.

Two point two

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
build table

The construction of the table, the need to build the table extraction uniform established, convenient management and maintenance for logic, business logic between tables, pay attention to open the data table and close data table pairs, do not close in time. Ensure data security.

Pay attention to grammatical structure when writing the SQL statement, the general operation was more than 90 per cent of the SQL statement is wrong in.

Two point three

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
adds version number

Based on timestamp, the common version number is convenient for later query management.

2.4 dynamically modify the data table structure

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
update data sheet version number

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
dynamically modify data table structure

In the init method you will see the dynamic modification of data table structure, if the latter, you need to add some fields in the table, then it can be added easily, it will not affect the changes before and after the data structure caused by different pit, don’t need to think of coreData costs as hate big effort to fit. Used coreData will feel whether FMDB is too cool.

3 additions and deletions to change

3.1.

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
inserts data

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
several methods of inserting data

Query FMDB library, you will find that there are several operating method to insert data, most of the time we will use the first way to operate, because this method is simple, easy to write, but this method is to use the time to have a pit, if your data has special characters, then insert the data will fail, such as the single quotes’, and other special characters, I was in the use of cheating too. So, if you read here, Congratulations, a pit is missing.

The solution, of course, is, and the FMDB library might have thought of writing the problem, all of which could be solved by second methods (arrays) and third (Dictionary key pairs). I am using second methods, third methods of implementation, self-learning, the following code.

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
special character processing

Look at the code structure is not too much, because in the use of methods 2S, you need to report the data into an object in order to deal with, and why? You know, what’s in the array?

If there are multiple tables, or more places to use such writing, repeat, is not too tired too so. You can encapsulate a method to rely on the SQL statement. The relationship between time, not beautiful, not out, later modify synchronization.

3.2 deleted

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)

3.3 change

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
change

3.4 check

IOS-FMDB- database usage - (executeUpdate:, withArgumentsInArray:, executeUpdate:)
check

Check, for the chat data storage, query, careful will find, there are two ID, one is landing ID, one is chat each other’s ID.

Join the ID login, can realize app data from multiple account login with a mobile phone which will not. This is easy to ignore. That’s what I didn’t take into account when I was writing a chat.

Join the chat each other’s ID, in order to log in a ID, for multiple friends chat record query, will generally think of here.

Summary

The basic note has been written, FMDB is not that simple, I can only be here, if you also need to pay attention to what, please enlighten, wish to learn from you.

Finally use the -Demo attached to the demo address FMDB- database

If you think it’s helpful, welcome to comment or star.

This article is original. All rights reserved. Please explain the source.