库例程调用不按顺序进行

发布于 2024-10-28 03:43:47 字数 840 浏览 2 评论 0原文

我正在尝试在 iPhone 上的 sqlite 中写入数据库,我想在数据库中输入 3 个值。下面是我的代码,但我收到以下 sqlite 错误消息:

-(void) writeintoDatabase:(id)value1:(id)value2:(id)value3 {
sqlite3 *database;


    const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
    sqlite3_stmt *insert_statement;
    sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);

    sqlite3_bind_text(insert_statement, 1, [value1 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 3, [value1 UTF8String], -1, SQLITE_TRANSIENT);

    NSInteger keyID;
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) ); 

        keyID = sqlite3_last_insert_rowid(database);


    sqlite3_reset(insert_statement);

}

I am trying to write into a DB in sqlite on the iphone , i want to enter 3 values into the DB . Given Below is my code but i am getting the following sqlite error message:

-(void) writeintoDatabase:(id)value1:(id)value2:(id)value3 {
sqlite3 *database;


    const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
    sqlite3_stmt *insert_statement;
    sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);

    sqlite3_bind_text(insert_statement, 1, [value1 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 3, [value1 UTF8String], -1, SQLITE_TRANSIENT);

    NSInteger keyID;
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) ); 

        keyID = sqlite3_last_insert_rowid(database);


    sqlite3_reset(insert_statement);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟雨凡馨 2024-11-04 03:43:47

谢谢mmcomb,谢谢马特。根据您的建议,我已经完全解决了问题。

  • 首先我没有检查数据库路径然后打开
    连接。

  • 其次在准备好声明并限制之后
    在调用之前我必须调用 sqlite3_step 的适当变量
    sqlite3_last_insert_rowid

  • 第三我需要调用sqlite3_finalize来释放资源。

  • 第四次我需要sqlite3_close连接。

“库例程调用顺序错误”错误是由于未正确打开和关闭连接造成的。

这是正确的代码:

NSInteger keyID; // in .h class
NSMutableArray *user;// in .h class

-(void) writeintoUserDatabase:(NSString*)value1:(NSString*)value2:(NSString*)value3 
{
sqlite3* database;
databaseName = @"UserData";// create a db with this name and keep it in app resources folder
NSString *pathToDatabase;
NSArray *documentPaths =         NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
pathToDatabase = [documentsDir stringByAppendingPathComponent:databaseName];
int databaseReturnCode = sqlite3_open([pathToDatabase UTF8String], &database);
NSLog(@"databaseReturnCode %d",databaseReturnCode);
if(databaseReturnCode == SQLITE_OK) {

const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
sqlite3_stmt *insert_statement;
    sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);

    sqlite3_bind_text(insert_statement, 1, [value0 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [value2 UTF8String], -1, SQLITE_TRANSIENT);

printf( "is there an error?: %s\n", sqlite3_errmsg(database) ); 

    while(sqlite3_step(insert_statement) == SQLITE_ROW) 
    {
        NSString *aFirstName  = [NSString stringWithUTF8String:      (char*)sqlite3_column_text(insert_statement, 1)];
        NSString *aLastName  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(insert_statement, 2)];
        NSString *aUserName  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(insert_statement, 3)];
users = [[NSMutableArray alloc]initWithObjects:aFirstName,aLastName,aUserName,nil];
    }

keyID = sqlite3_last_insert_rowid(database);


    sqlite3_reset(insert_statement);

sqlite3_finalize(insert_statement);

}
sqlite3_close(database);    
}

Thanks mmcomb, Thanks Mat. From both your advices i have solved the problem completely.

  • Firstly I wasn't checking the DB path and then opening the
    connection.

  • Secondly after having prepared the statement and bounding the
    appropriate variables i had to call sqlite3_step before calling
    sqlite3_last_insert_rowid.

  • Thirdly i needed to call sqlite3_finalize to free the resources.

  • Fourthly i needed to sqlite3_close the connection.

'library routine called out of sequence' error was due to not opening and closing the connection properly.

This is the right code:

NSInteger keyID; // in .h class
NSMutableArray *user;// in .h class

