• sqlite--代码操作


    1.创建数据库

            NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

            NSLog(@"%@",docPath);

            NSString * filePath = [docPath stringByAppendingPathComponent:@"stu.sql"];

            if (sqlite3_open([filePath UTF8String], &_mySQL)!=SQLITE_OK) {

                NSLog(@"open error");

                return 0;

            }

            NSLog(@"open ok");

     2.创建表       

            NSString *createTablesql = @"create TABLE IF NOT EXISTS Student (id integer primary key autoincrement,name varchar(20),address varchar(20),icon blob)";

            char *error = nil;

            if (sqlite3_exec(_mySQL, [createTablesql UTF8String], nil, nil, &error)!=SQLITE_OK) {

                NSLog(@"creat table error");

                return 0;

            }

            NSLog(@"creat table ok");

    3.插入数据

        NSString *insertSql =  @"insert into Student (name,address,icon) values (?,?,?)";

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(mysql, [insertSql  UTF8String], -1, &statement, NULL) != SQLITE_OK) {

            NSLog(@"insert prepare error");

            return;

        }else{

            NSLog(@"insert prepare ok");

            NSData * imageData = UIImagePNGRepresentation(self.iconImageView.image);

            sqlite3_bind_text(statement, 1, [_nameTextField.text UTF8String], -1, NULL);

            sqlite3_bind_text(statement, 2, [_addTextField.text UTF8String], -1, NULL);

            sqlite3_bind_blob(statement, 3, [imageData bytes], (int)[imageData length], NULL);

            if(sqlite3_step(statement)!=SQLITE_DONE){

                NSLog(@"insert error");

            }else{

                NSLog(@"insert ok");

            }

        }    

        sqlite3_finalize(statement);

    4.查询数据

        NSString *selectSql = @"select * from Student";

        sqlite3_stmt *statement; 

        if (sqlite3_prepare_v2(mysql, [selectSql UTF8String], -1, &statement, NULL) != SQLITE_OK) {

            NSLog(@"read prepare error");

            return;

        }else{

            NSLog(@"read prepare ok");

            while (sqlite3_step(statement)==SQLITE_ROW) {

                Student * stu = [[Student alloc]init];

                char * name = (char *)sqlite3_column_text(statement, 1);

                char * address = (char *)sqlite3_column_text(statement, 2);

                Byte * bytes = (Byte *)sqlite3_column_blob(statement, 3);

                int length = sqlite3_column_bytes(statement, 3);

                NSData * data = [NSData dataWithBytes:bytes length:length];

                UIImage * image = [UIImage imageWithData:data];

                stu.name = [NSString stringWithUTF8String:name];

                stu.address = [NSString stringWithUTF8String:address];

                stu.icon = image;

                [_studentArray addObject:stu];

            }

        }

  • 相关阅读:
    在linux中安装JAVA的环境和安卓的环境(1)
    如何安装Tomcat
    Android开发历程_2(实现简单的乘法计算)
    Android开发历程_1(从1个activity跳转到另一个activity)
    Java 征途:行者的地图
    android系统架构之虚拟机
    Android四大组件及生命周期
    GridView属性大全
    安卓中各种用到的监听器
    移动端控制台排查方法
  • 原文地址:https://www.cnblogs.com/damonWq/p/5342997.html
Copyright © 2020-2023  润新知