HOME > Mobile > Mobile Forum > iOS - ดึงข้อมูลมาจาก SQLite ให้โชว์ใน tableview ทำยังไงดีคับ output ข้างล่างก็โชว์นะคับ แต่พอ Simulator แล้วมันไม่โชว์อะคับ
//
// AppDelegate.h
// ProjectTest
//
// Created by Adullyarich on 8/4/56 BE.
// Copyright (c) 2556 Adullyarich. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
UIWindow *window;
}
@property (strong, nonatomic) UIWindow *window;
@end
Code (Objective-C)
//
// AppDelegate.m
// ProjectTest
//
// Created by Adullyarich on 8/4/56 BE.
// Copyright (c) 2556 Adullyarich. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
Code (Objective-C)
//
// OrderViewController.h
// ProjectTest
//
// Created by Adullyarich on 8/4/56 BE.
// Copyright (c) 2556 Adullyarich. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface OrderViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
sqlite3 *database;
}
-(void)initDatabase;
-(void)getStudent;
@end
Code (Objective-C)
//
// OrderViewController.m
// ProjectTest
//
// Created by Adullyarich on 8/4/56 BE.
// Copyright (c) 2556 Adullyarich. All rights reserved.
//
#import "OrderViewController.h"
@implementation OrderViewController{
NSMutableArray *listOfCid;
NSMutableArray *listOfCname;
NSMutableArray *listOfCadd;
}
- (void) initDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
stringByAppendingPathComponent:@"Project.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Project.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath
toPath:writableDBPath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message'%@'.", [error localizedDescription]);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initDatabase];
[self getStudent];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void) getStudent
{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory
stringByAppendingPathComponent:@"Project.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sql = "SELECT * FROM Customer";
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(database, sql, -1,
&searchStatement, NULL) == SQLITE_OK){
listOfCid = [[NSMutableArray alloc] init];
listOfCname = [[NSMutableArray alloc] init];
listOfCadd = [[NSMutableArray alloc] init];
{
while (sqlite3_step(searchStatement) == SQLITE_ROW) {
NSString *cid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
NSString *cadd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 3)];
NSLog(@"Cid: %@ ,Cname: %@,Cadd: %@",cid, name,cadd);
[listOfCid addObject:cid];
[listOfCname addObject:name];
[listOfCadd addObject:cadd];
}
}
}
sqlite3_finalize(searchStatement);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [listOfCid count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier =@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil){
//cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [listOfCid objectAtIndex:indexPath.row];
cell.detailTextLabel.text =[listOfCname objectAtIndex:indexPath.row];
//cell.imageView.image =[UIImage imageNamed:@"thumbnail.png"];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
@end