SQLite的使用——bruceyou1990

前端之家收集整理的这篇文章主要介绍了SQLite的使用——bruceyou1990前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文教你如何使用sqlite

一. 创建数据库

/*1@H_404_9@ 创建数据库---路径*/@H_404_9@
 dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
docsDir = [dirPaths objectAtIndex:0@H_404_9@];
/*2@H_404_9@ 创建数据库---db名字 databasePath这个地址*/@H_404_9@
// Build the path to the database file  关于数据库 根据docsDir的路径 找到contacts.db 这个数据库 databasePath这个地址

    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]]@H_404_9@;

二. 数据的操作

// filemgr 拿到数据库的管理权@H_404_9@
    NSFileManager@H_404_9@ *filemgr = [NSFileManager@H_404_9@ defaultManager];

    //判断databasePath 是否存在@H_404_9@
    if@H_404_9@ ([filemgr fileExistsAtPath:databasePath] == NO@H_404_9@) 
    {
        //导数据操作 要把NSString转换为C 因为sqlite是C语言写的 它不能理解什么是NSString@H_404_9@
        const@H_404_9@ char@H_404_9@ *dbpath = [databasePath UTF8String];


        //打开数据库 然后在判断 是否打开成功sqlITE_OK 默认为0打开成功@H_404_9@
        if@H_404_9@ (sqlite3_open(dbpath,&contactDB)==sqlITE_OK) 
        {
            char@H_404_9@ *errMsg;
            //一般看到sql这样的 就应该明白 这是一条sql 数据库语句@H_404_9@
            const@H_404_9@ char@H_404_9@ *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ADDRESS TEXT,PHONE TEXT)"@H_404_9@;



/* iOS中没有提供创建数据库的命令,当使用sqlite3_open时,如果数据库文件不存在会自行创建数据库,如果存在会打开数据库。打开数据库后就可以创建表并操作表内容了,iOS中的sqlite3使用sqlite3_exec来创建表、插入表内容修改内容删除内容等操作,使用sqlite3_prepare_v2来查询表。下面给给出了sqlite3_exec的封装:*/@H_404_9@



//通过 sqlite3_exec 在已经创建好的 contactDB 里面创建一条 sql_stmt 这样的表格 错误信息 &errMsg 这里从封装好的 sql里面返回@H_404_9@
            if@H_404_9@ (sqlite3_exec(contactDB,sql_stmt,NULL@H_404_9@,&errMsg)!=sqlITE_OK) {

                status.text@H_404_9@ = @"创建表失败\n"@H_404_9@;
            }
        }
        else@H_404_9@ 
        {
            status.text@H_404_9@ = @"创建/打开数据库失败"@H_404_9@;
        }
    }

三. 数据库的读写

//保存数据@H_404_9@
- (IBAction@H_404_9@)SaveToDataBase:(id@H_404_9@)sender 
{
    sqlite3_stmt *statement;

    const@H_404_9@ char@H_404_9@ *dbpath = [databasePath UTF8String];

    if@H_404_9@ (sqlite3_open(dbpath,&contactDB)==sqlITE_OK) {
        NSString@H_404_9@ *insertsql = [NSString@H_404_9@ stringWithFormat:@"INSERT INTO CONTACTS (name,address,phone) VALUES(\"%@\",\"%@\",\"%@\")"@H_404_9@,name.text@H_404_9@,address.text@H_404_9@,phone.text@H_404_9@];
        const@H_404_9@ char@H_404_9@ *insert_stmt = [insertsql UTF8String];
        sqlite3_prepare_v2(contactDB,insert_stmt,-1@H_404_9@,&statement,NULL@H_404_9@);
        if@H_404_9@ (sqlite3_step(statement)==sqlITE_DONE) {
            status.text@H_404_9@ = @"已存储到数据库"@H_404_9@;
            name.text@H_404_9@ = @""@H_404_9@;
            address.text@H_404_9@ = @""@H_404_9@;
            phone.text@H_404_9@ = @""@H_404_9@;
        }
        else@H_404_9@
        {
            status.text@H_404_9@ = @"保存失败"@H_404_9@;
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }
}

//读取数据@H_404_9@
- (IBAction@H_404_9@)SearchFromDataBase:(id@H_404_9@)sender 
{
    const@H_404_9@ char@H_404_9@ *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;

    if@H_404_9@ (sqlite3_open(dbpath,&contactDB) == sqlITE_OK) 
    {
        NSString@H_404_9@ *querysql = [NSString@H_404_9@ stringWithFormat:@"SELECT address,phone from contacts where name=\"%@\""@H_404_9@,name.text@H_404_9@];
        const@H_404_9@ char@H_404_9@ *query_stmt = [querysql UTF8String];
        if@H_404_9@ (sqlite3_prepare_v2(contactDB,query_stmt,NULL@H_404_9@) == sqlITE_OK) 
        {
            if@H_404_9@ (sqlite3_step(statement) == sqlITE_ROW) 
            {
                NSString@H_404_9@ *addressField = [[NSString@H_404_9@ alloc] initWithUTF8String:(const@H_404_9@ char@H_404_9@ *)sqlite3_column_text(statement,0@H_404_9@)];
                address.text@H_404_9@ = addressField;

                NSString@H_404_9@ *phoneField = [[NSString@H_404_9@ alloc] initWithUTF8String:(const@H_404_9@ char@H_404_9@ *)sqlite3_column_text(statement,1@H_404_9@    )];
                phone.text@H_404_9@ = phoneField;

                status.text@H_404_9@ = @"已查到结果"@H_404_9@;
                [addressField release];
                [phoneField release];
            }
            else@H_404_9@ {
                status.text@H_404_9@ = @"未查到结果"@H_404_9@;
                address.text@H_404_9@ = @""@H_404_9@;
                phone.text@H_404_9@ = @""@H_404_9@;
            }
            sqlite3_finalize(statement);
        }

        sqlite3_close(contactDB);
    }
}

猜你在找的Sqlite相关文章