sqllite3在iphone编程中准备插入查询

时间:2021-10-04 02:12:44
(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);
...

This code not working. Please can someone help me to correct it. If anybody can post sample code please upload.

这段代码不起作用。请有人帮我纠正。如果有人可以发布示例代码,请上传。

3 个解决方案

#1


Dublicate? Why can't I execute this code?

Dublicate?为什么我不能执行这段代码?

YOU MUST OPEN DATABASE before execute statement

您必须在执行语句之前打开数据库

#2


I'm not 100% sure, because there's no error detail provided, but it looks like addStatement is not being initialized. Does Objective-C initialize all pointers to 0 when they're allocated on the stack? I know that in Linux that would not work.

我不是100%肯定,因为没有提供错误细节,但看起来addStatement没有被初始化。当Objective-C在堆栈上分配时,它们是否将所有指针初始化为0?我知道在Linux中不行。

Perhaps try this: sqlite3_stmt *addStatement = nil;

也许试试这个:sqlite3_stmt * addStatement = nil;

I have to wonder, why are you even checking addStatement == nil? There's no reason to check it, since you must initialize it in every case.

我不得不怀疑,为什么你甚至检查addStatement == nil?没有理由检查它,因为你必须在每种情况下初始化它。

In short, re-write like so:

简而言之,重写如此:

(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);
...

#3


Consider FMDB or the Omni core data like library. All the details are hidden behind a nice easy abstract interface.

考虑FMDB或Omni核心数据,如库。所有细节都隐藏在一个简单易用的抽象界面背后。

http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html

or

http://www.omnigroup.com/developer (OmniDataObjects)

Good luck.

#1


Dublicate? Why can't I execute this code?

Dublicate?为什么我不能执行这段代码?

YOU MUST OPEN DATABASE before execute statement

您必须在执行语句之前打开数据库

#2


I'm not 100% sure, because there's no error detail provided, but it looks like addStatement is not being initialized. Does Objective-C initialize all pointers to 0 when they're allocated on the stack? I know that in Linux that would not work.

我不是100%肯定,因为没有提供错误细节,但看起来addStatement没有被初始化。当Objective-C在堆栈上分配时,它们是否将所有指针初始化为0?我知道在Linux中不行。

Perhaps try this: sqlite3_stmt *addStatement = nil;

也许试试这个:sqlite3_stmt * addStatement = nil;

I have to wonder, why are you even checking addStatement == nil? There's no reason to check it, since you must initialize it in every case.

我不得不怀疑,为什么你甚至检查addStatement == nil?没有理由检查它,因为你必须在每种情况下初始化它。

In short, re-write like so:

简而言之,重写如此:

(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);
...

#3


Consider FMDB or the Omni core data like library. All the details are hidden behind a nice easy abstract interface.

考虑FMDB或Omni核心数据,如库。所有细节都隐藏在一个简单易用的抽象界面背后。

http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html

or

http://www.omnigroup.com/developer (OmniDataObjects)

Good luck.