zoukankan      html  css  js  c++  java
  • a simple example for sqlite

    //#import "/usr/include/sqlite3.h" //add this to your header of code

    - (void) saveData 
    {
        sqlite3_stmt *statement; 
        const char *dbpath = [databasePath UTF8String]; 
        
        
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
        { 
            NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", name.text, address.text, phone.text]; 
            
            const char *insert_stmt = [insertSQL UTF8String]; 
            sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 
            
            if (sqlite3_step(statement) == SQLITE_DONE)
            { 
                self.status.text = @"Contact added"
                self.name.text = @""
                self.address.text = @""
                self.phone.text = @""
            } else { 
                self.status.text = @"Failed to add contact"
            } 
            sqlite3_finalize(statement); 
            sqlite3_close(contactDB); 
        }
    }

    - (void) findContact 

        const char *dbpath = [databasePath UTF8String]; 
        sqlite3_stmt *statement; 
        
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
        { 
            NSString *querySQL = [NSString stringWithFormat: @"SELECT address, phone FROM contacts WHERE name=\"%@\"", name.text]; 
            
            const char *query_stmt = [querySQL UTF8String]; 
            
            if (sqlite3_prepare_v2(contactDB,query_stmt, -1, &statement, NULL) == SQLITE_OK) 
            { 
                if (sqlite3_step(statement) == SQLITE_ROW) 
                { 
                    NSString *addressField = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text( statement, 0)]; 
                    self.address.text = addressField; 
                    NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
                    
                    self.phone.text = phoneField; 
                    
                    self.status.text = @"Match found"
                } else { 
                    self.status.text = @"Match not found";
                    self.address.text = @""
                    self.phone.text = @""
                } 
                sqlite3_finalize(statement); 
            } 
            sqlite3_close(contactDB); 
        }
    }

  • 相关阅读:
    分布式页面静态化
    采用rabbitMq消息推送方案
    rabbitmq的使用介绍和注意事项目
    redis面试题
    Redis 使用入门
    git使用介绍
    JavaSE11(1)采用类
    原型模式
    javaSE 10(2)
    javaSE 10
  • 原文地址:https://www.cnblogs.com/zyip/p/2656922.html
Copyright © 2011-2022 走看看