我试图在 sqlite3 数据库中插入值,但它无法在我的应用程序上运行。我遵循这段代码,其中表中的所有这些值都是 varchar。
sqlite3_stmt *statement;
NSString *docsDir;
NSArray *dirPaths;
int ID;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"weather.sqlite"]];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO satish VALUES ('jodhpur','hazy1','68','7','700','30','28','10sept')"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
{
NSLog(@"%@",statement);
sqlite3_bind_text(statement, 1, insert_stmt, -1, SQLITE_TRANSIENT );
if (sqlite3_step(statement) == SQLITE_ROW)
{
ID = sqlite3_last_insert_rowid(contactDB);
NSLog(@"Data inserted successfully ");
}
else{
NSLog(@"Failed to add contact");
}
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}