【问题标题】:Can not INSERT data to my sqlite3 database. (Iphone)无法将数据插入到我的 sqlite3 数据库中。 (苹果手机)
【发布时间】:2012-05-16 21:20:54
【问题描述】:

我在将一些数据写入我的 sqlite3 数据库时遇到问题。问题是,在应用程序在模拟器上运行期间,在 INSERT 之后,我通过 SELECT 对其进行检查,并且能够在数据库中看到我的新数据。但是在切换应用程序后,我丢失了我的数据。我认为我没有法律可以在此文件上写下数据,但我检查了它并且我拥有它。

当我在我的设备上工作时,我可以看到数据库中的数据,但是当我使用 INSERT 时没有任何反应。

有人遇到同样的问题吗?有人可以帮我解决这个问题吗?

听到的是INSERT的代码

NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO person (uniqueId, idn, heartP, sugar, data) VALUES (\"%i\", \"%@\", \"%@\", \"%@\", \"%@\");", uniqueIds, idns, heartPs, sugars, datas];

if(sqlite3_exec(database, [insertStatement UTF8String],NULL ,NULL ,nil)==SQLITE_OK){


    NSLog(@"well done");
}

}

【问题讨论】:

  • 我不知道如何使用 2 行在 sqlite 中插入。下次请发布您的完整代码,以便其他人能够理解。
  • 对不起伙计,我只添加了那部分,因为我没有询问如何创建数据库或如何打开数据库。我这样做是正确的,所以我只粘贴有人可能会看到问题的那部分。

标签: iphone database sqlite insert


【解决方案1】:

我认为您的数据库没有复制到文档目录中。

【讨论】:

  • 是的,不是,这可能是我问题第二部分的答案。
【解决方案2】:

请检查此代码并检查您犯了什么错误。我正在使用此代码,只有这对我有用。

sqlite3_stmt    *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &adddata) == SQLITE_OK)
{
    // NSLog(@"open");
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO contacts(FirstName,LastName,Company,Mobile,Home,Url,Street,City,State,Zip,Prefix,Suffix,Middle,JobTitle,Birthday,Note) VALUES (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", firstName.text,lastName.text,companyName.text,mobileNumber.text,homeNumber.text,homeUrl.text,street.text,city.text,state.text,zip.text,prefix.text,suffix.text,middle.text,jobTitle.text,birthday.text,note.text];

    const char *insert_stmt = [insertSQL UTF8String];

    int c=sqlite3_prepare_v2(adddata, insert_stmt, -1, &statement, NULL);
    NSLog(@"%d",c);
    if (sqlite3_step(statement)==SQLITE_DONE)
    {
        alert=[[UIAlertView alloc]initWithTitle:@"alert" message:@"Data is added" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];
        NSLog(@"%@",insertSQL);
        firstName.text = @"";

    } 
    else 
    {
        alert=[[UIAlertView alloc]initWithTitle:@"alert" message:@"Data is not added" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];                 
    }
    sqlite3_finalize(statement);
    sqlite3_close(adddata);
}    

【讨论】:

    【解决方案3】:

    即使您对该文件具有写入权限,也无法写入您的应用程序包。您需要将文件移动到 Library、Documents 或 tmp 目录。

    【讨论】:

      【解决方案4】:

      试试这个,

      sqlite3_stmt    *statement;
      NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO person (uniqueId, idn, heartP, sugar, data) VALUES (\"%i\", \"%@\", \"%@\", \"%@\", \"%@\");", uniqueIds, idns, heartPs, sugars, datas];
      
      sqlite3_prepare_v2(database, [insertStatement UTF8String],-1, &selectstmt, NULL);
      
             if(sqlite3_step(selectstmt)==SQLITE_DONE)
              {
                  NSLog(@"insert successfully");
              }
      

      【讨论】:

      • 它只插入语句来存储数据库中的数据
      猜你喜欢
      • 2021-10-25
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2012-06-25
      • 2013-03-02
      相关资源
      最近更新 更多