Iosfmdb and CoreData

Source: Internet
Author: User

Forward: Http://wenku.baidu.com/link?url=LSPSZSPxN4pVwWNwqEXSoY0-jlnXq-_14C7qV1FV9_gFIMPjdKlXrG4Nrh_ 08ezs10icym3syaulljwj0djvojepa8hl9rq1gvzv50yz7bi

Fmdb is a third-party framework for data storage, and it has many advantages over sqlite and core data.

Fmdb is object-oriented, it encapsulates the C language API of SQLite in OC Way, it is more convenient to use, and does not require much knowledge of database operation. But it also has some problems, such as cross-platform, because it is packaged in OC language, so it can only be used in iOS development, if you want to achieve cross-platform operation, to reduce the cost of development and maintenance costs, you need to use the comparison of the original SQLite.

Core data is an ORM embodiment, using core data need to use the transformation of the model, although the operation is simple, do not need to directly manipulate the database, but performance is not directly using SQLite high. But SQLite needs to use the C language function, the operation is more troublesome, so it needs to be encapsulated. But if it is simply encapsulated, it is likely to overlook many important details, such as how to handle concurrency and security issues.

Therefore, it is recommended to use the third-party framework Fmdb, which is the encapsulation of the LIBSQLITE3 framework, the use of steps similar to SQLite, and it is a multi-threaded simultaneous operation of a table when processed, it means that it is thread-safe. Fmdb is a lightweight framework that is flexible to use and is the first choice for many enterprise development.
Important classes in the Fmdb

Fmdatabase: A Fmdatabase object represents a separate SQLite database for executing SQL statements

Fmresultset: Result set after query execution with Fmdatabase

Fmdatabasequeue: Used to execute multiple queries or updates in multiple threads, it is thread-safe

FMDB Use steps

1. Download the Fmdb file Fmdb to add the Fmdb folder to your project

2. Import the SQLite framework and import the FMDatabase.h file

3. Similar to SQLite use steps, you need to get the database file path, then get the database, open the database, then the database to operate, and finally close the database. The code looks like this:

1.//VIEWCONTROLLER.M

2.//Jrfmdb

3.//

4.//Created by jerehedu on 15/6/18.

5.//Copyright (c) jerehedu 2015. All rights reserved.

6.//

    1. 7.

8. #Import "ViewController.h"

9. #Import "FMDatabase.h"

    1. @interface Viewcontroller ()
    2. @property (nonatomic, strong) Fmdatabase *db;
    3. @end
    4. @implementation Viewcontroller
    5. -(void) viewdidload
    6. {
    7. [Super Viewdidload];
    8. Import the SQLite framework and import the Fmdb folder
    9. 1. Get the path to the database file
    10. NSString *doc=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
    11. NSString *filename=[doc stringbyappendingpathcomponent:@ "Student.sqlite"];
    12. NSLog (@ "filename =%@", filename);
    13. 2. Access to the database
    14. Fmdatabase *db = [Fmdatabase databasewithpath:filename];
    15. 3. Open the Database
    16. if ([DB Open]) {
    17. NSLog (@ "OK");
    18. 4. Create a Watch
    19. BOOL result=[db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (ID integer PRIMARY KEY autoincrement, Name text not NULL, age integer Not NULL); "];
    20. if (Result) {
    21. NSLog (@ "Genesis success");
    22. }Else{
    23. NSLog (@ "Failure to create a table");
    24. }
    25. }
    26. self.db=db;
    27. Inserting data
    28. [Self insertstu];
    29. [Self deletestu:6];
    30. [Self updatestu:@ "apple7_name": @ "7777"];
    31. [Self querystu];
    32. [Self dropstu];
    33. [Self insertstu];
    34. [Self querystu];
    35. 6. Close the database
    36. [Self.db Close];
    37. }
    38. #pragma mark inserts data
    39. -(void) Insertstu
    40. {
    41. for (int i=0; i<10; i++)
    42. {
    43. NSString *name = [NSString stringwithformat:@ "1apple%i_name", I];
    44. int age = arc4random ()%3+20;
    45. 1. Executeupdate: An indeterminate parameter is used for the placeholder (the following parameters must all be OC objects)
    46. [Self.db executeupdate:@ INSERT into t_student (name, age) VALUES (?,?); ', name,@ (age)];
    47. 2. Executeupdatewithformat: Indeterminate parameters are occupied with%@,%d, etc. (the parameter is the original data type)
    48. [Self.db executeupdatewithformat:@ "insert into t_student (name, age) VALUES (%@,%i);", Name,age];
    49. 3. Array
    50. [Self.db executeupdate:@ INSERT into t_student (name, age) VALUES (?,?); ' withargumentsinarray:@[name,@ (age)];
    51. }
    52. }
    53. #pragma mark Delete data
    54. -(void) Deletestu: (int) idnum
    55. {
    56. A. Executeupdate: Indeterminate parameters are used to occupy bits (the following parameters must all be OC objects)
    57. [Self.db executeupdate:@ "Delete from t_student where id=?;", @ (Idnum)];
    58. B. Executeupdatewithformat: Indeterminate parameters are occupied by%@,%d, etc.
    59. [Self.db executeupdatewithformat:@ "Delete from t_student where name=%@;", @ "Apple9_name"];
    60. }
    61. #pragma mark destroys the form
    62. -(void) Dropstu
    63. {
    64. [Self.db executeupdate:@ "drop table if exists t_student;];
    65. 4. Create a Watch
    66. BOOL result=[self.db executeupdate:@ "CREATE TABLE IF not EXISTS t_student (ID integer PRIMARY KEY autoincrement, Name text Not NULL, the age integer is not null); "];
    67. if (Result) {
    68. NSLog (@ "Re-create TABLE success");
    69. }Else{
    70. NSLog (@ "Failed to create a table again");
    71. }
    72. }
    73. #pragma mark to modify the data
    74. -(void) Updatestu: (NSString *) Oldname:(nsstring*) newName
    75. {
    76. [Self.db executeupdatewithformat:@ "Update t_student set name=%@ where name=%@;", Newname,oldname];
    77. [Self.db executeupdate:@ "Update t_student set name=? Where Name=?", Newname,oldname];
    78. }
    79. #pragma mark queries the data
    80. -(void) Querystu
    81. {
    82. 1. Execute the query statement
    83. Fmresultset *resultset = [self.db executequery:@ "select * from T_student;];
    84. Fmresultset *resultset = [self.db executequery:@ "select * from T_student where id<?;", @ (14)];
    85. 2. Iterating through the result set
    86. while ([ResultSet next]) {
    87. int idnum = [resultSet intforcolumn:@ "id"];
    88. NSString *name = [resultSet objectforcolumnname:@ "name"];
    89. int age = [ResultSet intforcolumn:@ ' age '];
    90. NSLog (@ "id=%i, name=%@, age=%i", idnum,name,age);
    91. }
    92. }
    93. -(void) didreceivememorywarning
    94. {
    95. [Super didreceivememorywarning];
    96. Dispose of any resources the can be recreated.
    97. }
    98. @end

Iosfmdb and CoreData

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.