Iphone 如何将值插入sqlite3数据库表?

Iphone 如何将值插入sqlite3数据库表?,iphone,sqlite,insert-query,Iphone,Sqlite,Insert Query,我正在写一个简单的查询,如下所示 const char*sql=“插入abc(名称)值('Royal')” 这将在我的“名字”中插入每次“Royal”,所以现在我希望每次都从用户那里获取输入作为酒店的名字,并希望保存它们而不是“Royal”,那么我该怎么做呢 如果你不清楚我的问题,你可以再问我一次,,,,,,你好 如果您在userinput文本字段中获取用户输入 NSString *qry=[NSString stringwithformat:@"insert into abc(name) va

我正在写一个简单的查询,如下所示

const char*sql=“插入abc(名称)值('Royal')”

这将在我的“名字”中插入每次“Royal”,所以现在我希望每次都从用户那里获取输入作为酒店的名字,并希望保存它们而不是“Royal”,那么我该怎么做呢

如果你不清楚我的问题,你可以再问我一次,,,,,,

你好 如果您在userinput文本字段中获取用户输入

NSString *qry=[NSString stringwithformat:@"insert into abc(name) values (\"%@\")",userinput.text]; const char *sql = [qry UTF8string]; sqlite3 *contactDB; const char *dbpath = [databasePath UTF8String]; // Convert NSString to UTF-8 if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) == SQLITE_OK) { // SQL statement execution succeeded } } else { //Failed to open database } NSString*qry=[NSString stringwithformat:@“插入abc(名称)值(\“%@”)中”,userinput.text]; const char*sql=[qry UTF8string]; sqlite3*contactDB; const char*dbpath=[databasePath UTF8String];//将NSString转换为UTF-8 if(sqlite3\u打开(dbpath和contactDB)==SQLITE\u正常) { if(sqlite3执行(contactDB、sql stmt、NULL、NULL和errMsg)==SQLITE\u OK) {//SQL语句执行成功 } }否则{ //无法打开数据库 }
您需要连接到数据库

- (sqlite3 *)database {
    if (nil == db) {
        NSString *path = <path to your database>;
        int res = sqlite3_open([path UTF8String], &db);
        if (res != SQLITE_OK){
            // handle the error.
            db = nil;
            return nil;
        }
    }
    return db;
}
-(NSArray*)执行选择:(NSString*)查询{
//数据搜索块****************************************
char*zErrMsg;
字符**结果;
int nrow,ncol;
sqlite3_获取_表(
[自数据库],/*开放式数据库*/
[查询UTF8String],/*要执行的SQL*/
&result,/*result写入指向的char*[]中*/
&nrow,/*此处写入的结果行数*/
&ncol,/*此处写入的结果列数*/
&zErrMsg/*此处写入错误消息*/
);
NSMutableArray*returnArray=[NSMutableArray阵列容量:3];

对于(inti=0;i,在sqlite3表中插入值的代码非常简单

 -(void)writeValueInSettings:(NSMutableArray *)arrayvalue

   {   


  if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)

     {
     database *objectDatabase=[[database alloc]init];

     NSString *stringvalue2=[objectDatabase countValue];

    [objectDatabase release];

     NSLog(@"opened");
     NSString *sql1;

sql1=[[NSString alloc] initWithFormat:@"insert into setting values('%i','%i','%i','%@','%i','%i','%@','%i','%i','%i','%i','%i','%i','%@');",intvalue1,
        [[arrayvalue objectAtIndex:0] intValue],[[arrayvalue objectAtIndex:1] intValue],[arrayvalue objectAtIndex:2],[[arrayvalue objectAtIndex:3] intValue],[[arrayvalue objectAtIndex:4]intValue ],[arrayvalue objectAtIndex:5],[[arrayvalue objectAtIndex:6]intValue],[[arrayvalue objectAtIndex:7]intValue ],[[arrayvalue objectAtIndex:8] intValue],[[arrayvalue objectAtIndex:9] intValue],[[arrayvalue objectAtIndex:10]intValue ],[[arrayvalue objectAtIndex:11]intValue],[arrayvalue objectAtIndex:12]];
    char *err1; 
    if (sqlite3_exec(myDatabase,[sql1 UTF8String],NULL,NULL,&err1)==SQLITE_OK)
    {
        NSLog(@"value inserted:");

         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Attention" message:@"You  inserted successfully" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
       [alert release];
    }
    [sql1 release];
    sqlite3_close(myDatabase);
  }

  }

db是一个实例变量`sqlite3 db;`它在哪里给你这个错误。我不确定,但我认为这意味着它试图在某个地方做一个二进制模数'%'。下面是一个示例查询`[sqlContext executeQuery:[NSString stringWithFormat:@“Insert into abc(name)values('%@');”,hotel]];`我的sqlite3是作为“sqlite3*数据库”的数据库@Astaln Inpoi,但我已将输入字符串保存为NSString名称为“restaurant\u name\u save”,因此,现在当我只有NSString并希望插入时,该怎么办?它因以下控制台错误而崩溃,,,,,[NSString stringwithformat:]:发送到类0x1b3800的无法识别的选择器“您必须对方法名称使用正确的大小写”`[NSString stringWithFormat:``我的数据库是abc.sql,“abc”是表名,如果这些是名称,您的代码会是什么样子?@Rich,我的数据库是“abc.sql”,我的表名是abc和“name”名称是我以NSString格式插入数据的地方,名为restaurant\U name\U save,现在告诉我根据名称进行的确切编码你想找到路径吗?@Rich不,我想将save just NSString放入我的表中,我在前面的评论中,在“-(sqlite3)中告诉了你一切(关于表名和其他内容)数据库`如果abc.sql在您的资源文件夹中,您可以使用`[[NSBundle mainBundle]pathForResource:@“abc”的类型:@“sql”]`获取路径;如果您创建一个类来包含我提供的方法,并在声明为'restaurant\u name\u save'的类中创建一个名为sqlContext的实例,您可以这样做来保存restaurant name`[sqlContext executeQuery:[NSString stringWithFormat:restaurant_name_save,nameToSave]];`假设restaurant_name_save看起来像这样`NSString*const restaurant_name_save=@“插入abc(名称)值('%@');”`
-(NSArray *) executeSelect:(NSString *)query {
//Data search block****************************************

char *zErrMsg;
char **result;
int nrow, ncol;



sqlite3_get_table(
                  [self database],              /* An open database */

                  [query UTF8String],       /* SQL to be executed */
                  &result,       /* Result written to a char *[]  that this points to */
                  &nrow,             /* Number of result rows written here */
                  &ncol,          /* Number of result columns written here */
                  &zErrMsg          /* Error msg written here */
                  );


NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:3];



for (int i=0; i<nrow; i++){
    [returnArray addObject:[NSString stringWithUTF8String:result[ncol + i]]];

}

sqlite3_free_table(result);

return returnArray;
 -(void)writeValueInSettings:(NSMutableArray *)arrayvalue

   {   


  if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)

     {
     database *objectDatabase=[[database alloc]init];

     NSString *stringvalue2=[objectDatabase countValue];

    [objectDatabase release];

     NSLog(@"opened");
     NSString *sql1;

sql1=[[NSString alloc] initWithFormat:@"insert into setting values('%i','%i','%i','%@','%i','%i','%@','%i','%i','%i','%i','%i','%i','%@');",intvalue1,
        [[arrayvalue objectAtIndex:0] intValue],[[arrayvalue objectAtIndex:1] intValue],[arrayvalue objectAtIndex:2],[[arrayvalue objectAtIndex:3] intValue],[[arrayvalue objectAtIndex:4]intValue ],[arrayvalue objectAtIndex:5],[[arrayvalue objectAtIndex:6]intValue],[[arrayvalue objectAtIndex:7]intValue ],[[arrayvalue objectAtIndex:8] intValue],[[arrayvalue objectAtIndex:9] intValue],[[arrayvalue objectAtIndex:10]intValue ],[[arrayvalue objectAtIndex:11]intValue],[arrayvalue objectAtIndex:12]];
    char *err1; 
    if (sqlite3_exec(myDatabase,[sql1 UTF8String],NULL,NULL,&err1)==SQLITE_OK)
    {
        NSLog(@"value inserted:");

         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Attention" message:@"You  inserted successfully" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
       [alert release];
    }
    [sql1 release];
    sqlite3_close(myDatabase);
  }

  }