Advertisement
ZTCYING

Lab8- PeopleDB

May 4th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.27 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  PeopleDB
  4. //
  5. //  Created by Sabin Tabirca on 08/03/2024.
  6. //
  7.  
  8. import UIKit
  9.  
  10. @main
  11. class AppDelegate: UIResponder, UIApplicationDelegate {
  12.  
  13.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  14.         // Override point for customization after application launch.
  15.         return true
  16.     }
  17.  
  18.     // MARK: UISceneSession Lifecycle
  19.  
  20.     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  21.        
  22.         return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  23.     }
  24.  
  25.     func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  26.     }
  27. }
  28.  
  29. //
  30. //  SceneDelegate.swift
  31. //  PeopleDB
  32. //
  33. //  Created by Sabin Tabirca on 08/03/2024.
  34. //
  35.  
  36. import UIKit
  37.  
  38. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  39.  
  40.     var window: UIWindow?
  41.  
  42.  
  43.     func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  44.        
  45.         guard let _ = (scene as? UIWindowScene) else { return }
  46.     }
  47.  
  48.     func sceneDidDisconnect(_ scene: UIScene) {
  49.        
  50.     }
  51.  
  52.     func sceneDidBecomeActive(_ scene: UIScene) {
  53.        
  54.     }
  55.  
  56.     func sceneWillResignActive(_ scene: UIScene) {
  57.        
  58.     }
  59.  
  60.     func sceneWillEnterForeground(_ scene: UIScene) {
  61.        
  62.     }
  63.  
  64.     func sceneDidEnterBackground(_ scene: UIScene) {
  65.        
  66.     }
  67.  
  68.  
  69. }
  70.  
  71. //
  72. //  PeopleTableViewController.swift
  73. //  PeopleDB
  74. //
  75. //  Created by Sabin Tabirca on 08/03/2024.
  76. //https://pastebin.com/5e6Yg4Gd - objective C code
  77. // https://pastebin.com/7Hj0kcER - swift table view code
  78.  
  79. import UIKit
  80.  
  81. class PeopleTableViewController: UITableViewController {
  82.    
  83.     let peopleData = PeopleModel().allPeopleData();
  84.  
  85.     override func viewDidLoad() {
  86.         super.viewDidLoad()
  87.  
  88.        
  89.     }
  90.  
  91.     // MARK: - Table view data source
  92.  
  93.     override func numberOfSections(in tableView: UITableView) -> Int {
  94.         // #warning Incomplete implementation, return the number of sections
  95.         return 1
  96.     }
  97.  
  98.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  99.         // #warning Incomplete implementation, return the number of rows
  100.         return peopleData!.count
  101.     }
  102.  
  103.    
  104.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  105.         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  106.  
  107.         // Configure the cell...
  108.         let personData = peopleData![indexPath.row] as! [String]
  109.         cell.textLabel?.text = personData[0]
  110.         cell.detailTextLabel?.text = personData[1]
  111.  
  112.         return cell
  113.     }
  114.    
  115.  
  116.     /*
  117.     // Override to support conditional editing of the table view.
  118.     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  119.         // Return false if you do not want the specified item to be editable.
  120.         return true
  121.     }
  122.     */
  123.  
  124.     /*
  125.     // Override to support editing the table view.
  126.     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  127.         if editingStyle == .delete {
  128.             // Delete the row from the data source
  129.             tableView.deleteRows(at: [indexPath], with: .fade)
  130.         } else if editingStyle == .insert {
  131.             // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  132.         }    
  133.     }
  134.     */
  135.  
  136.     /*
  137.     // Override to support rearranging the table view.
  138.     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  139.  
  140.     }
  141.     */
  142.  
  143.     /*
  144.     // Override to support conditional rearranging of the table view.
  145.     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  146.         // Return false if you do not want the item to be re-orderable.
  147.         return true
  148.     }
  149.     */
  150.  
  151.     /*
  152.     // MARK: - Navigation
  153.  
  154.     // In a storyboard-based application, you will often want to do a little preparation before navigation在基于故事板的应用程序中,您通常需要在导航之前做一些准备工作
  155.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  156.         // Get the new view controller using segue.destination.使用 segue.destination 获取新的视图控制器。
  157.         // Pass the selected object to the new view controller.将选定的对象分配给新的视图控制器。
  158.     }
  159.     */
  160.  
  161. }
  162.  
  163. //
  164. //  PeopleModel.m
  165. //  PeopleDB
  166. //
  167. //  Created by Sabin Tabirca on 08/03/2024.
  168. //
  169.  
  170. #import <Foundation/Foundation.h>
  171. #import "PeopleModel.h"
  172.  
  173. @implementation PeopleModel
  174.  
  175. static sqlite3 * database;
  176.  
  177. // utility methods to where, open, close etc
  178. -(NSString *)getBundlePath{
  179.     NSString * path = [[NSBundle mainBundle]pathForResource:@"people" ofType:@"sqlite"];
  180.     return path;
  181. }
  182.  
  183. -(NSString *)getDocumentsPath{
  184.     NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  185.     NSString * path = [paths[0] stringByAppendingString:@"/people.sqlite"];
  186.     return path;
  187.    
  188. }
  189.  
  190. -(void)openDatabase{
  191.     NSString * nsPath = [self getDocumentsPath];
  192.     const char * utfPath = [nsPath UTF8String];
  193.    
  194.     BOOL success = sqlite3_open(utfPath, &database);
  195.     if(!success){
  196.         NSLog(@"DB CANNOT OPEN");
  197.     }
  198. }
  199.  
  200. -(void)closeDatabase{
  201.     sqlite3_close(database);
  202. }
  203.  
  204. // interface messages
  205. -(id)init{
  206.     self = [super init];
  207.    
  208.     // move teh db file from bundle to documents将 db 文件从捆绑包移动到文档
  209.     NSString * bundlePath = [self getBundlePath];
  210.     NSString * documentPath = [self getDocumentsPath];
  211.    
  212.     NSFileManager * manager = [NSFileManager defaultManager];
  213.     if(![manager fileExistsAtPath:documentPath]){
  214.         [manager copyItemAtPath:bundlePath toPath:documentPath error:NULL];
  215.     }
  216.     return self;
  217. }
  218.  
  219. -(NSArray *)allPeopleData{
  220.    
  221.     NSMutableArray * data = [[NSMutableArray alloc]initWithCapacity:20];
  222.    
  223.     [self openDatabase];
  224.    
  225.     // do retrieve work做检索工作
  226.     const char * query = "select * from people";
  227.    
  228.     static sqlite3_stmt * statement;
  229.    
  230.     BOOL success = sqlite3_prepare_v2(database, query, -1, &statement, NULL);
  231.     if(!success){
  232.         NSLog(@"DB CANNOT MAKE STATEMENT");
  233.     }
  234.    
  235.     while(sqlite3_step(statement) == SQLITE_ROW){
  236.         // extract the data from the statement从语句中提取数据
  237.         const char * name  = (const char *)sqlite3_column_text(statement, 0);
  238.         const char * phone = (const char *)sqlite3_column_text(statement, 1);
  239.        
  240.         // place name and phone to data地名和电话到数据
  241.         NSString * nsName  = [NSString stringWithUTF8String:name];
  242.         NSString * nsPhone = [NSString stringWithUTF8String:phone];
  243.        
  244.         [data addObject:@[nsName,nsPhone]];
  245.        
  246.     }
  247.    
  248.     [self closeDatabase];
  249.    
  250.     return data;
  251. }
  252.  
  253. @end
  254. //https://pastebin.com/3Z2PPZqY
  255.  
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement