如何将 SQlite 中的数据显示到 iPhone 应用程序的表视图中

2024-02-28

我正在 Xcode 4.3 中使用 SQlite3 开发 iPhone 项目,SQlite 和 Xcode 之间的连接已完成,现在我想将我的数据显示到表视图(三个视图)中,并且它是只读的! 所以我有主表视图,选择原始 - >进入第二个视图并从数据库加载其他数据选择原始 - >进入详细信息视图以显示长文本和图像!

任何帮助表示赞赏。

AppDelegate.h

#import "AppDelegate.h"

#import "MasterViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize navigationController = _navigationController;

- (void)dealloc
{
    [_window release];
    [_navigationController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if (success) {

        NSLog(@"we have the database");

    } else {

        NSLog(@"we have no database");

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cities.sqlite"];


        BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];

        if (moved) {
            NSLog(@"database copied");
        }

    }


    MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

MasterViewController.h

#import <UIKit/UIKit.h>
#import <sqlite3.h> 


@class DetailViewController;

@interface MasterViewController : UITableViewController {
    NSMutableArray *cities;
}

@property (strong, nonatomic) DetailViewController *detailViewController;

@end

MasterViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    students = [[NSMutableArray alloc] init];
    countries = [[NSMutableArray alloc] init];

    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
    self.navigationItem.rightBarButtonItem = addButton;


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];


    sqlite3 *database;

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sqlStatement = "select * from cities_info";

        sqlite3_stmt *compileStatement;

        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) {


            while (sqlite3_step(compileStatement) == SQLITE_ROW) {

                NSLog(@"one record");

                NSString *cityName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compileStatement, 1)];

                [cities addObject:cityName];

            }

            NSLog(@"cities: %@",cities);  

        }


    } else {


        NSLog(@"error in database");

    }
}

块引用


我建议在 SQLite 上使用一个轻量级包装器 - 请参阅https://github.com/JohnGoodstadt/EasySQLite https://github.com/JohnGoodstadt/EasySQLite

这将允许:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _personTable.rows.count;
}

AND

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...

     NSArray* row= _personTable.rows[indexPath.row];
     cell.textLabel.text = row[[_personTable colIndex:@"lastname"]];
     ...

通过使用代表 SQL 表的 iVar 进行设置:

self.personTable = [_db  ExecuteQuery:@"SELECT firstname , lastname , age , salary FROM person"];

数据库连接 iVar 传入您的 SQL 文件名:

