This is not working. When I execute this I get one message, EXC_BAD_ACCESS.
-(void) adddata{
NSString *event=#"max";
NSString *venue=#"tvm";
NSString *edate=#"may 6";
NSString *etime=#"10:30";
int admts=5;
NSString *ima=#"TTTTTTTTT";//ticobj.iimg;
sqlite3 *database;
databaseName = #"smbhDB.sql";
sqlite3_stmt *addStatement=nil ;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
const char *sql ="insert into tickets (admittance, venue, event, date, time, imagedata) Values (?,?,?,?,?,?)";
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"444 Error while creating add statement. '%s'", sqlite3_errmsg(database));
sqlite3_bind_int(addStatement,1,admts);
printf("\n\n inthe add ticket ");
sqlite3_bind_text(addStatement, 2, [venue UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 3, [event UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 4, [edate UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 5, [etime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 6, [ima UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStatement))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(addStatement);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have app in which i am inserting data in sqlite table i want that if data is already inserted then it should not insert same record here is my code for inserting data.
here is code with insert statement for inserting data
- (void) addCoffee:(NSString *)dbPath {
if(addStmt == nil) {
const char *sql = "insert into library(userID,contentAddedDateTime,contentType,contentTitle,contentSource,contentDescription,categoryTitle,subCategoryTitle,organizationCode,userName) Values(?,?,?,?,?,?,?,?,?,?)";
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
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, 3, [contentType UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [contentTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 5, [contentSource UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 6, [contentDescription UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 7, [categoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 8, [subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 9, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 10, [userName 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);
}
- (int) GetCount :(NSString *)UserID
{
[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];
int count = 0;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"dbfav2table.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt *statement;
NSString *sql_str = [NSString stringWithFormat:#"SELECT count(*) FROM tablename WHERE userID='%#'", UserID];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"%#", [NSString stringWithUTF8String:sqlStatement]);
if( sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
{
while( sqlite3_step(statement) == SQLITE_ROW )
{
count = sqlite3_column_int(statement, 0);
NSLog(#"count for %# is %i",UserID,count);
}
}
else
{
NSLog( #" Error is: %s", sqlite3_errmsg(database) );
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(database);
}
return count;
}
if it returns count = 1 it means there is already a value with same userId exist, else there is no such value. hope it helps
You should write select query with a unique column. lets say userID in your case
const char *sql = "select * from library where userID=%#“;
if record exist than enter else skip
This question already has answers here:
sqlite3_prepare_v2 != SQLITE_OK
(2 answers)
Closed 9 years ago.
Anybody help me please.
I can't insert the data to sqlite.
the output is "News added failed".
Note :
- The database is already created.
- News is an object
I just think that the problem is in this function.
Could you tell me whats wrong with my code?
Thanks.
- (void)addNews:(NSString *)content_caption content_date:(NSString *)content_date content_desk:(NSString *)content_desk content_id:(NSString *)content_id content_img:(NSString *)content_img content_type:(NSString *)content_type content_web_url:(NSString *)content_web_url{
char *error;
if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {
NSString *inserStmt = [NSString stringWithFormat:#"INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\')",content_caption, content_date, content_desk, content_id, content_img, content_type ,content_web_url];
const char *insert_stmt = [inserStmt UTF8String];
if (sqlite3_exec(database, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
NSLog(#"News added");
News *news = [[News alloc]init];
[news setContent_caption:content_caption];
[news setContent_date:content_date];
[news setContent_desk:content_desk];
[news setContent_id:content_id];
[news setContent_img:content_img];
[news setContent_type:content_type];
[news setContent_web_url:content_web_url];
[arrayOfNews addObject:news];
}else{
NSLog(#"News added failed");
}
sqlite3_close(database);
}
}
try with this
#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
const char *insertSql;
insertSql = nil;
insertSql = "INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (?,?,?,?,?,?,?)";
if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {
sqlite3_bind_text(insert_statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [content_desk UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);
}
First you need to set path of your database . Do like this
// Setting path
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
databasepath =[docsDir stringByAppendingPathComponent: #"yourDatabase"];
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasepath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"quizdb"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasepath error:&error];
}
//Next you have to insert as follows
sqlite3_stmt *statement;
const char *dbpath=[databasepath UTF8String];
if(sqlite3_open(dbpath,&contactDB)==SQLITE_OK)
{
const char *sqlStatement = "insert into dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) VALUES (?,?,?,?,?,?,?)";
if (sqlite3_prepare_v2(contactDB,sqlStatement,-1,&statement,NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3 ,[content_desk UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);
}
if (sqlite3_step(statement)!= SQLITE_DONE)
{
NSLog(#"Save Error:%s",sqlite3_errmsg(contactDB));
}
else {
NSLog(#"success");
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
Hope it will helps you..
i am inserting data into data base using following code but data is not inserted in the database
i am using following code
I am inserting data into iphone application but it does not insert
i have table survey_question_master(question_id,question_text,question_type)
- (void) addCoffee {
if(addStmt == nil) {
const char *sql = "insert into survey_question_master(question_text,question_type) 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, [question_text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [question_type UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
question_ID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
- (void) addCoffee:(DataController *)coffeeObj {
//Add it to the database.
[coffeeObj addCoffee];
//Add it to the coffee array.
[coffeeArray addObject:coffeeObj];
NSLog(#"Succeessfully Added");
}
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"database.sqlite"];
}
-(void)SaveData:(NSMutableArray *)insertDataArray
{
sqlite3_stmt *stmt;
const char *dbPath = [[self getDBPath] UTF8String];
if(sqlite3_open(dbPath, &database)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"insert into items (c1,c2,c3,c4 , c5 ,c6,c7,c8,c9) values (\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\",\"%#\")",[insertDataArray objectAtIndex:0],[insertDataArray objectAtIndex:1],[insertDataArray objectAtIndex:2],[insertDataArray objectAtIndex:3],[insertDataArray objectAtIndex:4],[insertDataArray objectAtIndex:5],[insertDataArray objectAtIndex:6],[insertDataArray objectAtIndex:7],[insertDataArray objectAtIndex:8]];
NSLog(#"%#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &stmt, NULL);
if(sqlite3_step(stmt)==SQLITE_DONE)
{
NSLog(#"insert success");
}
else
{
NSLog(#"insert un success");
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
int success=sqlite3_step(stmt);
if (success == SQLITE_ERROR)
{
NSAssert1(0, #"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
} sqlite3_finalize(stmt);
sqlite3_close(database);
}
}
I had used this code to write enteries into database but it is no refelecting into it...I don't know why?? Please give your suggestions!!Thanks in advance..
NSString* user_Name=txt_UserName.text;
NSString* password=txt_Password.text;
NSString* rePassword=txt_RePassword.text;
if ([password isEqualToString:rePassword]) {
sqlite3 *database;
sqlite3_stmt *compiledQuery;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "insert into Main values(?,?)";
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledQuery, NULL);
sqlite3_bind_text(compiledQuery, 1, [user_Name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledQuery, 2, [password UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledQuery))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_finalize(compiledQuery);
sqlite3_close(database);
}
is the path to the database is correct ? You can check the database path by
NSString *documentsDir = [DbCls getPath];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:DBNAME];
+(NSString *) getPath
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
return documentsDir;
}
NSString *event=#"max";
NSString *venue=#"tvm";
NSString *edate=#"may 6";
NSString *etime=#"10:30";
int admts=5;
NSString *ima=ticobj.iimg;
sqlite3 *database;
databaseName = #"smbhDB.sql";
sqlite3_stmt *addStatement ;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if(addStatement == nil) {
const char *sql ="insert into tickets (admittance, venue, event, date, time, imagedata) Values (?,?,?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"444 Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(addStatement,1,admts);
sqlite3_bind_text(addStatement, 2, [venue UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 3, [event UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 4, [edate UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 5, [etime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 6, [ima UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStatement))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, sql, -1, &Statement, NULL) == SQLITE_OK) {
sqlite3_exec(database, sql, -1, &Statement, NULL);
}
sqlite3_reset(addStatement);
Why can't I execute this code?
Where is
sqlite3_open([databasePath UTF8String], &database); ?
You can't work with a closed database.
-(BOOL)insertDatabaseValue:(SignIn *)objFavourie isUpdate:(BOOL)update
{
BOOL returnValue = YES;
sqlite3_stmt *insertStmt = nil;
sqlite3 *database = nil;
if (sqlite3_config(SQLITE_CONFIG_SERIALIZED) == SQLITE_OK){
returnValue = NO;
}
if (sqlite3_open([DataBasePath UTF8String], &database) == SQLITE_OK){
if(insertStmt == nil) {
NSString *aString;
if(!update)
aString =[NSString stringWithFormat:#"INSERT INTO Sanjay('Name', 'Password') Values(?, ?)"];
else
aString =[NSString stringWithFormat:#"UPDATE Sanjay SET Password = ? where 'Name'=%#",objFavourie.strName];
const char *sql = [aString UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &insertStmt, NULL) != SQLITE_OK)
{
NSLog(#"Error while creating insert :%s add statement.", sqlite3_errmsg(database));
returnValue = NO;
}
}
if(sqlite3_bind_text(insertStmt, 1, [objFavourie.strName UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK){
returnValue = NO;
}
if(sqlite3_bind_text(insertStmt, 2, [objFavourie.strPassword UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK) {
returnValue = NO;
}
if(SQLITE_DONE != sqlite3_step(insertStmt)){
NSLog(#"Error while inserting into '%s'", sqlite3_errmsg(database));
returnValue = NO;
}
sqlite3_reset(insertStmt);
if (insertStmt) {
sqlite3_finalize(insertStmt);
insertStmt = nil;
}
sqlite3_close(database);
database = nil;
}
else {
returnValue = NO;
}
return returnValue;
}