我有一个UITextview作为注释,当用户输入一些文本并点击保存按钮时,注释视图将为空,文本应保存在数据库中,重新打开应用程序后,相应的文本应显示在相应的标题上。
如何使用sqlite数据库执行这些操作?
这就是我的尝试。
-(void)readText
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
sqlite3 *database;
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
NSString *query=@"select Notes from dataTable where title like ?";
const char *sqlStatement=[query UTF8String];
sqlite3_stmt *compiledStatement;
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if( sqlite3_prepare_v2(database, sqlStatement, -1, & compiledStatement, NULL) == SQLITE_OK )
{
sqlite3_bind_text(compiledStatement,1,[selString UTF8String],-1,SQLITE_STATIC);
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString * pno3=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,9)];
[loadNotes addObject:pno3];
}
}
}
}
-(IBAction)saveText {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
sqlite3 *database;
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "insert into dataTable(Notes) values(?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
NSString * data9=[NSString stringWithFormat:@"%@",[loadNotes objectAtIndex:0]];
sqlite3_bind_text(compiledStatement,9,[data9 UTF8String],-1,SQLITE_STATIC);
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}