-1

当我在 SQLite 数据库中插入数据时,我的应用程序崩溃了。

我在 SQLite 数据库中插入数据的代码是

for (int i=0; i<[arrNewFriendsId count]; i++) {

NSString *stQuery=[NSString stringWithFormat:@"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%@','%@')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];
    NSLog(@"Query registered NewFriendsTable : %@",stQuery);
    if (sqlite3_open([appdelegate.databasePath UTF8String], &database) == SQLITE_OK) {

        const char *sql =[stQuery cStringUsingEncoding:NSASCIIStringEncoding];

                    sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL);

        if(sqlite3_step(selectstmt)==SQLITE_DONE)
        {
            sqlite3_bind_text(selectstmt, 1, [[arrNewFriendsId objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(selectstmt, 3, [[arrNewFriendsFullName objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
            NSLog(@"insert successfully NewFriendsTable ");
        }
        else
        {
            NSLog(@"insert not successfully NewFriendsTable ");

        }
        sqlite3_finalize(selectstmt);
    }

    sqlite3_close(database);


}}  

在上面的代码中,应用程序在这一行崩溃了

sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL);

插入的数据来自 Facebook,插入查询是

INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('100000564063540','Rãhüł Pâtęł') 

我不明白这个问题。
我认为这与Rãhüł Pâtęł. 因为它不是常规字符。

但也不知道这是确切的问题。
任何建议、教程、代码、链接都会有很大帮助。

4

4 回答 4

1
  1. 不要使用NSASCIIStringEncoding,尤其是非 ASCII 字符。与 SQLite 一起使用的所有字符串都必须是 UTF-8。
  2. 要使用参数,您实际上必须在查询中具有参数标记:

    "INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES (?,?)"
    

    (这会自动消除格式问题。)

  3. 必须检查 的返回值sqlite3_prepare_v2
  4. 两个参数号是1and 2,not 1and 3
  5. 你必须先打电话sqlite3_bind_text ,然后再打电话sqlite3_step
于 2013-10-09T11:10:32.607 回答
1

问题在您的代码中。

你为什么使用:

sqlite3_bind_text(selectstmt, 1, [[arrNewFriendsId objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectstmt, 3, [[arrNewFriendsFullName objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);

因为您已经在查询本身中有值。

您需要删除这些语句。

像这样使用:

for (int i=0; i<[arrNewFriendsId count]; i++)
{
    NSString *stQuery=[NSString stringWithFormat:@"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%@','%@')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];
    NSLog(@"Query registered NewFriendsTable : %@",stQuery);
    if (sqlite3_open([appdelegate.databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql =[stQuery UTF8String];
        if(sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL) == SQLITE_OK)
        {
           if(sqlite3_step(selectstmt)==SQLITE_DONE)
           {
               NSLog(@"insert successfully NewFriendsTable ");
           }
           else
           {
              NSLog(@"insert not successfully NewFriendsTable ");
           }
           sqlite3_finalize(selectstmt);
        }
        sqlite3_close(database);
    }
} 
于 2013-10-09T11:11:31.183 回答
1

替换cStringUsingEncoding:NSASCIIStringEncodingUTF8String

const char *sql =[stQuery UTF8String];  

不错的数据库教程

于 2013-10-09T11:31:39.087 回答
0

%@在下面的行中使用 facebook id,它是String. 我希望,您在数据库中的 facebook id 字段类型不是integer

 NSString *stQuery=[NSString stringWithFormat:@"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%@','%@')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];

如果您的 facebook id 字段不是,您可以在下面尝试integer

 sqlite3_bind_text(selectstmt, 1,[NSString stringWithFormat:@"%@",[[arrNewFriendsId objectAtIndex:i] UTF8String]], -1, SQLITE_TRANSIENT);
 sqlite3_bind_text(selectstmt, 3, [NSString stringWithFormat:@"%@",[[arrNewFriendsFullName objectAtIndex:i] UTF8String]], -1, SQLITE_TRANSIENT);
于 2013-10-09T11:23:50.847 回答