动态插入sqlite表中的数据不起作用

时间:2012-02-18 14:10:39

标签: iphone objective-c sqlite

我想在点击按钮时在SQLite表登录中插入数据,问题是当我点击注册按钮时数据存储在变量中但不存储在数据库表中,并且当我尝试静态传递时数据将被插入的值,但当我试图动态存储数据时,则不起作用。

这是我的代码。请仔细阅读并帮助解决这个问题。

NSString *path; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask ,YES);

path = [paths objectAtIndex:0];

dbName = [[NSString alloc] initWithString:[path stringByAppendingPathComponent:@"mnc.sqlite"]];

NSFileManager *fileMgr = [NSFileManager defaultManager];

if([fileMgr fileExistsAtPath:dbName]== NO)
{
  const char *dbPath = [dbName UTF8String];

  if (sqlite3_open(dbPath,&connectionDB)==SQLITE_OK) 
  {
    char *errMsg;
    const char *sql_stmt = " create table if not exists login(user_name text ,password text)";

    if (sqlite3_exec(connectionDB,sql_stmt,NULL,NULL,&errMsg))
    {
      printf("\n fail to create table ");
    }

    sqlite3_close(connectionDB);
  }
  else 
  {
    NSLog(@"fail to open create database");
  }
}

[fileMgr release];

if(sqlite3_prepare_v2(connectionDB, query ,-1, &statment,NULL) == SQLITE_OK)
{
  sqlite3_bind_text(statment,1,"cc", -1,SQLITE_TRANSIENT);
  sqlite3_bind_text(statment,2,"sa",-1,SQLITE_TRANSIENT);
  NSLog(@"aadd");
}

if (sqlite3_step(statment)== SQLITE_DONE)
{
  NSLog(@"This should be real error checking!");
  sqlite3_finalize(statment);
}  

1 个答案:

答案 0 :(得分:0)

使用sqlite非常复杂,这就是为什么我使用fmdb这将简化工作。

  

这是围绕SQLite的Objective-c包装器

希望这有帮助。