Can't insert " character into Sqlite DB [Objective-C] - objective-c

I'm inserting some data on a sqlite db, It works fine but what I noticed is that I can't insert words that contains the character ", is it a common issue? should I change parse the text and edit every " character I find?
This is the code i'm using in order to insert data into my DB:
UICollectionViewCell *cell = (UICollectionViewCell *)button.superview.superview;
NSIndexPath *indexPath = [self.customCollectionView indexPathForCell:cell];
FolderProducts *item = _feedItems[indexPath.item];
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Carrello) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO CarrelloMese (titolo, codice, prezzo, urlImg) VALUES (\"%#\", \"%#\", \"%#\", \"%#\")",item.nomeProdotto, item.codice, item.prezzo, item.urlImg];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(Carrello, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
} else {
}
sqlite3_finalize(statement);
sqlite3_close(Carrello);
}

You need to bind your SQLite statements using the sqlite3_bind_xxx() function. Basically, you remove all variables from your statement (in your case the %#) and replace them with '?'. SQLite then knows that where an ? is HAS to be a variable, and therefore doesn't get it mixed up with a command.
For example, say you wanted to bind the word "INSERT". Using ? SQLite won't read this as a command and then flag an error.
Read the docs (link above) for full information on how to use the bind function.
Here's what your code might look like with binding (UNTESTED):
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Carrello) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO CarrelloMese (titolo, codice, prezzo, urlImg) VALUES (?,?,?,?)"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(Carrello, insert_stmt, -1, &statement, NULL);
if (sqlite3_bind_text(statement, 0, item.nomeProdotto.UTF8String, item.nomeProdotto.length, SQLITE_STATIC) != SQLITE_OK) {
NSLog(#"An error occurred");
}
// Etc etc
// SQLite bind works like this: sqlite_bind_text/int/e.t.c(sqlite3_stmt,index_of_variable, value);
// there are optionally parameters for text length and copy type SQLITE_STATIC and SQLITE_TRANSIENT.
if (sqlite3_step(statement) == SQLITE_DONE)
{
} else {
}
sqlite3_finalize(statement);
sqlite3_close(Carrello);
}

Related

Objective C Sqlite Update Query Based on ID