-(void) writeintoUserDatabase:(NSString*)value1:(NSString*)value2:(NSString*)value3 
{
sqlite3* database;
databaseName = @"UserData";// create a db with this name and keep it in app resources folder
NSString *pathToDatabase;
NSArray *documentPaths =         NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
pathToDatabase = [documentsDir stringByAppendingPathComponent:databaseName];
int databaseReturnCode = sqlite3_open([pathToDatabase UTF8String], &database);
NSLog(@"databaseReturnCode %d",databaseReturnCode);
if(databaseReturnCode == SQLITE_OK) {

const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
sqlite3_stmt *insert_statement;
    sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);

    sqlite3_bind_text(insert_statement, 1, [value0 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [value2 UTF8String], -1, SQLITE_TRANSIENT);

printf( "is there an error?: %s\n", sqlite3_errmsg(database) ); 

    while(sqlite3_step(insert_statement) == SQLITE_ROW) 
    {
        NSString *aFirstName  = [NSString stringWithUTF8String:      (char*)sqlite3_column_text(insert_statement, 1)];
        NSString *aLastName  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(insert_statement, 2)];
        NSString *aUserName  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(insert_statement, 3)];
users = [[NSMutableArray alloc]initWithObjects:aFirstName,aLastName,aUserName,nil];
    }

keyID = sqlite3_last_insert_rowid(database);


    sqlite3_reset(insert_statement);

sqlite3_finalize(insert_statement);

}
sqlite3_close(database);    
}
溺渁∝ 2024-11-04 03:43:47

您的代码缺少对 sqlite3_step 的调用。您需要在准备好语句并绑定适当的变量之后、调用 sqlite3_last_insert_rowid 之前调用它。

有关详细信息,请查看 sqlite3_step 的文档,以及始终检查此类函数返回的错误代码。

如果没有发生错误,请不要调用sqlite3_errormsg,在这种情况下其结果是未定义的。

最后,如果您没有在某处保留该语句的句柄,则需要对该语句调用 sqlite3_finalize 。否则你会遇到资源泄漏并最终崩溃。 sqlite3_reset 不会释放资源,它只是“清理”语句,以便您可以使用不同的绑定值重新运行它。

Your code is missing a call to the sqlite3_step. You need to call that after having prepared the statement and bound the appropriate variables, and before you call sqlite3_last_insert_rowid.

Check the documentation for sqlite3_step for details, and always check the error codes returned by this type of function.

Don't call sqlite3_errormsg if no error has occurred, its result is undefined in that case.

Finally, you need to call sqlite3_finalize on the statement if you're not keeping a handle to it somewhere. Otherwise you'll get resource leaks and eventually crash. sqlite3_reset does not free the resources, it just "cleans" the statement so you can re-run it with different bound values.

夏末的微笑 2024-11-04 03:43:47

在准备 sqlite 语句/查询之前,您需要打开与数据库的连接...

sqlite3* database;
NSString pathToDatabase = @"/blah/blah/database.db";
const char* pathToDatabaseUTF8 = [databasePath UTF8String];
databaseReturnCode = sqlite3_open(pathToDatabaseUTF8, &database);

请参阅 sqlite 文档以获取 sqlite3_open 方法了解更多信息。

哦,别忘了关闭该连接......

int sqlite3_close(database);

Before preparing your sqlite stament/query you need to open the connection to your database...

sqlite3* database;
NSString pathToDatabase = @"/blah/blah/database.db";
const char* pathToDatabaseUTF8 = [databasePath UTF8String];
databaseReturnCode = sqlite3_open(pathToDatabaseUTF8, &database);

See the sqlite documentation for the sqlite3_open method for more info.

Oh, and don't forget to close that connection...

int sqlite3_close(database);
丑丑阿 2024-11-04 03:43:47

就我而言,它是一个包含一些特殊字符的字符串,例如(')单个冒号,我将其替换为(“”)空格,然后它开始工作。 [[[items objectAtIndex:locBtn.tag]valueForKey:@"description"] stringByReplacingOccurrencesOfString:@"'" withString:@""]

In my case it was a the string contained some special characters like it was ( ' ) single colon i replaced it with (" ") a space and it start working. [[[items objectAtIndex:locBtn.tag]valueForKey:@"description"] stringByReplacingOccurrencesOfString:@"'" withString:@""]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文