0

I am writing a simple query as below

const char *sql = "insert into abc(name) values ('Royal')";

and this will insert each time 'Royal' into my 'name', so now I want to take input from user each time as names of hotel and wants to save them instead of 'Royal', so what should I do?

If you are not clear to my question, you may as me again,,,,,

4 Answers 4

2

this code is very simple for insert the value in sqlite3 table

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

  }
Sign up to request clarification or add additional context in comments.

Comments

0

you need to connect to a database

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

then you can call this with the query

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

4 Comments

db is an instance variable ` sqlite3 db; `
where does it give you that error. I'm not sure, but I think that it means that it tried to do a binary modulus "%" somewhere.
heres a sample query ` [sqlContext executeQuery:[NSString stringWithFormat:@"Insert into abc (name) values('%@');", hotel]]; `
my sqlite3 is database as "sqlite3 *database"
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;

}

23 Comments

@Rich, my database is "abc.sql", my table name is abc, and "name" is name where I am inserting data in format of NSString named as restaurant_name_save, now tell me exact coding according to them
do you want a way to find the path?
@Rich no, I want to take save just NSString into my table, I told U everything (regarding names of tables and other things ) in previous comment,
in ` -(sqlite3)database ` if abc.sql is in your resources folder you can get the path with ` [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"sql"]; ` if you make a class to contain the methods I've provided and make an instance of it called sqlContext, in the class that you declared ` restaurant_name_save ` you can do this to save the resturant name ` [sqlContext executeQuery:[NSString stringWithFormat:restaurant_name_save, nameToSave]]; `
assuming restaurant_name_save looks like this ` NSString *const restaurant_name_save = @"Insert into abc (name) values('%@');"; `
|
0

Hi if Your getting user inputs in 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
}

4 Comments

@Astaln enopi, but I have saved my input string as NSString name as "restaurant_name_save", so now what should I do when I have only NSString, and wants to insert?
It is crashed with this console error,,,, [NSString stringwithformat:]: unrecognized selector sent to class 0x1b3800'
you must use proper capitalization for method names ` [NSString stringWithFormat:] `
my database is abc.sql, and 'abc' is table name, so how ur code look like if these are names?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.