Simple database design and use (FMDB), database design fmdb

Source: Internet
Author: User

Simple database design and use (FMDB), database design fmdb

There is a need: m users share n files, one user may use multiple files, one file may be used by multiple users; if a user leaves, this user will no longer use any files. If a file is not used by any user, the user will delete the file. When a user is known to be using some files and needs to manage the user's departure, whether to delete the original file. This problem may be several minutes or minutes for developers, but it is not that easy for an iOS developer. After thinking about it for a long time, I finally came up with a solution after asking my friends on the server... I can only say that the database is so powerful that it cannot be supplemented ~~ Simple record solution: use the database to create three tables. One user table: Record userId userName one file table: Record fileId filePath one user-file relationship table: record the relationship between the user and the file userId-fileId. The relational table is a m-to-n relational table. Both the user table and the file table can be directly added, deleted, modified, and deleted. Note the following when you create a relational table: when a user uses a file, insert a userId-fileId data in the relational table. When the user no longer uses this file, delete this data, so the content of the relational table is probably userId fileId1 11 21 31 42 12 4 ...... If you want to check the number of files used by a user whose userId is I, you only need to check all the userId = I in the relational table. If you want to check the number of users who use a file whose fileId is j, you only need to check all the fileId = j in the relational table. When userId is I, when a user wants to leave, should the files used by the user be deleted? The following steps are required: 1. query the files used by the user, cache the file fileId, 2. Delete the data of all userId = I in the relational table. 3, traverse the cached fileId and query the relational table again to query the number of users using the file. If the number of users using the file is 0, it indicates that the file is no longer used by users, it can be deleted. If the number of users is not 0, it indicates that the file is still used by other users and cannot be deleted. Complete this process ~~~ The logic is done, and the rest is to use SQLite on iOS. FMDB seems to be the best to use ~ FMDB documentation written is also very good I use FMDB to write the above logic a demo, put here what is the problem of https://github.com/Phelthas/Demo_FileManagerWithFMDB, welcome to discuss ~~ (If you think it is good, don't forget to order a star)

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.