I am making a quiz that reads from the database in SQL and puts the question in the label view and the answers in the textfield. I have done a lot of tutorials that have similar solutions but they use a table view. Here is my 开发者_StackOverflowattempt, I of course checked to see if I had a database and used:
-(void) readProjectsFromDatabase {
sqlite3 *database;
projects = [[NSMutableArray alloc] init];
// 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 * from t1";
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
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
// Create a new project object with the data from the database
Project *project = [[Project alloc] initWithName:aName description:aDescription url:aImageUrl];
// Add the project object to the project Array
[projects addObject:project];
[project release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
Are you trying to initialize the array using the database file ? I am not sure if the database file can be used like this to initialize the array.
Why don't you use the projects array to set the fields ? It appears that it is successfully being populated in your -(void) readProjectsFromDatabase
Also try to put NSLog
statements to see if the array is actually populated, if it is, then also check to see if field1
and field2
are properly connected in the interface builder to the corresponding outlets
精彩评论