ios-issue with sqlite-database path [closed] - iphone

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Here is my code:
It works for other methods but creating an issue here only.
Here I am unable to insert data...
What is the problem here?Code was working earlier perfectly.
-(void) insertleadership {
[self trunleadership];
// Setup the database object
sqlite3 *database;
NSLog(#"Inside leadership");
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
// NSString *query=[stringWithFormat: #"select name,password from customer where name=%# and password=%#",*login_name,*login_pass];
sqlite3_stmt *compiledStatement;
for(int i=0;i<ltitle.count;i++)
{
// NSString *insertSQL = [NSString stringWithFormat:#"insert into category values(\'aa\',\'bb\',\'cc\',\'cc\',\'cc\')"];
// NSLog(#"Inside leadership");
NSString *insertSQL = [NSString stringWithFormat:#"insert into leadership values(\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\')",[ltitle objectAtIndex:i],[ldate objectAtIndex:i],[lfunction objectAtIndex:i],[lservice objectAtIndex:i],[lsubservice objectAtIndex:i],[labstract objectAtIndex:i],[ldoctype objectAtIndex:i],[lcreated objectAtIndex:i],[lcorridor objectAtIndex:i],[lfeature objectAtIndex:i],[lindiastory objectAtIndex:i],[lpublish objectAtIndex:i],[ltopic objectAtIndex:i]];
// NSLog(#"SQL Query for leader:%#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
// sqlite3_prepare_v2(databaseName, insert_stmt, -1, &statement, NULL);
sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL);
if(sqlite3_step(compiledStatement) == SQLITE_DONE){
NSLog(#"Insert into leadership successful");
}
else {
NSLog(#"Unable to insert in leadership");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
}

If you add some reporting to your code, sqlite itself will tell you why it's not opening:
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Do something
splite3_close(database);
}
else
{
NSLog(#"Failed to open database '%#': %s", databasePath, sqlite3_errmsg(database));
}
Also #HotLicks is correct in that you should be copying the database out of the app bundle before you access it, unless you open it readonly (by using sqlite3_open_v2() and passing in SQLITE_OPEN_READONLY as one of the flags).

Related

Fetch different data types values from string and separate them [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using QR code and after scanning getting username, eventtitle and ticketcode. I need to get these values separately in three different strings, so that i can save them in sqlite. I want to know only how to do this separation, saving is i can do.
Thanks in advance.
- (void)saveData {
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 (\"%#\", \"%#\", \"%#\")", #"NameString", #"Address", #"Phone"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"Success");
} else {
NSLog(#"Failed");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
Note
If you post what type of string you are getting, i will add how exactly you have to split it.
Below is example how you can split
NSArray arrResult = [str componentsSeparatedByString:#"-"];
Or By Character set
NSString* str = #"A string with newlines in it";
NSArray *arrTemp = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
you can use componentsSeparatedByString
NSString *str = #"userName:Password:etc";
NSArray* parts1 = [str componentsSeparatedByString:#":"];
NSLog(#" [[parts1 objectAtIndex:0] integerValue]=%d", [[parts1 objectAtIndex:0] integerValue]);
//the output will be: username for index 0, and so on

I'm having trouble reading data from a sqlite file in objective c

I've been trying to figure this out for a while, and the problem seems to be that the if statement at the bottom of the following code is failing:
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
}
else
NSLog(#"Database opened successfully");
//CREATE THE TABLE HERE IF NEEDED
NSString *createSQL = #"CREATE TABLE IF NOT EXISTS workouts "" (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";
char *errorMsg;
if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database);
NSAssert(0, #"Error creating table: %s", errorMsg);
}
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
-1, &statement, nil) == SQLITE_OK)
What could be the problem? I know there are a lot of other factors involved, but anything would be helpful. Just ask if you want any additional info. Thanks StackOverflow!
edit: the error that i'm receiving is: no such column: columnName
Honestly I wouldn't recreate any part of the Core Data framework unless you have a very good reason to do so. This tutorial might help you get started with Core Data programming
http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
Here's the reference page for Core Data:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650

How compare data from database? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have create a database. In my view i am checking provided dat is already exist or not. If exist then retrun Yes otherwise no. for this i have use this code..
+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:(NSString *)Category_text{
BOOL flag=FALSE;
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
//int LastId="select Max(place_id) from Places";
// NSLog(#"%i",LastId);
const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text";
sqlite3_stmt *stmt;
int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rtnVal == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
flag=TRUE;
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
if(flag==TRUE)
return YES;
else
return NO;
}
on button action i have use this code
-(IBAction)Find{
BOOL tmp=[GlobalClass compareDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text];
if(tmp)
[GlobalClass storeDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text];
else
NSLog(#"exsit data in database");
}
Now problem is that it always return no. What is error in this code? And How make compression in data from data base?
Thanks in advance...
You have to use following code for SQL Statement.
NSString *tempSQL = [[NSString alloc] initWithFormat:#"select * from Places where Start_text = '%#' and End_text = '%#' and Category_text = '%#'", Start_text, Start_text, Category_text];
const char *sql = [tempSQL cStringUsingEncoding:NSUTF8StringEncoding];
[tempSQL release];
+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:NSString *)Category_text
{
BOOL flag=NO;
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
//int LastId="select Max(place_id) from Places";
// NSLog(#"%i",LastId);
const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text";
sqlite3_stmt *stmt;
int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rtnVal == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, [Start_text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [End_text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, [Category_text UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(stmt) == SQLITE_ROW) {
flag=YES;
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return flag;
}

sqlite3_prepare_v2 problem

I'm having a bit of trouble with statement:
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
in the code below, the code is jumping out of the IF at this point. Anyone any thoughts ?
// 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 = "select route_name from Route";
sqlite3_stmt *compiledStatement;
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)
{
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// Add the animal object to the animals Array
//[list addObject:animal];
[list addObject:aName];
//[animal release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
Problem solved, when I view the database through the Firefox plugin SLQite Manage, the table name is actually called ZROUTE.

sqlite3 is not working in my iphone application

I want to use sqlite3 for my iphone application. But it is not working. Scenario is :- On click event of a button a method should run and retrieve the data from the database. But My sequence of program is not going into
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from records";
sqlite3_stmt *compiledStatement;
0NSLog(#"%s", sqlite3_errmsg(database)); // Results - no error
-
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
//here NSLog statement does not work.
There is no problem in opening a database. What should be the reason? Any solution appreciable.
Thanks
Try this: NSLog(#"%s", sqlite3_errmsg(database)); and see if there are some errors in your query.
Here is a modified example of my own code :
NSString *file = #"youdatabase.db"
sqlite3 *db;
if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
{
NSString *req = #"SELECT * FROM totoTable";
sqlite3_stmt *returnReq;
if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
{
while (sqlite3_step(returnReq) == SQLITE_ROW)
{
//Printf first returned value
NSLog(#"%s", (char*)sqlite3_column_text(returnReq, 0));
}
}
sqlite3_close(db);
}
Hope it will help you ;-)