I try this code.
query = [NSString stringWithFormat:#"INSERT INTO Bookmark(Names,Details) Values('%#','%#')", delegate.str, delegate.str1];
NSLog(#"%#",query);
sqlite3_stmt *compiledStatement;
if (sqlite3_open([databasepath UTF8String],&database) == SQLITE_OK)
{
if(sqlite3_prepare_v2(database,[query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSLog( #"Error while inserting data: '%s'", sqlite3_errmsg(database));
else
NSLog(#"New data inserted");
sqlite3_reset(compiledStatement);
}else
{
NSLog( #"Error while inserting '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}
This is i want to store sqlite INSERT INTO Bookmark(Details) Values('Let's take an example, 23456789 divided by 7, 11, 13.
Make triplets as shown starting from unit's place.
23...456...789
Now add alternate triplets :
23 + 789 = 812 and 456.
Difference of these two sums :
812 - 456 = 356
Now divide this difference by 7, we get reminder 6
And divide this difference by 11, we get reminder 4
And divide this difference by 13, we get reminder 5')
And this the error after write your line...
2013-07-25 16:38:14.653 Maths Tricks Tips Patterns[4346:c07] Error while inserting 'near "s": syntax error'
How may i insert special charcter please help me out this thanks in advance.
You should NEVER form SQL queries manually using stringWithFormat etc.
Instead you should use prepared statements with variable binding. This will ensure the correct encoding and avoid SQL injections etc. You should begin by looking at this sqlite introduction.
The basic pattern is to use sqlite3_prepare followed by something like sqlite3_bind_text to set the value for one of the ? placeholders in your query.
Related
This question already has answers here:
SQlite: select into?
(5 answers)
Closed 8 years ago.
Right now my statement looks like this:
NSString *sqlJoin = #"SELECT * INTO #tmp FROM foods JOIN bridgeTable ON foods.foodID = bridgeTable.foodID";
if(sqlite3_prepare_v2(database, [sqlJoin UTF8String],
-1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
}
sqlite3_finalize(statement);
}
else {
NSLog(#"Statement: %s", sqlite3_errmsg(database));
}
NSLog(#"count: %i", [foodArray count]);
The output I'm getting is:
2012-07-06 10:32:04.940 ProFitness[7087:f803] Statement: near "INTO": syntax error
2012-07-06 10:32:04.940 ProFitness[7087:f803] count: 0
2012-07-06 10:32:04.940 ProFitness[7087:f803] sql statement: SELECT * FROM #tmp WHERE TableID = '3'
2012-07-06 10:32:04.941 ProFitness[7087:f803] Statement: near "#tmp": syntax error
If my syntax is wrong, what SHOULD it look like?
You have to create the temporary temp yourself using:
CREATE TEMP TABLE xxxx
then you would use
SELECT * INTO xxxx
For more information see SQLite's Use Of Temporary Disk Files.
Hi I'm trying to create a three fields table (date,text,real),
I can insert and update the first two fields but can't set or update the last one.
createSQL = #"CREATE TABLE IF NOT EXISTS ACTIVITIES (MYDATE DATETIME PRIMARY KEY, ACTIVITY TEXT , LENGTH REAL);";
try to insert
char *update = "INSERT OR REPLACE INTO ACTIVITIES (MYDATE, ACTIVITY , LENGTH) VALUES (?, ? ,?);";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update , -1, &stmt, nil) == SQLITE_OK)
{
sqlite3_bind_double(stmt, 1, valueToWrite);
sqlite3_bind_text(stmt, 2, [task UTF8String], -1, NULL);
sqlite3_bind_double(stmt, 3, 1.5);
}
if (sqlite3_step(stmt) != SQLITE_DONE)
{
NSAssert1(0, #"Error updating table: %s", errorMsg);
}
sqlite3_finalize(stmt);
sqlite3_close(database);
The first two values are updated but the last one remains 0 even if I'm writing a value like 1.5 or put a double variable with a different value.
I am trying to delete some entries in my sqlite database on my iPhone app, but am getting a weird error.
Here is my code:
if(sqlite3_open([databasePath UTF8String], &yerocDB)==SQLITE_OK)
{
sqlite3_stmt *compiledstatement;
NSString *deleteSql=[NSString stringWithFormat: #"delete from Favorites_Table where playlist_name = Studying and date = 1/1/2012"];
const char *sqlstmt = [deleteSql UTF8String];
if(sqlite3_prepare_v2(yerocDB, sqlstmt, -1, &compiledstatement, NULL)==SQLITE_OK)
{
int result = sqlite3_step(compiledstatement);
if(SQLITE_DONE != result)
NSAssert1(0,#"Error while creating delete statement => %s",sqlite3_errmsg(yerocDB) );
}else{
NSLog(#"didn't delete error: %s", sqlite3_errmsg(yerocDB));
}
sqlite3_finalize(compiledstatement);
}
but then I get the error:
didn't delete error: no such column: Studying
playlist_name and date are my columns...Why is it saying the "Studying" is not a column?
You need to wrap Studying in single quotes. And your date.
This:
delete from Favorites_Table
where playlist_name = Studying and date = 1/1/2012
Should be this:
delete from Favorites_Table
where playlist_name = 'Studying' and date = '2012-01-01'
The reason is that if you don't put it in single quotes, the parser will think it's a column name. In your first query, you were trying to delete from Favorites_Table where the playlist_name column equalled the Studying column. Henceforth your error, "No such column."
And once you fixed the quotes around studying, your date was going to throw an error, too. Dates use ISO format (yyyy-mm-dd) to compare. Don't use the localized mm/dd/yyyy or dd/mm/yyyy formats, as a rule of thumb.
i want to update my sqlite database but i cannot find the way to do it,following is the code:
const char *sqlStatement = "UPDATE frame SET fileExist = '1' WHERE name='$variable'";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(#"successupdate");
}
from the above code i want my table update where the name is equal to $variable name;how to achieve this?
You're nearly there.
const char *sqlStatement = "UPDATE frame SET fileExist = '1' WHERE name=?";
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
sqlite3_bind_text(sqlStatement, 1, variable, -1, SQLITE_TRANSIENT);
int success = sqlite3_step(sqlStatement);
sqlite3_reset(sqlStatement);
Note that preparing the SQL statement only tells SQLite what the statement looks like. It's sqlite3_bind_text that applies the variable to the SQL statement and the sqlite3_step line that actually runs it.
FMDB is a nice wrapper for SQLite statements. It takes care of most of the prepare/bind/step/reset drudgery. I highly recommend it for any project that needs a DB, but doesn't want to use Core Data.
I'm trying to use the SQLite C API in my iPhone app. I'm trying to query an SQLite database for the number of records that have been completed after a certain date. The database saves the completed date as text in YYYY-MM-dd format. For example the text 2009-04-10 might appear as a completed date.
When I query the database from the commandline my query works, but when run from the app, it doesn't. Here's what I'm doing:
From the commandline, I run this query:
sqlite> SELECT COUNT(*) FROM tasks WHERE completed > '2009-04-09'
...> go
1
As you can see, there is one record found as I expect.
In my app, I execute this code (written in Objective-C, obviously):
static sqlite3_stmt *count_tasks_statement = nil;
if(count_tasks_statement == nil) {
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > '?'";
if (sqlite3_prepare_v2(database, sql, -1, &count_tasks_statement, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
NSString *today = #"2009-04-09";
sqlite3_bind_text(count_tasks_statement, 1, [today UTF8String], -1, SQLITE_TRANSIENT);
// Get the row count from the query
NSInteger taskCount = 0;
if(sqlite3_step(count_tasks_statement) == SQLITE_ROW) {
// The second parameter indicates the column index into the result set.
taskCount = sqlite3_column_int(count_tasks_statement, 0);
}
// Reset the statement for future reuse.
sqlite3_reset(count_tasks_statement);
When I use the debugger on this code, and examine the taskCount variable, it is set to 0, indicating that no records were found. (If I change the code to return primary keys for found rows, it still returns nothing.)
Since it works from the commandline, but doesn't in my code, I assume that I'm doing something wrong with either the quoting of the question mark in my SQL, or with the binding of the literal text date to the query. But, I've tried it lots of different ways with no luck. Help!
Don't put parameter placeholders inside quotes, even if the value is a string or date literal.
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > ?";
I think that you do not need the extra quotes around the question mark.
Try
const char *sql = "SELECT COUNT(*) FROM tasks WHERE completed > ?";
and it should work.