self.db = [DBController sharedDatabaseController:@"DataTable.sqlite"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 SQlite 中的数据显示到 iPhone 应用程序的表视图中 的相关文章

  • Swift iOS 9.2 中的每日本地通知

    尝试快速发送每日本地通知 然而 由于某种原因 它只是每分钟发送一次 我希望第一个通知在应用程序打开后 30 分钟发送 然后每天重复此通知 在 swift fie 中我有以下代码 每日通知代码 同时在应用程序委托中添加部分 让 Date NS
  • 数组扩展以按值删除对象

    extension Array func removeObject
  • iOS 中通过 USB 进行反向端口转发

    我在桌面上有一个 Web 套接字服务器 在 iPhone 设备上有一个客户端 我想使用 USB 而不是任何网络与他们通信 我已经使用 adb reverse 在 android 上实现了它 但无法找到适用于 iOS 的任何解决方案 我尝试使
  • SwiftUI 转义闭包捕获变异的“self”参数

    我有一个可以通过两种方式打开的视图 一个包含提供给它的数据 另一个包含对 Firestore 文档的文档引用 我创建了两个构造函数 在第一个构造函数中我提供数据 在另一个构造函数中我提供文档参考 然后我使用此引用进行网络调用 但出现错误 E
  • swift:移动动画

    我在故事板中有 viewController 还有4个正方形 我想将我的方块放在视图中 首先我想显示两个正方形 如果我按下按钮 我希望我的红色 2 个方块向左移动 然后显示接下来的 2 个蓝色方块 就像这部动画 我需要创建一个scrollV
  • 在 Objective-C 中比较两次的最好/最简单的方法是什么?

    我有一个时间的字符串表示形式 例如 11 13 AM 这是使用 NSDateFormatter 和 stringFromDate 方法生成的 我想将此时间与当前时间进行比较 但是当我使用 dateFromString 方法将字符串转回日期时
  • SQLite3 中的“预准备”语句?

    在具有固定查询集的内存受限嵌入式系统中使用 SQLite 如果可以 预先准备 查询 则似乎可以节省代码和数据 也就是说 准备好的语句是由 相当于 sqlite3 prepare v2 在build运行时只需要调用 bind step 等 引
  • SQLite 在使用之间不保存数据

    我制作了一个包含以下内容的模块 import sqlite3 as sq connection sq connect test db cursor connection cursor cursor execute DROP TABLE IF
  • 如何让我的“点击”功能与 iOS 配合使用

    我有一组充当按钮的 Div 这些按钮有一个简单的 jquery click 函数 该函数适用于除 iOS 之外的所有浏览器 例如 div class button click me div and button click function
  • Swift 每 5 天重复一次 LocalNotification

    如何每 5 天上午 10 00 重复一次 LocalNotification 我尝试这个 但它不起作用 let content UNMutableNotificationContent content title Hello content
  • 如何在Android Studio中安装android 6.0.1模拟器?

    我在我的 Android 模拟器上遇到这个问题E Surface getSlotFromBufferLocked 未知缓冲区 0xab7519c0 https stackoverflow com questions 33046375 e s
  • 检索旋转的 UIImageView 的所有 4 个坐标

    如何获取 UIImageView 的 4 个坐标 我知道可以得到CGRect以及origin x和origin y 但是如何找到所有4个角呢 编辑 我正在旋转 UIImageViews 这就是我问的原因 P 您可以添加矩形的宽度和高度来获取
  • UIView transitionFromView:如何在转换过程中实现黑色背景?

    我使用下一个代码 UIView transitionFromView viewA toView viewB duration 1 0 options UIViewAnimationOptionTransitionFlipFromLeft c
  • 使用本地化故事板进行即时本地化

    我正在开发一个应用程序 它有一个切换按钮可以在英语和阿拉伯语之间切换 并且应该是动态的 我正在使用该方法https github com maximbilan ios language manager https github com ma
  • 从 Xcode iOS 项目运行 swift 脚本作为构建阶段

    这是一个简单的快速脚本 usr bin env xcrun swift import Foundation let task NSTask task launchPath bin echo task arguments farg1 arg2
  • Google Maps iOS SDK:地图上东西点之间的距离

    如何计算地图东点和西点之间的距离 以米为单位 假设用户更改了滚动地图的位置 然后我使用 mapView didChangeCameraPosition delegate 方法捕获移动 但我不知道如何计算距离 这是一个辅助函数 可用于计算两个
  • 如何在 iOS 中通过 wifi 传输大文件

    我下载了WiTap http developer apple com library ios samplecode WiTap Introduction Intro html代码来自苹果网站 它用于通过本地 WiFi 网络传输数据 我正在从
  • 应用程序终止时处理通知(iOS)

    我正在开发接收通知的应用程序 使用苹果推送通知 我正在存储这些通知并在一个控制器中显示为列表 据我了解 每当收到通知时都会调用 didReceiveRemoteNotification 当应用程序位于前台和后台时 我可以通过 didRece
  • 检测 UITableViewCell 何时离开屏幕

    我正在实施一个丰富的UITableView与定制创建UITableViewCell 我以一种方式在屏幕上显示这些 但是一旦它们离开屏幕 我想记下这一点 因为它们第二次出现时我希望它们以不同的方式显示 认为离开屏幕时自动 标记为已读 我一直在
  • 呈现 UIActivityViewController 时发出警告

    当我提出一个UIActivityController使用我得到的下面的代码 它被呈现 但控制台显示 Warning Attempt to present

随机推荐