如何通过sqlite加载数据到tableview

#import "mygreenteaTableViewController.h"


@interface mygreenteaTableViewController ()


@end


@implementation mygreenteaTableViewController

@synthesize  Such;


- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}



-(NSString*)databasePath    //===========我把加载数据和获得路径的过程封装为一个方法

{

    

    NSString *docsDir;

    NSArray *dirPaths;

    

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    

    NSString  *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"mygreenteainfo.db"]];//新建一个数据的名字

    

    return databasePath;       //返回路径

}





- (void)viewDidLoad


{

       NSLog(@"%@",NSHomeDirectory());

    [super viewDidLoad];

    

 

    

    

    [super viewDidLoad];

NSString *databasePath=[self databasePath];//调用我原来封装的方法

    

    

    NSFileManager *filemanager = [NSFileManager defaultManager];

    

    if ([filemanager fileExistsAtPath:databasePath] == NO) {

        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &db)==SQLITE_OK)          //这里的db是之前的添加的  *db

        {

            char *errmsg;

            const char *createsql = "CREATE TABLE IF NOT EXISTS INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT,PICTURE TEXT)";//这里是sql语言的创建列表

            if (sqlite3_exec(db, createsql, NULL, NULL, &errmsg)!=SQLITE_OK) {

                //status.text = @"create table failed."; 用来提示的lable

                NSLog(@"create  db ok");

            }

        }

        

    }

    

    

    

    //===============loadDATA==========================================

    

    sqlite3_stmt *statement;


    const char *dbpath = [databasePath UTF8String];

     NSMutableArray *such =[[NSMutableArray alloc]initWithCapacity:30];

      for (int i=1; i<4; i++) {

    if (sqlite3_open(dbpath, &db)==SQLITE_OK) {

        NSString *querySQL = [NSString stringWithFormat:@"SELECT name from info where id=\"%d\"",i];  //================这里就实现了用号码查询,其他的可以另外实现

        const char *querystatement = [querySQL UTF8String];

        

        if (sqlite3_prepare_v2(db, querystatement, -1, &statement, NULL)==SQLITE_OK) {

            

            if (sqlite3_step(statement)==SQLITE_ROW) {

                /*

                 

                 //做个小测试(原来版本)

                 

                 NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                 

                 NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];

                 

                 

                 classname.text = classnameField;

                 

                 name.text = nameField;

                 

                 

                 */

                

                //=======测试部分=====================

                

                

              

            

                

               

                    

                    NSString  *tab=[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                   // select t.user_id,random() as Random from udb_user t limit 10;

                    

                

                    [such addObject:tab];

                

                 self.Such=such;

                }

                

            //    classname.text=[such objectAtIndex:0];

             //   name.text=[such objectAtIndex:1];

                

                

                

                

                

                //=======================================

                

                //status.text = @"find~~~";

            }

            sqlite3_finalize(statement);

        }

        sqlite3_close(db);

    }

   

    //============

    

    

    }


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

   }











- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{


  

    return 1;

}


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

{


   

    return Such.count;

}



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

{

    static NSString *CellIdentifier = @"cell";

   UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];//设置风格

     

       

       

        [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5]];//这样就可以把cell设成半透明

    }

    NSInteger row=[indexPath row];

    

    cell.textLabel.text=[Such objectAtIndex:row];//用数组的数据加载名字!

    

    cell.accessoryType=UITableViewCellStyleDefault;//后面那个标标的风格;

    

    

    return cell;

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值