I Am using following code for getting maximum value in column
Column no 11 (means 10 no, in the code)
Column Name uniqueColumnSNo
Data type of Column -integer.
I am confused with syntax used by dbhanlder.
Here is the code.
Just let me know , HOW I WILL ACHIEVE MAXIMUM VALUE TO BE RETURNED from the specified column.
Help. Thanks in Advance.
+(int)getMaxColumnSNo
{
NSInteger favid;
int count=0;
sqlite3 *database;
NSString *dbpath;
dbpath = [dbhandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:#"SELECT max(uniqueColumnSNo) FROM CustomerFields"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
//[result addObject:[[NSMutableDictionary alloc] init]];
favid=(NSInteger)sqlite3_column_int64(statement,10);
//favid = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)] intValue];
count++;
}
sqlite3_finalize(statement);
}
else
{
NSLog(#"Sql Preparing Error");
}
sqlite3_close(database);
}
else
{
NSLog(#"Database not opening");
}
int i;
if (count!=0) {
i=favid ;
}
else {
i=1;
}
return i;
//return favid;
}
You need:
sqlite3_column_int64( statement, 0 );
The column number parameter is the index of the result set, not of the original table column number. Your only column in the result set is max(uniqueColumnSNo), thus this is column index number 0.
Related
The code runs fine if the commented portion remains commented.The while loop should run two times and it does cause there are only two rows in threads table.However,if i uncomment the commented code the while loop runs only one time which is the wrong output
sql = "select thread_id,timestamp from threads";
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString* abc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSString* def = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(#"%#",abc);
NSLog(#"%#",def);
NSString* threadid = abc;
NSString *sql2 = [NSString stringWithFormat:#"select * from my where t_id=\"%#\"",threadid];
**Commented code begin**
/* sqlite3_finalize(selectstmt);
sqlite3_open([path UTF8String], &database);
if(sqlite3_prepare_v2(database, [sql2 UTF8String], -1, &selectstmt2, NULL) == SQLITE_OK) {
int a = sqlite3_data_count(selectstmt2);
NSLog(#"%d",a);
if (a==1) {
sql2 = [NSString stringWithFormat:#"select timestamp from my where t_id=\"%#\"",threadid];
if(sqlite3_prepare_v2(database, [sql2 UTF8String], -1, &selectstmt2, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString* abc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt2, 0)];
NSLog(#"%#",abc);
}
}
}
if(a==0) {
char* error;
sql2 = [NSString stringWithFormat:#"insert into my(t_id,time) values(\"%#\",\"%#\")",threadid,#"0"];
int i = sqlite3_exec(database, [sql2 UTF8String], NULL, NULL, &error) ;
NSLog(#"inserted");
sqlite3_finalize(selectstmt);
}
}*/
**Commented code end**
**program end**
You are finalizing the prepared statement, inside the same loop you are processing it in. Not surprisingly, the next time you attempt to use the statement, it fails. Note that, each of the calls you make to the statement, after it is has been finalized, returns an error code that you can check before proceeding with additional logic.
Please find the below function.it works fine for the first time.
I mean , when i add a text it works for the first time , then when i try to add a new text again , it give me like "DB is locked".
I am not sure how it works for the first not again?
Please let me know what is the problem in my code.
//==================================================================
- ( BOOL ) addNewSimpleTemplates:(NSString*)dbPath:(NSString*)title{
//==================================================================
BOOL returnVal = NO;
NSString *maxValuePosition;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat:#"select MAX(pr.position) FROM phrase_reference pr inner join storyboard_main_categories smc on smc.id = pr.main_category_id where smc.category_name = %#", #"'Simple Templates'"];
const char *sql = [selectSQL UTF8String];
sqlite3_stmt *selectStmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
//
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
if (localityChars == NULL)
maxValuePosition = nil;
else
maxValuePosition = [NSString stringWithUTF8String: localityChars];
// increment the postion value
int postion = [maxValuePosition intValue] + 1;
NSLog(#"%d look here %# gtitle", postion , title);
selectStmt = nil;
//saving a new simple phrase is started here
sql = "insert into storyboard_phrases(phrase) Values(?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_text(selectStmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s just here itself", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
///
int phrase_id = sqlite3_last_insert_rowid(database);
int sub_category_id = 0;
selectStmt = nil;
NSString *selectSQL = [NSString stringWithFormat:#"select id from storyboard_main_categories where category_name = %#", #"'Simple Templates'"];
sql = [selectSQL UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
NSString* main_category_id = [NSString stringWithUTF8String: localityChars];
sql = "insert into phrase_reference (phrase_id, sub_category_id,main_category_id, position) Values(?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_int(selectStmt, 1, phrase_id);
sqlite3_bind_int(selectStmt, 2, sub_category_id);
sqlite3_bind_int(selectStmt, 3, [main_category_id intValue]);
sqlite3_bind_int(selectStmt, 4, postion);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
returnVal = YES;
}
}
}
sqlite3_finalize(selectStmt);
selectStmt = nil;
/////
}
}
}
sqlite3_finalize(selectStmt);
}
NSLog(#"sdsdfsdsd closed");
sqlite3_close(database);
return returnVal;
}
I am not going deep within your code but make sure that -
Dont forget to finalize the statements before close the database.
And do reset after every execution of the sqlite3 statement
You are not using reset statement any where.. Try using reset statement..
I have a SQLite database that I am creating in my iOS application. A series of numbers are being stored in this database. I want to sum the entire column, and return the data to be displayed within the application.
Everything writing to the DB is working properly, but I am stuck trying to return the summed data. Any help would be greatly appreciated.
-(void) dataReturn: (NSString *) tableNamed{
NSString *myData = [NSString stringWithFormat:#"SELECT SUM(column1) AS data1 FROM myDB", tableNamed];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [myData UTF8String], -1, &statement, nil) ==SQLITE_OK){
while (sqlite3_step(statement) == SQLITE_ROW){
int *field2 = (int *) sqlite3_column_int(statement, 1);
NSString *myString =[[NSString alloc] initWithFormat:#"%#", field2];
}
}
}
Hello Nathan making a call as
[self dataReturn:#"Bill"];
to function
-(void)dataReturn:(NSString *)tableName{
sqlite3 *database;
sqlite3_stmt *statement;
NSString *queryString = [NSString stringWithFormat:#"SELECT SUM(price) AS TOTAL FROM %#", tableName];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, [queryString UTF8String], -1, &statement, nil) == SQLITE_OK){
while (sqlite3_step(statement) == SQLITE_ROW) {
int field1 = sqlite3_column_int(statement, 0);
NSLog(#"The sum is %d ", field1);
}
}
}
}
will Fetch you desired data. The schema for Table "Bill" is "CREATE TABLE Bill (price double,quantity INTEGER)". The result fetched will have columns indexed from "0" so we pass 0 for first column. Hope you can take some hint from it. Cheers!!
int field1 = sqlite3_column_int(statement, 0);
From the sqlite3_column_int docs:
The leftmost column of the result set has the index 0.
Additionally, that function returns an int, not an int*. Try:
int field2 = sqlite3_column_int(statement, 0);
I am getting EXC_BAD_ACCESS when I attempt to do anything with the value I'm selecting from the local SQLITE database on an iPhone development. The code for my function is
-(void) updateFromDB {
// Setup the database object
sqlite3 *database;
// Open DB
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *query = [NSString stringWithFormat:#"SELECT MOD_ID FROM MODULE;"];
//NSLog(#"QUERY: %#",query);
// Prepare statement
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {
// Execute SQL
while (sqlite3_step(statement) == SQLITE_ROW) {
// Get MOD_IDs
NSInteger MOD_ID = sqlite3_column_int(statement, 0);
NSString *ID = [NSString stringWithFormat:#"%#",MOD_ID];
//=======================
// Get Notice Module Data
//=======================
if (MOD_ID == 1) {
self.noticeModule = [[ModuleSetting alloc] initWithID:ID];
}
}
} else {
NSAssert1(0,#"Error: failed to prepare statement. '%s'", sqlite3_errmsg(database));
}
// Release the compiled statement from memory
sqlite3_finalize(statement);
} else {
sqlite3_close(database);
NSAssert1(0,#"Failed to open database. '%s'",sqlite3_errmsg(database));
}
sqlite3_close(database);
}
The bad access occurs on the line
NSString *ID = [NSString stringWithFormat:#"%#",MOD_ID];
Thanks for any help you can offer!
%# denotes objects. But MOD_ID seems to be an integer. So your format should be %d,
NSString *ID = [NSString stringWithFormat:#"%d", MOD_ID];
You can't use %# in format strings for integers, only for Obj-C objects. For integers, use %d (or for NSInteger, I think it is recommended to use %ld).
Have a look at the String Format Specifiers guide.
MOD_ID is not a pointer, so %# isn't correct.
Use below
NSInteger MOD_ID = sqlite3_column_int(statement, 0);
NSString *ID = [NSString stringWithFormat:#"%d",MOD_ID];
Say i've got a table called Incidents, and a table called Devices.
Each device has a CI-code.
I want to create a tableView, with in the sections the names of the devices (CI), and each row to represent an incident.
To get the correct number of rows under each section, I use the following code:
NSMutableArray *lijstDevices = [[NSMutableArray alloc] init];
const char *dbpath = [appDelegate.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT DISTINCT CI FROM Devices"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
// NSLog(#"SQLite ROW for filling in cell");
NSString *addDev = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
[lijstDevices addObject:addDev];
}
sqlite3_finalize(statement);
}
NSString *ciNaam = [lijstDevices objectAtIndex:section];
NSString *querySQL2 = [NSString stringWithFormat:#"SELECT COUNT(ID) FROM Incidents WHERE CI='%#'", ciNaam];
const char *query_stmt2 = [querySQL2 UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt2, -1, &statement, NULL) == SQLITE_OK)
{
//NSLog(#"SQLite ok in rowforindexpath");
if(sqlite3_step(statement) == SQLITE_ROW) {
// NSLog(#"SQLite ROW for filling in cell");
teller = sqlite3_column_int(statement, 0);
}
else
{
NSLog(#"Not.. good...");
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
Here comes the problem: The sections are filled correctly. Some sections need 1 row, other sections need 2 rows. Whenever I run the program, each section which needs 1 row ends up with 0 rows, while each section with 2 rows needed end up with only 1 row.
Besides that, I had an error before which didn't clean the tables. Therefore, if I ran it twice, the database got doubled up on rows. In that case, each one which needed 1, has 2 rows. Each one which needed 2, had 4 rows. This is obviously not the case anymore but it might help understanding what's wrong.
Anyone having any idea???
u see this example and check it
-(void)ChekInDataBase{
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT count(id) as countRow FROM password"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStatement, NULL) == SQLITE_OK)
{
NSLog(#"%#",sqlStatement);
if (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
countRow = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0 )]intValue] ;
}
}
sqlite3_finalize(compiledStatement);
if (countRow > 0)
{
[yesBtnPressed setImage:[UIImage imageNamed:#"yes_tab_over.png"]forState:UIControlStateNormal];
[noBtnPressed setImage:[UIImage imageNamed:#"no_tab.png"]forState:UIControlStateNormal];
[noBtnPressed setUserInteractionEnabled:TRUE];
[yesBtnPressed setUserInteractionEnabled:FALSE];
}
else
{
[yesBtnPressed setImage:[UIImage imageNamed:#"yes_tab.png"]forState:UIControlStateNormal];
[noBtnPressed setImage:[UIImage imageNamed:#"no_tab_over.png"]forState:UIControlStateNormal];
[noBtnPressed setUserInteractionEnabled:FALSE];
[yesBtnPressed setUserInteractionEnabled:TRUE];
}
sqlite3_close(database);
}
}
There was no error in this line. Apparently, the CI-code didn't get added in the table correctly. Sometimes it shows the CI-code correct (the ones I can see in the list), but the others just have (null) or 1 as code.