Trying to learn Sqlite without using a wrapper and have managed most things but am really stuck on UPDATE queries
I am trying to update a sting in one column based on its _ID number which is the primary Key and is unique.
I have tried all sorts of code from all over google . This one says it has worked but when I check the column has not been updated
here is the code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSString *IDCODE = code.stringValue;
NSInteger b = [IDCODE integerValue];
sqlite3 *contactDB; //Declare a pointer to sqlite database structure
const char *dbpath = [dbPath UTF8String]; // Convert NSString to UTF-8
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
//Database opened successfully
NSString *databaseName = #"vistorlog.db";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &myDB)==SQLITE_OK) {
NSLog(#"database Opened");
const char* updateQuery="update LOG set TIMEOUT='22/03/19' where _ID=1";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(myDB, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
NSLog(#"Query Executed");
}else{
NSLog(#"Query NOT Executed");
}
}
sqlite3_close(myDB);
}
else {
//Failed to open database
}
It opens the DB vistorlog.db ok. There is a table called LOG and there is column called TIMEOUT which is where I want the string to be updated and there is a column called _ID which is what Im basing the query on yet it won't update
eventually I want to have the update statement use the string variable dateString as the string to update and b as the integer variable for the _ID
any ideas where Im going wrong?
Any help appreciated
Mark
EDIT
WORKING CODE TO UPDATE A QUERY
sqlite3 *contactDB; //Declare a pointer to sqlite database structure
const char *dbpath = [dbPath UTF8String]; // Convert NSString to UTF-8
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
//Database opened successfully
NSString *databaseName = #"vistorlog.db";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &myDB)==SQLITE_OK) {
NSLog(#"database Opened");
// define dateString and IDCODE as strings before this
const char *updateQuery = "Update log set TIMEOUT=? where _ID=?";
sqlite3_stmt *stmt;
// Prepare Stment
if (sqlite3_prepare_v2(myDB, updateQuery, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [dateString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [IDCODE UTF8String], -1, SQLITE_TRANSIENT); if(sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(#"Query Executed");
} else {
NSLog(#"Query NOT Executed: %s", sqlite3_errmsg(myDB));
}
sqlite3_finalize(stmt);
}else{
NSLog(#"Statement NOT Prepared: %s", sqlite3_errmsg(myDB));
}
}
sqlite3_close(myDB);
}
else {
//Failed to open database
}
Don't forget to import sqlite3.h
Mark
You are only preparing the statement but not actually executing it.
You need to execute sqlite3_step() after you execute sqlite3_prepare_v2
if (sqlite3_prepare_v2(myDB, updateQuery, -1, &stmt, NULL) == SQLITE_OK) {
if(sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(#"Query Executed");
} else {
NSLog(#"Query NOT Executed: %s", sqlite3_errmsg(myDB));
}
sqlite3_finalize(stmt);
}else{
NSLog(#"Statement NOT Prepared: %s", sqlite3_errmsg(myDB));
}

SQLite Case Sensitive and Case Insensitive Search

I have NSMutableArray with NSMutableDictionary that carries a SQLite search result. I need to make a difference between case-sensitive and case-insensitive search. If I read some topics here, they say SQLite by default returns a case-sensitive result. And they suggest that you use COLLATE nocase to make a case-insensitive search. Actually, with the following code, I get the same result with or without COLLATE nocase. What am I doing wrong?
const char *sql;
if (checkButtonR5.state == 1) { // checkButtonR5 is a checkbox button. If it's on, it tells a search will be case-sensitive.
sql = "Select address,date,name,age,ID From data1a WHERE request LIKE ?";
} else {
sql = "Select address,date,name,age,ID From data1a WHERE request LIKE ? COLLATE nocase";
}
sqlite3_stmt *statement; // outputFile is a file path
if (sqlite3_open([outputFile UTF8String], &connection) == SQLITE_OK){
if(sqlite3_prepare_v2(connection, sql, -1, &statement, NULL) != SQLITE_OK) {
//NSLog(#"It cannot connect the database file.");
}
NSString *bindParam = [NSString stringWithFormat:#"%%%#%%",rqt];
if(sqlite3_bind_text(statement, 1, [bindParam UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK){
//NSLog(#"Problem binding search text param.");
} else {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *field0 = (char *) sqlite3_column_text(statement, 0); // address
NSString *field1Str0 = [[NSString alloc]initWithUTF8String:field0];
NSString *str0 = [[NSString alloc]initWithFormat:#"%#", field1Str0];
...
...
}
sqlite3_finalize(statement);
sqlite3_close(connection);
}
}
Thank you for your help.

Why is SQLite record not found?

I am trying to program a username/password log in view controller, there is no errors, no warnings in my app but it always output out "match not found" even when i select a username that already exist in my database..I'd really appreciate it if u could help
this is the code:
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths [0];
_databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"bank.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:_databasePath] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = " CREATE TABLE IF NOT EXISTS USER (USERNAME TEXT PRIMARY KEY, PASSWORD TEXT)";
if (sqlite3_exec(_bankDb, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_status.text= #"failed to create table";
}
sqlite3_close(_bankDb);
} else {
_status.text=#"failed to open/create database";
}
}
- (IBAction)findContact:(id)sender {
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT USERNAME, PASSWORD FROM USER WHERE USERNAME=\"%#\"", _usernameTextField.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_bankDb, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *usernameField=[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
_usernameTextField.text=usernameField;
NSString *passwordField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1) ];
_passwordTextField.text=passwordField;
_status.text=#"match found";
} else {
_status.text=#"match not found";
}
sqlite3_finalize(statement);
}
sqlite3_close(_bankDb);
}
}
You should do the following things,
look at sqlite3_errmsg values if any queries fail (e.g. sqlite3_prepare_v2 does not return SQLITE_OK or sqlite3_step does not return either SQLITE3_DONE or SQLITE3_ROW);
Do not use stringWithFormat with your queries, but rather use ? placeholders and bind values with sqlite3_bind_text
Thus:
if (sqlite3_open(dbpath, &_bankDb) == SQLITE_OK)
{
const char *query_stmt = "SELECT USERNAME, PASSWORD FROM USER WHERE USERNAME=?";
if (sqlite3_prepare_v2(_bankDb, query_stmt, -1, &statement, NULL) != SQLITE_OK)
NSAssert(0, #"prepare failed: %s", sqlite3_errmsg(_bankDb));
if (sqlite3_bind_text(statement, 1, [_usernameTextField.text UTF8String], -1, NULL) != SQLITE_OK)
NSAssert(0, #"bind failed: %s", sqlite3_errmsg(_bankDb));
int rc = sqlite3_step(statement);
if (rc == SQLITE_ROW)
{
NSString *usernameField=[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
_usernameTextField.text=usernameField;
NSString *passwordField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1) ];
_passwordTextField.text=passwordField;
_status.text=#"match found";
} else if (rc == SQLITE_DONE) {
_status.text=#"match not found";
} else {
NSAssert(0, #"step failed: %s", sqlite3_errmsg(_bankDb));
}
sqlite3_finalize(statement);
sqlite3_close(_bankDb);
}
If you're still failing with match not found, you should:
examine the contents of _usernameTextField.text to make sure your IBOutlet is hooked up correctly.
look at the contents of the database and make sure a record with the desired userid is found; you haven't shown us where you add the userid, so it's hard for us to diagnose why the userid in question was not found.
I must confess that the overall logic (just looking for matching records for that userid and populating the password field if you found the userid) looks highly suspect (you shouldn't be storing passwords in plaintext, you certainly shouldn't be returning a password provided simply a userid), but I'm focusing solely on the tactical issue of why you're seeing match not found error message.

Sqlite_binding methods

How can I write this query without using stringWithFormat. How can I pass parameters to the SQLite Query. Now my code is this:
NSString *querySQL = [NSString stringWithFormat:#"SELECT name, char_code, sound, status From Tmy_table Where ID=\"%d\"", i];
Thanks in advance
You should use sqlite3 host parameters and sqlite3_bind() to bind variables to them. This would like something like this in your example.
NSString* query = #"SELECT name, char_code, sound, status From Tmy_table Where ID=?";
sqlite3_stmt* myStatement = NULL;
sqlite3_prepare_v2(myDBConnection, [query UTF8String], -1, &myStatement, NULL);
sqlite3_bind_int(myStatement, 1, i);
Points to note:
The two sqlite3 functions return error codes that you must check. I've left that out for clarity.
The second parameter in sqlite3_bind_int() tells you which question mar to replace with the third parameter. The index starts at 1, not 0.
See also docs about binding.
NSString sql = #"SELECT name, char_code, sound, status From Tmy_table Where ID=?";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, SQLITE_STATIC) == SQLITE_OK)
{
// If 'i' was text:
// if (sqlite3_bind_text(stmt, 1, i, -1, SQLITE_STATIC) == SQLITE_OK)
if (sqlite3_bind_int(stmt, 1, i) == SQLITE_OK) // Note: 1-based column when binding!!!!
{
while (sqlite3_step(stmt) == SQLITE_ROW)
{
const char *name = sqlite3_column_text(stmt, 0); // Note: 0-based column when fetching!!!
const char *sound = sqlite3_column_text(stmt, 1);
const char *status = sqlite3_column_text(stmt, 2);
// ... print the values or whatever
}
}
else
{
NSLog(#"Failed to bind int: %s", sqlite3_errmsg(database));
}
sqlite3_finalize(stmt);
}
else
{
NSLog(#"Failed to prepare statement '%#': %s", sql, sqlite3_errmsg(database));
}
EDIT Changed the bind to sqlite3_bind_text() as i appears to be text...

Count Number of Rows in a SQLite Database

I'm trying the following code to count the number of rows in my SQLite database table, but it throws an exception. Is these a simpler way to do this?
- (void) countRecords {
int rows = 0;
#try {
NSString *dbPath = [self getDBPath];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *strSQL;
strSQL = #"SELECT COUNT(*) FROM MYTABLE";
const char *sql = (const char *) [strSQL UTF8String];
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {
// THIS IS WHERE IT FAILS:
if (SQLITE_DONE!=sqlite3_step(stmt) ) {
NSAssert1(0,#"Error when counting rows %s",sqlite3_errmsg(database));
} else {
rows = sqlite3_column_int(stmt, 0);
NSLog(#"SQLite Rows: %i", rows);
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
}
}
#catch (NSException * e) {
NSLog(#"Error Counting");
}
}
I came across a solution, using my code above, just replacing the step statement with the code below:
if (sqlite3_step(stmt) == SQLITE_ERROR) {
NSAssert1(0,#"Error when counting rows  %s",sqlite3_errmsg(database));
} else {
rows = sqlite3_column_int(stmt, 0);
NSLog(#"SQLite Rows: %i", rows);
}
This usually works for me
- (NSInteger )numberRecordsForTable:(NSString *)table {
NSInteger numTableRecords = -1;
if (sqlite3_open([self.dbPath UTF8String], &database) == SQLITE_OK) {
NSString *sqlStatement = [NSString stringWithFormat: #"select count(*) from %#", table];
const char *sql = [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, sql, -1, &sqlClause, NULL) == SQLITE_OK) {
while(sqlite3_step(sqlClause) == SQLITE_ROW) {
numTableRecords = sqlite3_column_int(sqlClause, 0);
}
}
else {
printf("could not prepare statement: %s\n", sqlite3_errmsg(database));
}
}
else {
NSLog(#"Error in Opening Database File");
}
sqlite3_close(database);
return numTableRecords;
}
HTH
There is no SQL expression to count rows in a database: you can count rows in a every table and then add them up.
I thought I'd trow in my two cents here as there is an expression to count rows in a database, I use it when dealing with MySQL databases using php scripts all the time. and I tested it in an ios app it's available in there too behold:
sqlite3 *database;
if(sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *sql = #"select count(*) from today";
sqlite3_stmt *selectStatement;
int returnValue = sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectStatement, NULL);
if (returnValue == SQLITE_OK)
{
if(sqlite3_step(selectStatement) == SQLITE_ROW)
{
numrows= sqlite3_column_int(selectStatement, 0);
}
}
sqlite3_finalize(selectStatement);
sqlite3_close(database);
}
no need for a fancy loop counter thing. btw if your using an auto increment int for the primary key. it works just slightly different then an array's key. where as in an array that is n items long the valid array elements are from 0 to n-1 in a database the key field is from 1 to n simple enough to work around if you just keep that in mind.
-(void)databaseRecordCount{
int rows = 0;
#try {
sqlite3 *database;
NSString *filePath = [self databaseDocumentsFilePath];
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
NSString *query = #"SELECT * FROM MYTABLE";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, NULL) != SQLITE_OK)
NSLog(#"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, nil) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
rows++;
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
}
#catch (NSException * e) {
NSLog(#"Error Counting");
}
NSLog(#"SQLite Rows: %i", rows);
NSUserDefaults *userDefaults;
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:rows forKey:#"databaseRecordCount"];
[userDefaults synchronize];
}
You'll have to count of each table individually. Some pseudo code:
sql = "SELECT name FROM sqlite_master" WHERE type = 'table'
tables() = GetRows(sql)
Dim total As Integer
For Each t As String in tables
sql = "SELECT COUNT(*) FROM " + t
total = total + GetValue(sql)
Next
Show(total)