Sqlite3 数据库使用

来源:互联网 发布:python快速开发平台 编辑:程序博客网 时间:2024/05/22 15:44

iphone本身是支持 Sqlite3 数据库的,在项目中导入libsqlite3.dylib。并创建数据库,在终端,创建数据库的方式:

---》mkdir  SQL   //创建SQL文件夹

---》cd SQL  //进入SQL目录下

---》sqlite3  Student.sql//创建名为 Student.sql的数据库  

//创建表

- (void)createTable{
    sqlite3 * database;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(
                                                      NSDocumentDirectory,
                                                      NSUserDomainMask,
                                                      TRUE);
      NSString * path=[paths objectAtIndex:0];
    filePath=[path stringByAppendingPathComponent:kFileName];
    NSLog(@"this is kFileName : %@",kFileName);
    NSLog(@"init  filePath : %@",filePath);
    NSFileManager * fileManager=[NSFileManager defaultManager];
    BOOL fileFinded=[fileManager fileExistsAtPath:filePath];
    NSLog(@"this is fileFinded  :  %d",fileFinded);
    if (fileFinded) {
        NSLog(@"database is existed");
        if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
            char *errorMsg;
            NSString * createSQL=
            @"create table  newteacher (id integer primary key ,username text,password text)";
            if (sqlite3_exec(database,
                             [createSQL UTF8String],
                             NULL,
                             NULL,
                             &errorMsg) == SQLITE_OK) {
                NSLog(@"create ok....");
            }
        }
              sqlite3_close(database);
    }
}


//插入数据
- (void)insertDataToTalbe{
    sqlite3 *database;
    if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
        sqlite3_stmt * sqlStatement;
        NSLog(@"-----11111111");
        const char * sql="insert into newteacher (username,password) values (?,?)";
        if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL)==SQLITE_OK) {
            NSLog(@"-------222222");
            sqlite3_bind_text(sqlStatement,
                              1,
                              [student.username UTF8String],
                              -1,
                              SQLITE_TRANSIENT);
            sqlite3_bind_text(sqlStatement,
                              2,
                              [student.password UTF8String],
                              -1,
                              SQLITE_TRANSIENT);
        }
        sqlite3_step(sqlStatement);
        sqlite3_finalize(sqlStatement);
    }
    sqlite3_close(database);
}
//查询数据
- (BOOL)selectDatabase{
     sqlite3 * database;
    sqlite3_stmt * compliedStatement=nil;
    NSLog(@"11111");
    if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
        NSLog(@"2222222");
        const char * sql="select * from newteacher";
        if (sqlite3_prepare(database, sql , -1, &compliedStatement, nil)==SQLITE_OK) {
            NSLog(@"333333");
            while (sqlite3_step(compliedStatement)==SQLITE_ROW) {
           NSLog(@"--------%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compliedStatement, 1)]) ;
            }
             sqlite3_step(compliedStatement);
        }
        sqlite3_close(database);
    }
    return YES;
}

方法不全面,练习时写下的。