Sqlite3 数据库使用

本文介绍了如何在iOS应用中使用SQLite3进行数据库操作,包括创建数据库和表、插入数据及查询数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值