SQLite- how to insert data in ios - iphone

i tried some sqlite program for practice when i insert the record with this code it give some error
-(void) insertRecordIntoTableNamed:(NSString *) tableName
withField1:(NSString *) field1
field1Value:(NSString *) field1Value
andField2:(NSString *) field2
field2Value:(NSString *) field2Value {
NSString *sql = [NSString stringWithFormat:
#"INSERT INTO ‘%#‘ (‘%#‘, ‘%#‘) VALUES (‘%#‘,’%#‘)",tableName, field1, field2, field1Value, field2Value];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, #"Error updating table.");
}
}
the error message is
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table.'
i try to insert data through terminal and the data is retrived properly
so Please help me for inserting data through programms

may it works better when you use a char * as sql statement as an NSString object. For example:
char *sql = "INSERT INTO ‘?‘ (‘?‘, ‘?‘) VALUES (‘?‘ ....";
to replace the ? with your NSString arguments use the function below.
sqlite3_bind_text(insert_statement, 1, [tableName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [field1 UTF8String], -1, SQLITE_TRANSIENT);
etc....
First you have to define the insert_statement.
static sqlite3_stmt *insert_statement = nil;
... and at the end you should reset the statement with sqlite3_reset(insert_statement);

LEFT SINGLE QUOTATION MARK is unsuitable for quoting either identifiers or values in SQLite. Quote identifiers with " (\x22) and data with ' (\x27).
Also, I hope your program is simple enough that potential SQL injection doesn't matter.

Related

Sqlite Prepare Statement

I have 12 columns in my table. In which last two columns are added newly. I use sqlite3 database. Database is created and query is executed correctly if I use where clause for any other columns other than lastly created two new columns. The last two columns return NULL string which makes the app crash throwing Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' *+[NSString stringWithUTF8String:]: NULL cString'**
When the query is executed in SQL Manager I could retrieve last two columns. I suppose somewhere something is wrong in my prepare statement.
NSString *query = #"select * from tableName where lastColumn = 'Value'";
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
NSLog(#"could not prepare statement: %s\n", sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
}
Please Help me on this. I totally have no clue where the last two columns have gone.. :)
You're passing NULL to stringWithUTF8String: method, which is not allowed. You should check the value you get from sqlite before instantiating NSString object.
const char *ctext = sqlite3_column_text(compiledStatement, 2);
if (ctext) {
text = [NSString stringWithUTF8String:ctext];
}
When you added new columns, values for this columns in all existing rows were set to NULL.

data is not being inserted from this insert query

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_sqlStmt1);
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));
...

Select Query Is Not Being Prepared

