views:

36

answers:

3

Hello all,

I'm new to Objectiv-C and i try to get my database SQLite to work, but i get sommene trobble i hobe i can get sommen help here.

That i will have my code to doe its bind all "region_title" to tableview, and "region_id" to the id like "html" when you use "options"

i have try to search about it but i can't find help to my problem, i hob sombardy can help me here.

say if you need more code samples from me, you can recode this code if you think its better, right now i relly need to try this and get it to work. :0)

tanks a lot for all help from all.

   const char *sql = "SELECT region_id, region_title FROM region";
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) 
    {
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSString *aRegionId = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] autorelease];
            NSString *aRegionTitle = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] autorelease];

            MapClass *mapclass = [[MapClass alloc] initWithName:aRegionTitle region_id:aRegionId];
            [todoArray addObject:mapclass];

        }

        self.listData = todoArray;
    }
A: 

You should remove the autorelease messages, since [NSString stringWith...] returns autoreleased strings already. Furthermore, numbering starts at 0.

NSString *aRegionId = [NSString stringWithUTF8String:
                                    (char *)sqlite3_column_text(statement, 0)];
NSString *aRegionTitle = [NSString stringWithUTF8String:
                                    (char *)sqlite3_column_text(statement, 1)];

should work.

(and, strictly speaking, you should autorelease the MapClass object!)

mvds
Hmm, nop its not working :/
NeoNmaN
Then you have even more problems ;-) maybe enlighten us a little as to *what* goes wrong?
mvds
I have maket a Answers, its work now just a lillte detail to get a name on it :0)
NeoNmaN
A: 

Its work fine if i use this

            NSString *rowData = [NSString stringWithUTF8String: sqlite3_column_text(statement, 0)];
            NSString *RegionTitle = [NSString stringWithUTF8String: sqlite3_column_text(statement, 1)];

            [mapArray addObject:rowData];
            NSLog(@"rowData: %@", rowData); 

Now i only need to get RegionTitle to a label text, soe my ID not its the label text, and my jobs done :) can you help whit that?

NeoNmaN
you should update your question to reflect the new situation (just edit the code in there) and elaborate a little more on what goes wrong. (and not post another question or update as an answer)
mvds
A: 

Since I do not see any error message or more details to what is wrong (or your database structure), I think your query is the problem. What are the data types of region_id and region_table? I assumed int and 'string'.

assuming this works:

NSString *query = [NSString stringWithFormat:@"select id, tableview from region"];

then try this:

NSString *query = [NSString stringWithFormat:@"select `%i`, `%@` from `region`", region_id, region_table]; // assuming region_id is int and region_table is a string of some sort...

if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK) 
    {
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSString *aRegionId = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            NSString *aRegionTitle = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];

            MapClass *mapclass = [[MapClass alloc] initWithName:aRegionTitle region_id:aRegionId];
            [todoArray addObject:mapclass];

        }

        self.listData = todoArray;
    }

Note that I changed your query to an NSString... your choice (no need to release as the above posts already stated), but in doing so, you have to convert it to UTF8 in this line:

if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)

of course, you are not catching NULLs in your statement, so you may throw in a "where %@ is not null" into your query...

J. Dave
This is a really weird piece of code. What's the point of that first line? the query will make no sense at all.
mvds
NeoNman's question was very vague.The first line is the query statement, which NeoNmaN indicated needed to be 'id' and 'tableview'... unless I am mistaken.So %i or %@ are placeholders to store the id and tableview's int and text values (if he renamed them, which is what it seemed in his/her initial post... unless I am mistaken.I was going for generalization, to show the point of the query, not the exact answer in this case.Yes, I did forget to omit "sqlite3_stmt *statement;"...but that was not the point I was trying to show.Without knowning the table structure,an exact query in anyone's guess.
J. Dave