iphone - 如何插入到iPhone sdk中的表(sqlite db)

标签 iphone sqlite sdk insert

如何将数据插入 iphone sdk (xcode) 中的 sqlite 数据库? 谢谢!

最佳答案

首先您需要使用这两种方法初始化数据库

-(id) initDatabase{
    databaseName = @"db.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    [databasePath retain];

    return self;
}

-(void) checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    [fileManager release];
}

然后您可以与它交互:

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    static sqlite3_stmt *compiledStatement;
    sqlite3_exec(database, [[NSString stringWithFormat:@"insert into myTable (var1, var2) values ('%@', '%@')", myVar1, myVar2] UTF8String], NULL, NULL, NULL);
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

关于iphone - 如何插入到iPhone sdk中的表(sqlite db),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305255/

相关文章:

iphone - 在iPhone中使用FMOD向音频添加效果并保存新音频

iphone - 用于 arm7/iOS 4.3 的 pjsip 构建 - 在 mips_test.o 中找不到符号

mysql - SQL 查询使用第二个查找表解码表

android - 无法从游标适配器获取数据

sql - 如何在 SQLite 中使用序列?

c++ - 使用C++ SDK从OPCUA读取自定义节点-字符串作为节点标识符

java - 无法升级 eclipse 或 Android ADK 并且找不到 java.exe...hunh?

iphone - NSInvocationOperation 忽略 maxConcurrentOperationCount

iphone - 制作 iPhone 4S ***ONLY*** 应用程序

json - 去dialogflow SDK WebhookResponse没有返回正确的json