开发者

data is not being inserted from this insert query

开发者 https://www.devze.com 2023-04-07 22:00 出处:网络
I am using this code to insert the data in my database but it is not working.. My data is not being inserted in the table ..

I am using this code to insert the data in my database

but it is not working..

My data is not being inserted in the table ..

what can be the problem??

in function.h

 +(BOOL)insertStudentinfoData:(NSString *)first_name last_name:(NSString *)last_name phone_num:(NSString *)phone_num;

in function.m

 +(BOOL)insertStudentinfoData:(NSString *)first_name last_name:(NSString *)last_name phone_num:(NSString *)phone_num
  {
    NSString *sql = [NSString stringWithFormat:@"insert into add_data   
    values(NULL,'%@','%@','%@')",first_name,last_name,phone_num];
    return [DBOperation executeSQL:sql];
 }

And I am giving the data from this

[Function insertStudentinfoData:@"hello" last_name:@"w3qrq" phone_num:@"efew"];

but my data is not being inserted in the table

////In DBOperation.h

    +(BOOL) executeSQL:(NSString *)sqlTmp {

if(conn == SQLITE_OK) {
    NSLog(@"\n\n%@",sqlTmp);       

    const char *sqlStmt = [sqlTmp cStringUsingEncoding:NSUTF8StringEncoding];
    sqlite3_stmt *cmp_sqlStmt1;
    int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt1, NULL);

    returnValue == SQLITE_OK ?  NSLog(@"\n Inserted \n") :NSLog(@"\n Not Inserted \n");

    sqlite3_step(cmp_sqlStmt开发者_开发知识库1);
    sqlite3_finalize(cmp_sqlStmt1);

    if (returnValue == SQLITE_OK) {
        return TRUE;
    }
}
return FALSE;

}


I guess you are passing the NULL value for PRIMARY KEY, first reset the simulator

if you are not specifying the column name and inserting the values then you should pass the value for each column in particular order of column created otherwise its a good idea to specify column

NSString *sql = [NSString stringWithFormat:@"INSERT INTO add_data 
                     (first_name,last_name,phone_num) VALUE('%@','%@','%@')",first_name,last_name,phone_num];

or

NSString *sql =[NSString stringWithFormat:@"INSERT INTO add_data 
(first_name,last_name,phone_num,email,address,city,zip) VALUES 
('%@','%@','%@','%@','%@','%@','%@');",first_name,last_name,phone_num,email,addr‌​ess,city,zip];


are you sure that the database is initialized correctly? I always had trouble with the DB not actually there. Which DB Framework are you using?

[EDIT] do you see this log statement? NSLog(@"\n\n%@",sqlTmp);
Did you write the DBOperations class yourself? I think there is some issue there with the static variable you're using. I'd suggest to either use an existing db framework like fmdb or something, or else modify your class so that it uses the Singleton Pattern.

Oh, and one thing: Are you calling all methods from the same thread? SQlite DBs are not thread safe!

[edit2] You can use the link provided here: SQLite3 error - iOS to check what the value in returnValue actually states - and then figure out the underlying error. It's probably best for now to modify your not inserted statement like this:

 returnValue == SQLITE_OK ?  NSLog(@"\n Inserted \n") :NSLog(@"\n Not Inserted with code: %i\n",returnValue);


What if you provide the names of the columns to be inserted:

                 insert into T (foo, bar, baz)
                 values( ....)


Try it this way to see full error code and message:

The sql comes as NSString *, do not convert it into a cstring.

int rc;
sqlite3_stmt *sel;

if ((rc = sqlite3_prepare_v2(db_handle, [sql UTF8String], -1, &sel, NULL)) != SQLITE_OK) {
    NSLog(@"cannot prepare '%@': %d (%s)", sql, rc, sqlite3_errmsg(db_handle));
    return;
}

if (sqlite3_step(sel) != SQLITE_DONE) {
    NSLog(@"error insert/update: '%s'", sqlite3_errmsg(db_handle));
...
0

精彩评论

暂无评论...
验证码 换一张
取 消