数据未插入sqlite数据库 [英] Data not inserting into sqlite database

查看:131
本文介绍了数据未插入sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的应用程序中有数据要插入我的 sqlite 数据库。所以我已经成功创建了数据库,但问题是它没有将数据插入数据库。

Hi in my application I have data which want insert into my sqlite database .So I have already created the database successfully, But the problem its not insert data into database.

我的数据库创建代码。

 - (void)createDatabase
  {

     NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *doctumentsDirectory = [directories lastObject];
     self.databasePath = [[NSString alloc] initWithString:[doctumentsDirectory stringByAppendingPathComponent:@"/bp.sqlite"]];

     NSFileManager *fileManager = [NSFileManager defaultManager];

   // create DB if it does not already exists
   if (![fileManager fileExistsAtPath:self.databasePath]) {

       const char *dbPath = [self.databasePath UTF8String];

    if (sqlite3_open(dbPath, &_myDataBase) == SQLITE_OK) {

        char *errorMsg;
        const char *sql_statement = "CREATE TABLE IF NOT EXISTS br (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, COST TEXT,QUTY TEXT)";

        if (sqlite3_exec(_myDataBase, sql_statement, NULL, NULL, &errorMsg) != SQLITE_OK)    {

            [self errorCreatingTable:[NSString stringWithFormat:@"failed creating table. ERROR:%s", errorMsg]];
         }

          sqlite3_close(_myDataBase);

      } else {

         [self errorCreatingTable:@"failed openning / creating table"];
      }
   }
 }

我的插入代码我有UIButton动作方法,我给我的插入代码。

My insert code i have UIButton action method where i give my insert code.

  -(void)aMethod2:(UIButton*)sender
 {

    NSString *inr=[intarr objectAtIndex:sender.tag];
    NSLog(@"%@",inr);
    NSString *item=[self.menuarray objectAtIndex:sender.tag];
    NSLog(@"%@",item);
    NSString *cost=[self.menuarray1 objectAtIndex:sender.tag];
    NSLog(@"%@",cost);
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO br ('NAME','COST','QUTY') VALUES ('%@','%@','%@')",item,cost,inr];
    NSLog(@"%@",sql);
 }

我使用上面的代码获取数据并插入我的数据库但是所有传递正确的值问题是它没有插入我的数据库请告诉我如何解决这个问题我已经被困在这里很长时间帮助我了。

I have used the above code where get the data and inserting into my database but values all passing correctly problem is its not inserting into my database please tell me how to resolve this issue I have been stuck here for long time help me out.

谢谢。

推荐答案

试试这段代码......

try this code...

-(void)Insertdata:(NSString*)query{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"bp.sqlite"];
    NSLog(@"%@",databasePath);
    if(sqlite3_open([databasePath UTF8String],&_myDataBase) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat: @"%@",query];

        char *errmsg=nil;

        if(sqlite3_exec(_myDataBase, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
        {
            NSLog(@".. Inserted ..");
        }
    }
    sqlite3_close(_myDataBase);
}

这篇关于数据未插入sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