如何在我的sqlite3数据库表中插入值?

时间:2011-02-10 11:10:42

标签: iphone sqlite insert-query

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

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

这将每次'皇家'插入我的'名字',所以现在我想每次都将用户的输入作为酒店的名称,并希望保存它们而不是'皇家',所以我该怎么办?

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

4 个答案:

答案 0 :(得分:2)

此代码非常简单,可以在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);
  }

  }

答案 1 :(得分:0)

您好 如果您的用户输入 userinput textfield

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
}

答案 2 :(得分:0)

您需要连接到数据库

- (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;
}

然后你可以用查询

来调用它
    -(void)executeQuery:(NSString *)query{


        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2([self database], [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            sqlite3_step(statement);

        }else{
            // handle the error.
        }

        sqlite3_finalize(statement);
    }

-(void)dealloc {
    //close database connection
    sqlite3_close(db);
    db = nil;
    [super dealloc];
}

答案 3 :(得分:0)

-(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;

}

相关问题