数据库Sqlite-(Xcode7,ios9,objective-C)


首先,纪录一下学习的注意点:获取地址的方式和之前的版本其实没有变化,但是要注意一点

NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPath objectAtIndex:0];

    NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPath objectAtIndex:0];
这两段代码只差了其中一个很相似的参数,一开始的时候一直没搞懂为什么路径从之前的/documents变成了/documentation,这是很大差异的,因为暂时没有从模拟器的沙箱中找到这样一个路径,所以刚开始的时候也许会没整理好导致错误。


另外基本的操作:(增、查)剩下的删、改其实没有其他 区别了就是语句的问题了,注意bug主要会出现在sql语句的错误中,其他么就没什么注意点了。

- (IBAction)save:(id)sender {
    sqlite3_stmt *statement;
    const char *dbPath = [databasePath UTF8String];
    
    if (sqlite3_open(dbPath, &database)==SQLITE_OK) {
        if ([stuid.text isEqualToString:@""]) {
            UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry! id nil" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
            [av show];
        }else
        {
            NSString * insertSQL = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",stuid.text,classname.text,name.text];
            const char *insertStatement = [insertSQL UTF8String];
            NSLog(@"%@",insertSQL);
            sqlite3_prepare_v2(database, insertStatement, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE) {
                stuid.text=@"";
                classname.text=@"";
                name.text=@"";
                UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"YES!Saved" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
                [av show];
            }
            else
            {
                UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry!Saved failed" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
                [av show];
            }
            sqlite3_finalize(statement);
            sqlite3_close(database);
        }
    }else{NSLog(@"failed in Saving!");}
}

- (IBAction)query:(id)sender {
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;
    NSLog(@"query");
    if(sqlite3_open(dbpath, &database)==SQLITE_OK){
        NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",stuid.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;
                
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"we" message:@"query good" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *one =[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:nil];
                [controller addAction:one];
                [self presentViewController:controller animated:YES completion:nil];
                
                
            }else{
                
                NSLog(@"NONONONONO");
            }
            sqlite3_finalize(statement);
            
        }else{NSLog(@"tag1");}
            sqlite3_close(database);
    }
}

- (IBAction)clear:(id)sender {
    stuid.text = @"";
    classname.text = @"";
    name.text = @"";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
sqlite-jdbc-3.36是一个用于Java编程语言的JDBC驱动程序,用于连接和操作SQLite数据库SQLite是一种轻量级的嵌入式数据库引擎,没有独立的服务器进程,它将整个数据库作为一个文件存储在主机文件系统中。 sqlite-jdbc-3.36提供了用于连接SQLite数据库的API和工具,使得开发者可以使用Java语言轻松地在应用程序中操作SQLite数据库。它提供了各种功能,如连接数据库、创建和执行SQL语句、事务管理、批处理操作等。 使用sqlite-jdbc-3.36,可以通过以下步骤在Java应用程序中连接和操作SQLite数据库: 1. 下载并导入sqlite-jdbc-3.36的JAR文件到项目中。 2. 加载驱动程序类,这样可以将其注册到Java的JDBC驱动管理器中。 3. 使用JDBC连接字符串指定要连接的SQLite数据库文件路径,并使用驱动程序的getConnection()方法获得一个连接对象。 4. 通过连接对象创建一个语句对象,并使用该对象执行SQL查询或更新语句。 5. 处理和检索结果,可以使用语句对象的executeQuery()方法执行查询SQL语句,并使用结果集对象获取查询结果。 6. 关闭连接和释放资源,最后要确保关闭连接对象和释放相关资源,以防止资源泄漏。 sqlite-jdbc-3.36具有良好的性能和稳定性,可以轻松地与Java应用程序集成,并提供了强大的SQLite数据库操作功能。无论是开发桌面应用程序、移动应用程序还是服务器端应用程序,sqlite-jdbc-3.36都是一个很好的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值