美文网首页
SQLite数据库的简单使用

SQLite数据库的简单使用

作者: 雷鸣1010 | 来源:发表于2017-01-11 13:35 被阅读13次

DEMO地址下载

创建数据库:

一:接下来如果该数据库不存在需要创建这个数据库,创建的过程写在viewDidLoad里面:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 一:创建数据库
    NSString *docsDir;
    NSArray *dirPaths;
    
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  //  NSLog(@"dirpaths = %@",dirPaths);
    docsDir = [dirPaths objectAtIndex:0];
  //  NSLog(@"docsDir = %@",docsDir);
    databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"info.db"]];
  //  NSLog(@"databasePath = %@",databasePath);
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    if ([filemanager fileExistsAtPath:databasePath] == NO) {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
            char *errmsg;
            const char *createsql = "CREATE TABLE IF NOT EXISTS INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, NUM TEXT, CLASSNAME TEXT,NAME TEXT)";
            
            if (sqlite3_exec(dataBase, createsql, NULL, NULL, &errmsg)!=SQLITE_OK) {
                status.text = @"create table failed.";
            }
        }
        else {
            status.text = @"create/open failed.";
        }
    }
}

因为SQLite数据库是文件数据库,是保存在文件系统中的,ios下:
Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
我们的数据库文件是保存在Documents下的。
切记,因为用的是C语法,sqlite3_open传入的是database的地址!

二:保存信息:

屏幕快照 2017-01-11 下午12.19.09.png
//保存信息
- (IBAction)saveinfo:(id)sender {
    
    sqlite3_stmt *statement;
    
    const char *dbpath = [databasePath UTF8String];
    
    if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
        if ([num.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SORRY!" message:@"number cannot be nil!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else {
        
        NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",num.text,classname.text,name.text];
        const char *insertsatement = [insertSql UTF8String];
        sqlite3_prepare_v2(dataBase, insertsatement, -1, &statement, NULL);
        if (sqlite3_step(statement)==SQLITE_DONE) {
            status.text = @"save to DB.";
            num.text = @"";
            classname.text = @"";
            name.text = @"";
        }
        else {
            status.text = @"save failed!";
        }
        sqlite3_finalize(statement);
        sqlite3_close(dataBase);
        }
    }
}

三:在往数据库里面插入数据的时候,我们需要先打开数据库,然后执行插入语句,结束的时候切记要关闭数据库!

// 查询信息
- (IBAction)searchResult:(id)sender {
    const char *dbpath = [databasePath UTF8String];
    
    sqlite3_stmt *statement;
    
    if (sqlite3_open(dbpath, &dataBase)==SQLITE_OK) {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",num.text];
        const char *querystatement = [querySQL UTF8String];
        if (sqlite3_prepare_v2(dataBase, querystatement, -1, &statement, NULL)==SQLITE_OK) {
            if (sqlite3_step(statement)==SQLITE_ROW) {
                NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
                classname.text = classnameField;
                NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                name.text = nameField;
                
                status.text = @"find~~~";                
            }
            else {
                status.text = @"did not find you need.";
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(dataBase);
    }
}

四:查询操作同样也是需要先打开数据库,再查询,最后关闭数据库,在这里就指定了根据学号来查询,其他情况未涉及。

可以使用Navicat软件打开数据包来查看数据


屏幕快照 2017-01-11 下午12.20.10.png 屏幕快照 2017-01-11 下午12.19.09.png

五:在本例中还涉及一个触摸屏幕来关闭键盘:

在viewcontroller.h中添加申明代码:

//点击屏幕收起键盘
- (IBAction)backgroundTap:(id)sender {
    [num resignFirstResponder];
    [classname resignFirstResponder];
    [name resignFirstResponder];
}

相关文章

网友评论

      本文标题:SQLite数据库的简单使用

      本文链接:https://www.haomeiwen.com/subject/fnfrbttx.html