您可以使用以下代码:
- (void)SaveAction {
NSString * databasefile = [[NSBundle mainBundle] pathForResource:@\"SavedPapers\" ofType:@\"db\"];
sqlite3 *database = NULL;
// Open the database and return a database identifier variable
if (sqlite3_open([databasefile UTF8String], &database) == SQLITE_OK) {
// Create and prepare template insert statements (one for each field we want to save)
const char * SaveCommand = \"insert into papers values(?, ?, ?, ?)\";
if ( sqlite3_prepare_v2(database, SaveCommand, -1, &SaveCommand_stmt, NULL) != SQLITE_OK) {
NSAssert1(0, @\"Error while creating save statement. '%s'\", sqlite3_errmsg(database));
}
// Insert the data into the save statement
sqlite3_bind_text(SaveCommand_stmt,1,[currentTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(SaveCommand_stmt,2,[currentAuthor UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(SaveCommand_stmt,3,[currentSummary UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(SaveCommand_stmt,4,[currentLink UTF8String], -1, SQLITE_TRANSIENT);
// Execute the SQLITE command
if (SQLITE_DONE != sqlite3_step(SaveCommand_stmt)) {
NSAssert1(0, @\"Error while saving data. '%s'\", sqlite3_errmsg(database));
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@\"Save\" message:@\"Error saving paper to database... :(\" delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
} else {
UIAlertView * GoodAlert = [[UIAlertView alloc] initWithTitle:nil message:@\"Paper Saved!\" delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
[GoodAlert show];
[GoodAlert release];
}
// Reset the command contents and close the database
sqlite3_reset(SaveCommand_stmt);
sqlite3_close(database);
}
我认为希望是有用的。