I am working on sqlite database , trying to insert into database but it is giving error "Error while inserting " Database is locked . I searched some post and also write reset and finalize statement but it is giving same error.
Here is my code
if(addStmt == nil)
{
//const char *sql = "insert into Limittabel (limitAmt) Values(?)";
//const char *sql="INSERT INTO Limittabel (limitAmt, Profileid) VALUES(?, ?)";
const char *sql="insert into Limittabel (limitAmt,Profileid) Values (?,?)";
// Insert into Limittabel (limitAmt,Profileid) Values (500,1ドル)
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, [limitAmt UTF8String], -1, SQLITE_TRANSIENT);
// NSString *str_id_poi = [[NSString alloc] initWithFormat:@"%d", [Profileid integerValue]];
// sqlite3_bind_text(addStmt, 2, [str_id_poi UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(addStmt, 2, [ UTF8String], -1, SQLITE_TRANSIENT);
NSLog(@"***Storing END on Database ***");
if(SQLITE_DONE != sqlite3_step(addStmt))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
} // sqlite3_finalize(addStmt);
else{
//sqlite3_last_insert_rowid;
limitid = sqlite3_last_insert_rowid(database);
NSLog(@"YES THE DATA HAS BEEN WRITTEN SUCCESSFULLY");
}
sqlite3_finalize(addStmt);
}
sqlite3_reset(addStmt);
//sqlite3_finalize(addStmt);
}
If I am not opening the database it is giving Database locked error, if opening database it is giving ' No such Table ' error Please Help me. Thanks
4 Answers 4
I don't understand the "if I am not opening the database it is giving Database locked error" comment, because you simply can't use a database without opening it. It makes no sense to call SQLite functions without first opening database. Maybe I'm not understanding this comment.
But ignoring that for a second, you later say, "if opening database it is giving 'No such table' error": Are you sure you're finding the database correctly? The problem is, if you open database and it's not found, then a blank database will be created. Are you sure this hasn't happened to you? If this may have happened to you, you might want to
Reset the simulator (if you're using simulator) or delete and reinstall the app to make sure any blank databases that
sqlite3_openmay have created for you will be removed; andCheck to make sure the database exists before you open it or replace your occurrence of
sqlite3_openwithsqlite3_open_v2with a third parameter ofSQLITE_OPEN_READWRITEand fourth parameter ofNULL. See SQLite will not prepare query when accessing database in XcodeAs an aside, you don't mention whether you're opening this from the bundle or
Documents, but generally I advise to programmatically copy from bundle toDocumentsand then open it from there.If you're doing this on the simulator, I sometimes find it helpful to examine the database used by the simulator from my Mac. Thus, I go to "~/Library/Application Support/iPhone Simulator/5.1/Applications/" (replace the 5.1 with whatever version of your simulator you are using ... if the
Libraryis hidden, unhide it fromTerminalwithchflags nohidden ~/Library), I then go into the directory for the particular application, go into it'sDocumentsfolder, and then open the database in the Mac command linesqlite3program. You can use that to examine the database and make sure the db is as you expect it.
Just a few thoughts for the "no such table" error.
1 Comment
LIBRARY just click on the Finder > Go and with option(alt) pressed will show you the LIBRARY and just click ont it will lead you to the LIBRARY :)Here is link for creating, Open , inserting, and retrieve data from the SQLite database . It will help you.
SQLite Datase Create,Insert and Retrieve from the database Click here
3 Comments
jonty to deal with the database its compulsory that first we open the database and then we makes changes in database and if it giving the error No such table then i recommend you check the existence of table in database i.e. such table created or not.
1 Comment
Try this code,
if(sqlite3_open([YourDatabasePath UTF8String],&db) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"insert into Limittabel (limitAmt,Profileid) Values (?,?)"];
char *errmsg=nil;
if(sqlite3_exec(db, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(@"YES THE DATA HAS BEEN WRITTEN SUCCESSFULLY");
}
}
sqlite3_close(db);
If this code doesn't work then there is a problem in your query, check your query again. this code works at my side.
9 Comments
querySQLand show me your query. is it proper ? or what you wrote instead of YourDatabasePath in first line ?create table query, i want query of insert statement put log of insert query. are you able to see Limittabel inside your Db ?Limittabel
Limittabelexists at your Document Directory path ?