I am making an iphone app, in which I am using sqlite. In one class when I tried to insert a string it get crash and giving the following error
-[CFString UTF8String]: message sent to deallocated instance 0xe555220
I don't know Why it is happening.
Following is the insert query which I am implementing.
-(void)againinsert
{
sqlite3_stmt *addStmt = nil;
if(addStmt == nil)
{
NSString *str = #"don't know";
const char *sql = "insert into medicationdetail1(doctorname, emailid, medicationname, status) values (?,?,?,?)";
if(sqlite3_prepare_v2(database, sql , -1, &addStmt, NULL) != SQLITE_OK)
{
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [comparedoctorname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [globalemail UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [comparemedic UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 7, [str UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
{
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
}
}
When the breakpoint moves from second to third statement it get crash.
Please provide the solution.
Thanku very much.
use this one
sqlite3_bind_text(addStmt, 3, [comparemedic UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [str UTF8String], -1, SQLITE_TRANSIENT);
USE
sqlite3_bind_text(addStmt, 1, [comparedoctorname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [globalemail UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [comparemedic UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [str UTF8String], -1, SQLITE_TRANSIENT);
Related
I have sqlit data base i want to insert data in data base problem is that when i insert data it shows following error in insert query.
- (void) addCoffee {
if(addStmt == nil) {
const char *sql = "insert into library(userID,contentAddedDateTime,contentType,contentTitle,contentSource,contentDescription,categoryTitle,subCategoryTitle,organizationCode,userName) Values(?,?,?,?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [userID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [contentType UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 6, [contentTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 7, [contentSource UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 8, [contentDescription UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 9, [categoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 10, [subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 11, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
coffeeID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
Error which shows is:
NSInternalInconsistencyException', reason: 'Error while creating add statement. 'out of memory''
You need to open your database before querying it.
Open you database using sqlite3_open().
Have this line before sqlite3_prepare_v2.
if (sqlite3_open([databaseFilePath UTF8String], &database) == SQLITE_OK)
Also make use of sqlite3_finalize(statement); and sqlite3_close(database); once you done inserting data.
This is my code:
- (void) addOrder {
if(addStmt == nil) {
const char *sql = "insert into item(menuid,itemName,price,quantity,spiciness) Values(?, ?, ?, ?, ?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(addStmt, 1, [menuID integerValue]);
sqlite3_bind_text(addStmt, 2, [itemName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(addStmt, 3, price );
sqlite3_bind_int(addStmt, 4, [quantity integerValue]);
sqlite3_bind_text(addStmt, 5, [spiciness UTF8String],-1,SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
menuID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
My menuID is NSdecimalNumber.In the else portion
menuID = sqlite3_last_insert_rowid(database); I got error as "Implicit conversion of sqlite3_int64 to NsdecimalNumber is disallowed with ARC.
How could I remove this error?Please help..
As you said that menuID is NSDecimalNumber than It should be like,
menuID = [NSDecimalNumber numberWithInt:sqlite3_last_insert_rowid(database)];
As it appears to be a 64 bit int, and iOS is a 32 bit system, you might want to use:
menuID = [NSDecimalNumber numberWithLongLong:sqlite3_last_insert_rowid(database)];
This is my code :
- (void) addOrder {
if(addStmt == nil) {
const char *sql = "insert into item(menuid,itemName,price,quantity,spiciness) Values( ?, ?, ?, ?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
// NSLog(#"ADDSTMT:%#",addStmt);
sqlite3_bind_int(addStmt, 1, [menuID integerValue]);
sqlite3_bind_text(addStmt, 2, [itemName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(addStmt, 3, price );
sqlite3_bind_int(addStmt, 4, [quantity integerValue]);
sqlite3_bind_text(addStmt, 5, [spiciness UTF8String],-1,SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
menuID = [NSDecimalNumber numberWithLongLong:sqlite3_last_insert_rowid(database)];
//Reset the add statement.
sqlite3_reset(addStmt);
}
Here data is inserted into DB but got the error "Constraint Failed"
hi may b this code will help you. insert data with this style.
sqlite3_stmt *stment;
stment =nil;
NSString *sql=[NSString stringWithFormat:#"insert into DateSave(Date,Name) Values('%#','%#')",Date,strTestName];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stment, NULL) == SQLITE_OK)
{
sqlite3_step(stment);
testID =sqlite3_last_insert_rowid(database);
}
}
sqlite3_finalize(stment);
sqlite3_close(database);
iam developing one application.In that i plce the images in 3 views.And first 2 views are worked well.But in third view images are not saved in database.Below one is my code for updating the images.
-(void)updateStaff:(DataBaseFields*)databasefields
{
if(stfupdateStmt == nil) {
const char *sql = "update FoodBlogger Set update_date_time = ?, Stf_Mgr_Image = ?, Stf_Mgr_Text = ?, Stf_Srvr_Image = ?, Stf_Chf_Image = ?, Stf_Owr_Image= ? Where insert_date_time = ?";
if(sqlite3_prepare_v2(database, sql, -1, &stfupdateStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(stfupdateStmt, 1, [databasefields.update_datetime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(stfupdateStmt, 2, [databasefields.mgrImage bytes], [databasefields.mgrImage length], SQLITE_TRANSIENT);
sqlite3_bind_blob(stfupdateStmt, 3, [databasefields.srvrImage bytes], [databasefields.srvrImage length], SQLITE_TRANSIENT);
sqlite3_bind_blob(stfupdateStmt, 4, [databasefields.chfImage bytes], [databasefields.chfImage length], SQLITE_TRANSIENT);
sqlite3_bind_blob(stfupdateStmt, 5, [databasefields.owrImage bytes], [databasefields.owrImage length], SQLITE_TRANSIENT);
sqlite3_bind_text(stfupdateStmt, 6, [databasefields.insert_datetime UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(stfupdateStmt))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_reset(stfupdateStmt);
}
And image varaible in database type is varchar.Before 2 views are worked correctly.But this view was not working.So please help me how to solve this one.
Hello I'm trying to save string into SQLite database, but nothing happens. I have a string:
"tret44404%40ukr.net%3A100001740940428%3A06Kz8p1Xbk0PzpH%2F%3A0%3Av_1%2Cajax_1%2Cwidth_320%2Cpxr_1%2Cgps_1%3A1307716802"
I'm trying to save it to database but it saving string as:
"tret44404
80792kr.net-0X1.FD428P+01000017409404280X1.B2C260000001P-100906Kz8p1Xbk0PzpH0.0000000X1.149242P-104700X1.400000018P-1040v_1灠ajax_1Ῐwidth_320"
A method how I save string:
- (void) addCookie {
if(addStmt == nil) {
const char *sql = "insert into FaceBookCookies(userId, created, domain, Expires, HttpOnly, Name, Path, Value) Values(?, ?, ?, ?, ?, ?, ?, ?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(addStmt, 1, userID);
sqlite3_bind_int(addStmt, 2, created);
sqlite3_bind_text(addStmt, 3, [domain UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_double(addStmt, 4, [Expires timeIntervalSince1970]);
sqlite3_bind_text(addStmt, 5, [HttpOnly UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(addStmt, 6, [Name UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(addStmt, 7, [Path UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(addStmt, 8, [Value UTF8String], -1, SQLITE_STATIC); //here is a String...
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
userID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
Please help how to fix it...
You must probably remove the percent escapes before saving. Do it like this,
sqlite3_bind_text(addStmt, 8, [[Value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] UTF8String], -1, SQLITE_STATIC);