I can't update my database: I connect to it and then I use this function but it must execute but no update:
- (BOOL) updatePoint:(NSString *) word {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"master.sqlite"];
sqlite3_stmt *statement;
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
int newPoint = point + 1;
NSString *sql = [NSString stringWithFormat:#"UPDATE %# SET Point='%d' WHERE Parola=?",
dictionaries1[[[word substringWithRange:NSMakeRange(0,1)] intValue]-1],newPoint];
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)==SQLITE_OK)
sqlite3_bind_text(statement,1,[word UTF8String] ,-1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);
//sqlite3_reset(statement);
sqlite3_close(database);
return YES;
} else
return NO;
}
What's wrong?
you can look at this, the update code above seems fine but the only problem that might come may be due to database being overwritten or it might not be present at the specified location.
Problem with inserting data into a table
You can replace the insert code with your update statements
Related
This question already has answers here:
SQLite insert works in simulator but not on device
(2 answers)
Closed 9 years ago.
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSBundle mainBundle] pathForResource:#"etkYeni2" ofType:#"sqlite"];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){
NSLog(#"icerdeyim");
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO etk (etkTip) VALUES ('%#')",yeniEkleLabel.text];
const char *query_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"OK INSERTED");
} else {
NSLog(#"NOT INSERTED !!");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
Here is my part of the code which I getting a string from the textbox and inserted it to the sqlite database. I created the database from the firefox extention sqlite manager and added it to the app. It is running as I expected in simulator and ipod touch how I wanted (inserts elements and I can see them when I used SELECT query ), but it is not inserts an element on iphone . My ipod touch is on iOS 5 and iphone is on iOS 6. Is it because of the version of iOS or iphone and ipod touch is pretends differently to the sqlite or what ? I read most of the answers of this problem and still don't get the solution. Could someone can help me in this situation ?
Thanks ..
Your database not Copied at phone directory u need to add that database.
using this code.
-(void)createdatabase
{
NSString *databaseName = #"Card.sqlite"; // Database Name
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = documentPaths[0];
NSLog(#"Dir : %# ",documentsDir);
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager] ;
success = [fileManager fileExistsAtPath:databasePath];
if(success)
return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
NSLog(#"%#",databasePathFromApp);
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
add your sqlite file into mainbundle.
thsi will really helpful.
Please try to use this one. If you wanna insert data in sqlite in that case you must create your Database and insert query using this method.I hope it will be helpful..
- (void)executeQuery:(NSString *)insertQuery
{
sqlite3_stmt *statement;
if (sqlite3_open([[self databaseFilePath] UTF8String],&sqliteDB) == SQLITE_OK)
{
if(sqlite3_prepare_v2(sqliteDB,[insertQuery UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) != SQLITE_DONE)
{
sqlite3_finalize(statement);
}
}
else
NSLog(#"query Statement Not Compiled");
sqlite3_finalize(statement);
sqlite3_close(sqliteDB);
}
else
NSLog(#"Database Not Opened");
}
- (NSString *)databaseFilePath
{
databaseName=#"abc.sqlite";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];
return [path stringByAppendingPathComponent:databaseName];
}
I am fairly new to iOS development. I am trying to build application which searches given search term in sqllite3 DB. I am having trouble with binding parameter to that sql statement.Following is my code to read data from database.
-(void) readPlayersFromDatabase
{
NSString * strTemp=[[NSString alloc]init];
strTemp=#"ben";
sqlite3 *database;
players = [[NSMutableArray alloc] init];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
char *sqlStatement = "SELECT * FROM Player WHERE LENGTH (lastname)>0 AND LENGTH (firstname)>0 AND ( lastname LIKE '%?%' OR firstname LIKE'%?%' OR Height LIKE '%?%' OR Weight LIKE '%?%') ORDER BY lastname,firstname";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(compiledStatement, 1, [strTemp UTF8String],-1, SQLITE_TRANSIENT );
sqlite3_bind_text(compiledStatement, 2, [strTemp UTF8String],-1, SQLITE_TRANSIENT );
sqlite3_bind_text(compiledStatement, 3, [strTemp UTF8String],-1, SQLITE_TRANSIENT );
sqlite3_bind_text(compiledStatement, 4, [strTemp UTF8String],-1, SQLITE_TRANSIENT );
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *playerId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *playerName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
Player *temp = [[Player alloc] initWithID:playerId name:playerName];
[players addObject:temp];
[temp release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
All I am getting at the end is name of player which has ? in their name. Can anyone help me out here. Can you also tell me how can I connect UITextfield input to the strTemp in above code ?
Thanks.
What do you want to do exactly? You can send as a parameter to your readPlayersFromDatabase method to your UITextfield input.
Here is my method to select command: I had a class which is called DBOperations. I hope that helps
+ (int) getUserID{
//check DB
if (![DBOperations checkDB])
{
NSString *msgAlert = #"can not access db file.";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Connection error" message:msgAlert
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
return 0;
}
[DBOperations open];
int _userID = 1;
const char* sql = " select id from Oyuncu ORDER BY id desc";
if (sqlite3_prepare_v2(database, sql, -1, &hydrate_statement, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
if (sqlite3_step(hydrate_statement)==SQLITE_ROW)
{
_userID= sqlite3_column_int(hydrate_statement,0);
NSLog(#"%d",_userID);
}
// Execute the query.
sqlite3_finalize(hydrate_statement);
return _userID;
[DBOperations close];
}
+(BOOL) checkDB {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"dbOyun.db"];
BOOL success = [fileManager fileExistsAtPath:writableDBPath];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return true;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"dbOyun.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
return success;
}
+ (void) open{
//check db validity
NSString *dbPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dbPath = [documentsDirectory stringByAppendingPathComponent:#"dbOyun.db"];
if (sqlite3_open([dbPath UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
}
I had used this code to write enteries into database but it is no refelecting into it...I don't know why?? Please give your suggestions!!Thanks in advance..
NSString* user_Name=txt_UserName.text;
NSString* password=txt_Password.text;
NSString* rePassword=txt_RePassword.text;
if ([password isEqualToString:rePassword]) {
sqlite3 *database;
sqlite3_stmt *compiledQuery;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "insert into Main values(?,?)";
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledQuery, NULL);
sqlite3_bind_text(compiledQuery, 1, [user_Name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledQuery, 2, [password UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledQuery))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_finalize(compiledQuery);
sqlite3_close(database);
}
is the path to the database is correct ? You can check the database path by
NSString *documentsDir = [DbCls getPath];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:DBNAME];
+(NSString *) getPath
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
return documentsDir;
}
I'm having problems with SQLite on the iPhone simulator. I've reduced it to a simple test app. sqlite3_prepare_v2 fails. But the real device works fine.
I created a database using sqlite3, and added it to my resources. In my code, in viewDidLoad, I call createEditableCopyOfDatabaseIfNeeded (which is a common, widely-used method, seen everywhere). And then I call the following:-
- (void) queryDatabase {
sqlite3 *database;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myDatabase.sql"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
NSMutableString *result = [[NSMutableString alloc] init];
const char *sqlStatement = "select * from people";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *firstname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
[result appendFormat:#"%# %#\n", firstname, surname];
}
[resultView setText:result];
}
else {
[resultView setText:#"Problem querying database"];
}
sqlite3_finalize(compiledStatement);
[result release];
}
sqlite3_close(database);
}
On the simulator, sqlite3_prepare_v2 doesn't return SQLITE_OK
Can you please tell, what error are you getting?
To find error you can use
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
Ensure you have deleted the app icon from simulator & clean all targets before build & run. This will probably resolve your issues.
Uninstall app from simulator and reinstall may work.
i am using this
-(void) Data
{
databaseName = #"List.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath =[documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
sqlite3 *database;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
if(detailStmt == nil)
{
const char *sql = "Select item from list";
if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) == SQLITE_OK)
{
NSLog(#"Hiiiiiii");
while(sqlite3_step(detailStmt) == SQLITE_ROW)
{
NSLog(#"Helllloooooo");
NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];
//item1=item;
NSLog(#"%#",item);
}
sqlite3_reset(detailStmt);
}
sqlite3_finalize(detailStmt);
sqlite3_clear_bindings(detailStmt);
}
}
detailStmt = nil;
sqlite3_close(database);
}
You should have a look at this tutorial. It always helped me when I used sqlite in ios. For me it looks like you should structure your code a little bit, you shouldn't load any path's inside a query method.
Maybe the problem occurs because of
sqlite3_reset(detailStmt);
sqlite3_clear_bindings(detailStmt);