#import "mygreenteaTableViewController.h"
@H_502_4@@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;
}