0

私は以下のように簡単なクエリを書いています

const char * sql = "abc(name)値に挿入('Royal')";

これにより、毎回「Royal」が「name」に挿入されるので、毎回ユーザーからホテルの名前として入力し、「Royal」の代わりに保存したいので、どうすればよいですか?

あなたが私の質問に明確でないなら、あなたは再び私としてかもしれません、、、、、、

4

4 に答える 4

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

  }
于 2012-08-28T06:14:00.427 に答える
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;

}

于 2011-02-10T11:39:18.067 に答える
0

こんにちは、userinputテキストフィールド にユーザー入力を取得する場合

NSString * qry = [NSString stringwithformat:@ "abc(name)値に挿入(\"%@ \ ")"、userinput.text ];
const char * sql = [qry UTF8string];
sqlite3 * contactDB;

const char * dbpath = [databasePath UTF8String]; //NSStringをUTF-8に変換します

if(sqlite3_open(dbpath、&contactDB)== SQLITE_OK)
{{
if(sqlite3_exec(contactDB、sql_stmt、NULL、NULL、&errMsg)== SQLITE_OK)
 {//SQLステートメントの実行に成功しました
 }
} そうしないと {
    //データベースを開くことができませんでした
}
于 2011-02-10T11:18:06.927 に答える
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];
}
于 2011-02-10T11:19:33.373 に答える