Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - Variables/parameters in Sqlite query for Iphone App

In my Iphone App I have created this query:


"SELECT * FROM visuel where id_visuel = 1"

And so I have this function that works very well:


- (void)getVisuel {

    visuelArray = [[NSMutableArray alloc] init];

    sqlite3 *database;

    if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
        sqlite3_reset(getVisuelStatement);

        while(sqlite3_step(getVisuelParcStatement) == SQLITE_ROW) {
            NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getVisuelStatement , 2)];
            NSString *aLpath = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getVisuelStatement , 3)];

            Visuel *aVisuel = [[Visuel alloc] initWithName:aTitle lpath:aLpath];

            [visuelArray addObject:aVisuel];
        }
    }
    sqlite3_close(database);

}

What I want is to change the query like this: "SELECT * FROM visuel where id_visuel = ?" I don't want to have a static id, but I don't know how to do that.

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, first change your query to your parameterized query like you have there. Then, just before you call sqlite3_step bind the right id to the parameter:

sqlite3_reset(getVisuelStatement);

//Add these two lines
int visuelId = 1;  //Put whatever id you want in here.
sqlite3_bind_int(getVisuelParcStatement, 1, visuelId);

while(sqlite3_step(getVisuelParcStatement) == SQLITE_ROW) {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...