Preface:

I don’t know if you have any trouble when you need to view the content of the database in iOS. In the past, you exported the database file to the computer, and then opened the database visualization software on MAC to view the data inside. I was not satisfied with this waste of time and low efficiency, so I thought it would be better to view the database directly on the phone or simulator, and operate the database directly, which could greatly save time and improve work efficiency.

Implementation:

  • Most of the database storage in iOS is based on the SQLite framework, while others, such as Realm, have their own database storage engine, and some use Apple’s own Core Data. I’ve only made a visual solution for the SQLite-based implementation here, with possible support for other storage engines in the future.
  • This is done by encapsulating the SQLite API to support generic queries, deletions, and modifications. There is no function to add a new line. But it also solves most of the need to view the data source library, update the database.

The effect can be seen below:

  • You can delete a selected row for a table and modify the value of a row or field (except for the primary key). We all know that usually modify one row of data or delete a where condition is limited, generally speaking is the only sign to judge a field, but we do not know the database which is the only sign of a field, or some of the database is also a primary key does not exist, of course, can also list all fields to judge the current row, This is a bit of energy, some of the fields need to judge a lot. So HERE I use a hidden field ROWID of the database to find out which row we want to modify, but the ROWID is self-increasing and will not be repeated. If a data is deleted, the ROWID of the new data will also be +1 on the original maximum ROWID. If a row is deleted, the ROWID will not correspond to the row displayed. It seems that the path just found is a little blocked. However, we can add restrictions to find the ROWID of this row, and then modify the data of this row through roWID. For example, to modify the third row of data IN the display, you can use a conditional statement like “WHERE rowid IN (SELECT rowid FROM table limit 2,1)”.
  • Update database also through the above ROwid to find a specific line as a qualified condition to modify, but we visually display the string type, modify to the data you want, when sending the update and then modify string to the corresponding field all the corresponding data types to update. # # # # to use
CocoaPods

1. Update Cocoapods to the latest version.

2. Add POD ‘DatabaseVisual’ to your Podfile.

3. Run pod Install or POD Update.

4.import <DatabaseVisual/DatabaseManager.h>.

Manually

1. Download the DatabaseManager folder.

2. Add source files to your project.

3.import “DatabaseManager.h”

The tool is also super easy to use, just specify the folder where you currently store your database. All files with the suffix.db.sqlite.sqlite3 under the folder will be displayed.

//you can specify the folder where the database resides
[DatabaseManager sharedInstance].dbDocumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
[[DatabaseManager sharedInstance] showTables];
Copy the code

Has been updated on Github. If you like, please send 👍. Written in haste, the test sample is not much, any questions please timely mention to me. Of course, any good suggestions are also welcome to me, we discuss to do better. Other storage visualizations such as Realm should be considered in the future.

Finally, the DatabaseVisual address is attached


Welcome to reprint, and indicate the source