加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何通过sqlite加载数据到tableview

发布时间:2020-12-12 19:59:38 所属栏目:百科 来源:网络整理
导读:#import "mygreenteaTableViewController.h" @interface mygreenteaTableViewController () @end @implementation mygreenteaTableViewController @synthesize Such; - ( id )initWithStyle:( UITableViewStyle )style { self = [ super initWithStyle :styl

#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];

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,&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;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读