IOS development: use native SQL functions in Core Data applications

Source: Internet
Author: User

IOS developmentApplication inCore DataThe use of native SQL is the content to be introduced in this article, mainly to learnCore DataApplication and database operations. Apple providesCore DataFunction, which greatly improves convenience for common database application development.

When you create a Window Base Application, select the above and above to use Core Data, and the template will be automatically created. All the methods for using Core Data to access Data are provided in the delegate file, in other View controllers, you only need to call the methods in delegate. Modifying the Data Model and creating Entity definitions based on the Model also provides visualized operations. Once an instance is defined, you only need to create a new instance in the program and assign a value to the variable, call the save method in delegate to add a new record. This method is especially useful for fields that are more difficult to process in SQL such as long text and time. It is also convenient to retrieve the list of saved records. (For details, refer to the Core Data getting started tutorial provided by apple ).

However, when I needed the aggregation function, it was completely silly. Although Core Data also provides simple sum and avg operations, however, the sum under ralationship cannot be found at all, or even the solution is not found on the forum. Do I need to retrieve the list and manually summarize it one by one? In this way, the efficiency is lower when the data volume is large. Of course, if you can directly use SQL statements to summarize at this time, it will be the easiest. However, the Core Data package is complete and I don't know where it stores the Data. What should I do? Do you want to useSqliteTo rewrite the entire program, you need to make significant unnecessary changes to the existing functions such as addition and details.

Finally, we found a solution: Core Data is based onSqliteSo its underlying layer is still usedSqliteThe database used to store data is in the delegate file. It is usually the sqlite file named by the program name under the program Documents directory in the procedural order. If the program has been run in the Simulator, you only need to enter the Library/Application Support/iPhone Simulator/User/Applications/in the User directory. Here is the program that has been run in all your simulators, find the one you need and go to the Documents file, which is an automatically generated sqlite database file. Copy it and use the sqlite3 command to directly view the database structure.

By default, the name of the generated data table is your Entity name, which starts with a letter Z. The field name is also the Instance name you defined, and starts with a letter Z, the primary key is a self-increasing int type. With this database structure, add the framework of libsqlite3.0.dylib to the program, and then you can use the native sqlite3 function in the program.

With easeCore dataWith simple data operations and fully customized sqlite functions, you can now develop powerful database applications as you wish.

Summary:IOS developmentApplication inIn Core DataThe content of the application's use of native SQL functions has been introduced. I hope this article will help you!

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.