(BOOL) addticket {
NSString *event=@"max";
NSString *venue=@"tvm";
sqlite3 *database;
databaseName = @"smbhDB.sql";
sqlite3_stmt *addStatement ;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if(addStatement == nil) {
const char *sql ="insert into tickets (venue, event,) Values (?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
NSAssert1(0, @"444 Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStatement, 1, [venue UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 2, [event UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStatement))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(addStatement);
...
此代码无效。请有人帮我纠正。如果有人可以发布示例代码,请上传。
答案 0 :(得分:3)
Dublicate? Why can't I execute this code?
您必须在执行语句
之前打开数据库答案 1 :(得分:1)
我不是百分百肯定,因为没有提供错误详细信息,但看起来addStatement
未初始化。 Objective-C是否在堆栈上分配时将所有指针初始化为0?我知道在Linux中无法正常工作。
也许试试这个:sqlite3_stmt *addStatement = nil;
我不得不怀疑,你为什么要查看addStatement == nil
?没有理由检查它,因为你必须在每种情况下初始化它。
简而言之,就像这样重写:
(BOOL) addticket {
NSString *event=@"max";
NSString *venue=@"tvm";
sqlite3 *database;
databaseName = @"smbhDB.sql";
sqlite3_stmt *addStatement ;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
const char *sql ="insert into tickets (venue, event,) Values (?,?)";
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
/* this is no longer conditional code */
if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
NSAssert1(0, @"444 Error while creating add statement. '%s'", sqlite3_errmsg(database));
sqlite3_bind_text(addStatement, 1, [venue UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 2, [event UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStatement))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(addStatement);
...
答案 2 :(得分:1)
考虑FMDB或Omni核心数据,如库。所有细节都隐藏在一个简单易用的抽象界面背后。
http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html
或
http://www.omnigroup.com/developer(OmniDataObjects)
祝你好运。