iOS 数据库

来源:互联网 发布:电影机智问答知乎 编辑:程序博客网 时间:2024/06/01 10:32

ios中常用的数据是sqlite,常见的编辑器是lita

新建数据库DishOrder,两张表一张是Category,一张是Dish表。

Category表中,一个是Categoryid,一个是CategoryTitle.

Dish表中,Dishid,DishTitle,DishWeight,DishPrice,Categoryid

把数据库文件加入前一个工程。下载FMDB库。

https://github.com/ccgus/fmdb

删除FMDB.m文件。添加FrameWork

libsqlite3.dylib

TableViewController添加成员变量

 NSMutableArray *_items;

修改TableViewController.m

- (void)viewDidLoad

{

    [superviewDidLoad];


    // 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;

    //self.tableView.rowHeight=80;

   self.title=@"首页";

    _items=[[NSMutableArrayalloc]init];

    FMDatabase *db=[FMDatabasedatabaseWithPath:[[NSBundlemainBundle]pathForResource:@"DishOrder"ofType:@"db"]];

   if ([dbopen]) {

       FMResultSet *fResult =[dbexecuteQuery:@"select * from category"];

       while ([fResultnext]) {

           NSString *t=[fResultstringForColumn:@"categoryTitle"];

            [_itemsaddObject:t];

        }

        [fResultclose];

    }

    [dbclose];

    

}

- (void) viewDidUnload

{

    [_itemsrelease];

   _items=nil;

    [superviewDidUnload];

}

- (void)dealloc

{

    [_itemsrelease];

    [superdealloc];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

#warning Incomplete method implementation.

    // Return the number of rows in the section.

   return [_itemscount];

}

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

{

   staticNSString *CellIdentifier =@"Cell";

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier ];

    

    // Configure the cell...

   if (!cell) {

                    cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];

        

        }

   

    cell.textLabel.text=[_itemsobjectAtIndex:indexPath.row];

      return cell;

}

源码地址:http://download.csdn.net/detail/cloud95/5191785
原创粉丝点击