Hey my code is very simple , i have crated a sqlite table with one column id and message.
my sqlite table is created successfully. i want to insert 5 string value in message column .my code for insert string value
- (IBAction)addTextToDatabase:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT INTO SAMPLETABLE (MESSAGE) VALUES (\"%#\")",
self.textField.text];
const char *insert_stmt = [insertSQL UTF8String];
for (int k = 0; k < 5; k++) {
sqlite3_prepare_v2(myDatabase, insert_stmt,
-1, &statement, NULL);
sqlite3_finalize(statement);
}
// sqlite3_prepare_v2(myDatabase, insert_stmt,
// -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
statusOfAddingToDB = [NSString stringWithFormat:#"Text added -- %#",
textField.text];
} else {
statusOfAddingToDB = #"Failed to add contact";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"DB Status"
message:statusOfAddingToDB delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
// sqlite3_finalize(statement);
sqlite3_close(myDatabase);
}
}
but when i check my database only one value is added to it.please help how to achieve this?
Write sqlite3_step statement within for loop before sqlite3_finalize statement
You have to run this statement:
sqlite3_step(compiledStatement) == SQLITE_DONE
after each insertion.
Related
I have some values in labels and i'm trying to store it in my database but when i hit save button it isn't save and log a message failed to save. The database is properly made but data is not storing. My code for saving data is this,
- (IBAction)btnSave:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO data (ID, ALERT, XREF,TEXT,GRAPHIC,PROMPT,VOICE) VALUES (\"%#\", \"%#\", \"%#\",\"%#\",\"%#\",\"%#\",\"%#\")", _lblID.text, _lblAlert.text, _lblxref.text,_lbltext.text, _lblgraphic.text, _lblprompt.text,_lblvoice.text];
NSLog(#"DDD %#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
_lblstatus.text = #"Contact added";
} else {
_lblstatus.text = #"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
if (sqlite3_step(statement) == SQLITE_DONE){
_lblstatus.text = #"Contact added";
} else {
NSLog(#"prepare failed: %s", sqlite3_errmsg(contactDB));
_lblstatus.text = #"Failed to add contact";
}
1. Replace this snippet in your code to print error message. It will help you to find exact issue.
2. May be the SQL query which your forming is syntactically wrong.
I am trying to insert a BLOB data of SignatureView using this code but when i actually browse the database there is null instead of data.
My signature table schema is given below.
create table sign(id integer primary key AUTOINCREMENT, image blob,invoiceid integer);
-(void)storeImageData:(NSData *)imageData withInvoiceID:(NSInteger)invoiceID{
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:#"database.sqlite3"];
const char *dbpath = [dbPath UTF8String];
sqlite3 *contactDB;
sqlite3_stmt *statement;
NSLog(#"%#",dbPath);
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
int invoiceIDINT = (int)invoiceID;
//
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO sign (image,invoiceid) VALUES (?,%d)", invoiceIDINT];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
} else {
const char *Error = sqlite3_errmsg(database);
NSString *error = [[NSString alloc]initWithUTF8String:Error];
UIAlertView *view = [[UIAlertView alloc]initWithTitle:#"Error2" message:[NSString stringWithFormat:#"Last inserted ID: %#",error] delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[view show];
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
Always check the result of sqlite3_prepare_v2. If it fails, log the problem using sqlite3_errmsg.
Only call sqlite3_finalize if sqlite3_prepare_v2 succeeds.
You get NULL for the blob because your call to sqlite3_bind_blob is passing the wrong column index. It should be 1, not 2 since your want to bind to the first ? in your INSERT statement.
Why the inconsistency? Why do you use stringWithFormat to set the value for the invoiceid column and then use sqlite_bind_xxx for the image column? You should bind both. Never use stringWithFormat to build a query.
You call sqlite3_step twice. Only call it once and bind your values before you call it.
You appear to be writing to a database inside your app's resource bundle. You can't do that on a real device. It works in the simulator but not on real iOS devices. You need to put your database file in the Documents folder.
Given all of the above, your code should be something like this:
-(void)storeImageData:(NSData *)imageData withInvoiceID:(NSInteger)invoiceID{
// This is wrong - you need to update this to use the Documents folder
NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"database.sqlite3"];
const char *dbpath = [dbPath UTF8String];
NSLog(#"%#",dbPath);
sqlite3 *contactDB;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
const char *insert_stmt = "INSERT INTO sign (image, invoiceid) VALUES (?, ?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 2, (int)invoiceID);
if (sqlite3_step(statement) == SQLITE_DONE) {
// Row inserted successfully
} else {
const char *Error = sqlite3_errmsg(contactDB);
NSString *error = [[NSString alloc] initWithUTF8String:Error];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:#"Error2" message:[NSString stringWithFormat:#"Last inserted ID: %#",error] delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[view show];
}
sqlite3_finalize(statement);
} else {
NSLog(#"Unable to prepare statement %s: %s", insert_stmt, sqlite3_errmsg(contactDB));
}
sqlite3_close(contactDB);
} else {
NSLog(#"Unable to open database at path %#: %s", dbPath, sqlite3_errmsg(contactDB));
}
}
I am trying to insert a member in sqlite DB member table. After inserting values if I take sqlite3_last_insert_rowid() I can't insert another member. the statement shows SQLITE_BUSY.Here is my code. Please anybody help.
-(NSInteger) saveMember:(TMMember *)member {
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"insert into members (memberName, memberAmount,shareFlag) values(\"%#\", \"%f\",%d)",member.memberName,member.amount,[[NSNumber numberWithBool:member.shareFlag]intValue]];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
NSInteger lastRowId = sqlite3_last_insert_rowid(database);
member.memberId = lastRowId;
NSLog(#"inserted member id = %ld",lastRowId);
NSLog(#"member is added");
}
sqlite3_finalize(statement);
statement = nil;
}
sqlite3_reset(statement);
sqlite3_close(database);
return 0;
}
This error is getting when sqlite already processing another statement, and you are trying to execute another one. So the db is locked until you finalise the statement.
For more info. Read: SQLite Exception: SQLite Busy
I have a "messages table" , and i want only to retrieve the "user ID" with his last message.
I tried to add "2 sql statements" inside each other , But it keeps on looping without stopping,
sqlite3_stmt *statement;
NSMutableArray * messages = [[NSMutableArray alloc]init];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_chatDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT DISTINCT FROMID , USERNAME from CHATCOMPLETE"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_chatDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
int userID = [[[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)] integerValue];
NSString *querySQL2 = [NSString stringWithFormat:
#"SELECT MESSAGE , USERNAME from CHATCOMPLETE where FROMID=\"%d\"",userID];
const char *query_stmt2 = [querySQL2 UTF8String];
if (sqlite3_prepare_v2(_chatDB,
query_stmt2, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"LAST MESSAGE %#",[[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)]);
sqlite3_reset(statement);
}
}
}
sqlite3_reset(statement);
}
}
return messages;
UPDATE:
This is the insert message
-(void)saveData:(NSString *)message toID:(int)toID fromID:(int)fromID isRead:(BOOL)read date:(NSDate *)date messageID:(int)messageID userName:(NSString*)userName
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_chatDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO CHATCOMPLETE (MESSAGE, TOID, FROMID, READ, date, MESSAGEID, USERNAME) VALUES (\"%#\", \"%d\", \"%d\", \"%c\", \"%#\", \"%d\", \"%#\")", message, toID, fromID, read, date,messageID,userName];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_chatDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"DONE");
/* status.text = #"Contact added";
name.text = #"";
address.text = #"";
phone.text = #"";*/
} else {
// status.text = #"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(_chatDB);
}
}
This is the query to get the last message with a given fromID:
SELECT * FROM chatting WHERE fromID=9999 ORDER BY id DESC LIMIT 1
In SQLite 3.7.11 or later, the following query will return the message with the largest date for each sender:
SELECT *, MAX(date)
FROM ChatComplete
GROUP BY FromID
There are a few issues:
You have only one sqlite3_stmt variable for your two nested queries. You want a separate sqlite3_stmt for each.
You are calling sqlite3_reset. That is only used when binding new values to ? placeholders in your prepared statement, which is not applicable here. Worse, you're calling it inside your loop.
Unrelated to the problem at hand, but for each prepared statement, don't forget to call sqlite3_finalize when done looping through the results, in order to release the memory used when preparing the statements.
Thus, you might want something like:
sqlite3_stmt *userStatement;
sqlite3_stmt *messageStatement;
int rc; // the return code
NSMutableArray * messages = [[NSMutableArray alloc]init];
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_chatDB) == SQLITE_OK)
{
const char *query_stmt = "SELECT DISTINCT FROMID , USERNAME from CHATCOMPLETE";
if (sqlite3_prepare_v2(_chatDB, query_stmt, -1, &userStatement, NULL) != SQLITE_OK)
{
NSLog(#"%s: prepare userStatement failed: %s", __PRETTY_FUNCTION__, sqlite3_errmsg(_chatDB));
}
else
{
while ((rc = sqlite3_step(userStatement)) == SQLITE_ROW)
{
int userID = [[[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)] integerValue];
const char *query_stmt2 = "SELECT MESSAGE , USERNAME from CHATCOMPLETE where FROMID=? ORDER BY timestamp DESC LIMIT 1"; // change the `ORDER BY` to use whatever field you want to sort by
if (sqlite3_prepare_v2(_chatDB, query_stmt2, -1, &messageStatement, NULL) != SQLITE_OK)
{
NSLog(#"%s: prepare messageStatement failed: %s", __PRETTY_FUNCTION__, sqlite3_errmsg(_chatDB));
}
else
{
if (sqlite3_bind_int(messageStatement, 1, userID) != SQLITE_OK)
{
NSLog(#"%s: bind userID %d failed: %s", __PRETTY_FUNCTION__, userID, sqlite3_errmsg(_chatDB));
}
while ((rc = sqlite3_step(messageStatement)) == SQLITE_ROW)
{
NSLog(#"LAST MESSAGE %#",[[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)]);
}
if (rc != SQLITE_DONE)
{
NSLog(#"%s: step messageStatement failed: %s", __PRETTY_FUNCTION__, sqlite3_errmsg(_chatDB));
}
sqlite3_finalize(messageStatement);
}
}
if (rc != SQLITE_DONE)
{
NSLog(#"%s: step userStatement failed: %s", __PRETTY_FUNCTION__, sqlite3_errmsg(_chatDB));
}
sqlite3_finalize(userStatement);
}
}
else
{
NSLog(#"%s: open %# failed", __PRETTY_FUNCTION__, _databasePath);
}
return messages;
Note, this code sample, in addition to my three points above, also:
Log errors using sqlite3_errmsg if sqlite3_prepare_v2 fails.
Added check on return codes from sqlite3_step, too, again logging sqlite3_errmsg if it fails.
Added log if sqlite3_open failed.
Use sqlite3_bind_int() rather building SQL using stringWithFormat. In this case, because userID is numeric, this isn't critical, but if ever using string values in your WHERE clauses, using the sqlite3_bind_text() function becomes critical, so I just wanted to show the pattern.
For example, look at your save routine and try saving a message that happens to have double quotation mark in it (e.g. I spoke with Bob and he says "hello" to you.). Your stringWithFormat construct will fail. If you use sqlite3_bind_text, it will solve that problem.
BTW, as you can see, when you add all of the proper validation of results, binding of values, etc., the code becomes a bit unwieldy. You might consider using FMDB, which greatly simplifies your SQLite Objective-C code.
In the code below, the commented-out code works.
But using the saveData method of the DBMgr Class results in "Failded to add contact".
I want to see "Contact added" instead.
-(void) saveData{
NSString *insSQL = [NSString stringWithFormat:#"INSERT INTO CONTACTS (name,address,phone) VALUES (\"%#\",\"%#\",\"%#\")",name.text,address.text,phone.text];
DBMgr *dbmgr = [DBMgr alloc];
if([dbmgr saveData:insSQL]== 0){
status.text = #"Contact added";
}else if([dbmgr saveData:insSQL]== 1){
status.text=#"Failded to add contact";
}
/*sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO CONTACTS (name,address,phone) VALUES (\"%#\",\"%#\",\"%#\")",name.text,address.text,phone.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
status.text = #"Contact added";
name.text = #"";
address.text = #"";
phone.text = #"";
}else{
status.text=#"Failded to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}*/
}
-(NSInteger) saveData:(NSString *) querySQL{
NSInteger result;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = querySQL;
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
result = 0;
}else{
result = 1;
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
return result;
}
You should check the result codes of all of your SQLite calls, and if they fail, log the error:
- (NSInteger) saveData:(NSString *) querySQL{
NSInteger result = 1;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = querySQL;
const char *insert_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) != SQLITE_OK)
NSLog(#"%s: prepare failed: %s", __FUNCTION__, sqlite3_errmsg(contactDB));
else
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
result = 0;
}else{
NSLog(#"%s: step failed: %s", __FUNCTION__, sqlite3_errmsg(contactDB));
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
} else {
NSLog(#"%s: open failed", __FUNCTION__);
}
return result;
}
Unless you look at sqlite3_errmsg, you're just guessing. And check sqlite3_prepare_v2 return code, too, like I did above, (as that will more likely be the initial indication of a problem).
Two other, unrelated, observations:
The DBMgr should be initialized, e.g.:
DBMgr *dbmgr = [[DBMgr alloc] init];
You are building your INSERT statement using stringWithFormat. That's very dangerous, you should use ? placeholders in your SQL:
const char *insSQL = "INSERT INTO CONTACTS (name,address,phone) VALUES (?, ?, ?)";
sqlite3_prepare_v2(contactDB, insSQL, -1, &statement, NULL);
Then, after preparing that statement, you should then use the sqlite3_bind_text function to assign your values to those three placeholders, e.g.
sqlite3_bind_text(statement, 1, [name.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [address.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [phone.text UTF8String], -1, SQLITE_TRANSIENT);
By the way, if you wanted to specify NULL, you'd call sqlite3_bind_null instead of sqlite3_bind_text.
Obviously, check the return code from each of those to make sure you returned SQLITE_OK for each, again, logging sqlite3_errmsg if it failed.
I appreciate that this change is going to require some refactoring of your code, but it's important to use sqlite3_bind_text to avoid SQL injection attacks and errors that will result if the user typed in a value that included quotation marks.
By the way, if you're looking at the above and realizing that it takes a lot of code to do this properly, you might want to consider using FMDB which can significantly simplify your life.