Here i am implementing a select query to fetch data from table but no any row is been returned by my query.
below is my code that i am using:
if ((sqlite3_open([[self dataFilePath] UTF8String], &database))==SQLITE_OK)
{
NSLog(#"DataBase OpeneD..!!");
imageId=[[NSMutableArray alloc] init];
const char *slectImageIdQuery="SELECT * from ts_Gallary;";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, slectImageIdQuery, -1, &statement, nil)==SQLITE_OK)
{
NSLog(#"Query CREATED..!!");
while (sqlite3_step(statement)==SQLITE_ROW)
{
int iId=sqlite3_column_int(statement, 0);
NSString *imgTtl=[NSString stringWithFormat:#"%d",iId];
NSLog(#"image id in while=%d",iId);
[imageId addObject:[NSString stringWithFormat:#"%#",imgTtl]];
}
}
else
{
NSLog(#"query could not be prepared");
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
My console display:
2011-04-06 17:44:33.381 Tuscany[4680:207] DataBase OpeneD..!!
2011-04-06 17:44:33.382 Tuscany[4680:207] query could not be prepared
Note: Here dataFilePath is method which return path of my database and database is sqlite3 object
No Error or exception is been thrown by my application.
What problem can there be and how can I detect the problem as well as solve that?
Thanks.
No Error or exception is been thrown
by my application.
Well, yeah, there is. Your call to sqlite3_prepare_v2() fails; it doesn't return SQLITE_OK.
What happens when you run this statement at your database's sqlite prompt?
SELECT * from ts_Gallary;
Try replacing this line
NSLog(#"query could not be prepared");
with something that will give you more information about the error. Include a call to sqlite3_errmsg().
Remove the semicolon(;) from const char *slectImageIdQuery="SELECT * from ts_Gallary;"
like const char *slectImageIdQuery="SELECT * from ts_Gallary"
and its work properly.

iPhone, sqlite3, how do I pass values into a select statement?

This is the code I'm using to select some records from my database. I'm binding two dates into my sql, however, when I get to sqlite3_step I get SQLITE_DONE where I should be getting SQLITE_ROW. It looks like its processing the bindings rather than querying the data.
What am I doing wrong ?
NSString *startDateRangeString = #"2000-05-01";
NSString *endDateRangeString = #"2011-05-01";
sqlite3 *database;
int result = sqlite3_open("mydb.db", &database);
if(result != SQLITE_OK)
{
NSLog(#"Could not open db.");
}
const char *sql = "select pid from tmp where due >= '%#' and due < '%#' order by due, pid;";
sqlite3_stmt *statementTMP;
int error_code = sqlite3_prepare_v2(database, sql, -1, &statementTMP, NULL);
if(error_code == SQLITE_OK) {
sqlite3_bind_text(statementTMP, 1, [startDateRangeString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statementTMP, 2, [endDateRangeString UTF8String], -1, SQLITE_TRANSIENT);
int step_error_code = sqlite3_step(statementTMP);
while(sqlite3_step(statementTMP) == SQLITE_ROW) // I get 101 aka SQLITE_DONE
{
NSLog(#"Found!!");
}
}
sqlite3_finalize(statementTMP);
sqlite3_close(database);
I think your SQL is bad. Instead of %# you should use ? for the positional parameters.
I strongly suggest using a wrapper to simplify life. FMDB is a great one at http://github.com/ccgus/fmdb.
char *statementTMP = "select pid from tmp where due >= '?1' and due < '?2' order by due, pid";
....
sqlite3_bind_text(statementTMP, 1, [startDateRangeString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statementTMP, 2, [endDateRangeString UTF8String], -1, SQLITE_TRANSIENT);
This is by design.
sqlite3_step returns SQLITE_ROW for each row in the resultset, and SQLITE_DONE to indicate there are no more rows. So if a resultset contains N rows, then N calls would return SQLITE_ROW and (N+1)st returns SQLITE_DONE. An empty resultset is not in any way special, it follows the same logic with N=0 (so the very first call returns SQLITE_DONE). This allows client code to handle all resultsets uniformly.
Hope that helps.
Let me know if you need anymore help.
PK
If I read correctly.. You are not executing the statement.. you are just preparing.. you have to do both..
this part just prepare the query
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK)
{
NSLog(#"SQL Warning: failed to prepare statement
with message '%s'.", sqlite3_errmsg(database));
}
This part actually executes the query
if(sqlite3_exec(database, sql, nil, &stmt, &errmsg) == SQLITE_OK)
{
NSLog(#"it means that you query executed correctly");
if(sqlite3_step(stmt) == SQLITE_ROW)
{
NSLog(#"Found!!");
}
}else
{
NSLog(#"SQL Warning: '%s'.", sqlite3_errmsg(database));
}
=)

SQLite database - cannot see updates

I have a sqlite database and I am adding to it new words. The problem is that I can see them added to a table only after restarting application. The "SELECT" statement doesn't "see" newly added elements before restarting application.
Why may this happen?
I am creating some kind of a dictionary. Here is how I add new items:
const char *sql_query = "INSERT INTO words(word) VALUES(?)";
if(sqlite3_prepare_v2(database, sql_query, -1, &addWordsStmt, NULL) != SQLITE_OK)
{
return FALSE;
}
sqlite3_bind_text(addWordsStmt, 1, [ word UTF8String], -1, SQLITE_TRANSIENT);
if( sqlite3_step(addWordsStmt) != SQLITE_DONE)
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(addWordsStmt);
sqlite3_finalize(addWordsStmt);
And here is my retrieval code:
const char *sql_query = "SELECT word FROM words WHERE id=?";
if(sqlite3_prepare_v2(database, sql_query, -1, &getWordsStmt, NULL) != SQLITE_OK)
{
return;
}
sqlite3_bind_int(getWordsStmt, 1, wordid);
if( sqlite3_step(getWordsStmt) != SQLITE_ROW)
{
NSLog(#"Error while getting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(getWordsStmt);
return;
}
NSString *word = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getWordsStmt, 0)];
sqlite3_reset(getWordsStmt);
sqlite3_finalize(getWordsStmt);
My guess is that you are in a transaction. There must be some other calls during your open and close routines that are wrapping your calls in a transaction and not displayed in your code fragments.
That is why you don't see the new words until you application exits
There's your problem:
const char *sql_query = "SELECT word FROM words WHERE id=?";
This isn't how you use SQL. Instead, you should be using SELECT word FROM words; and stepping to get each row as long as you're getting SQLITE_ROW until you get SQLITE_DONE. That will get you all your words. How are you going to find a word by id when you don't know the id of newly added words?