如何通过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];

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;

}

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能