从 Xcode 中将数据添加到 SQLite3 DB
我正在尝试将 3 条数据添加到 SQLite3 DB 中。在教程的帮助下,我快要成功了。下面是我的代码。
我收到了我期望从 NSLogs
获得的信息。我发现数据库没有问题,我编译语句发送到数据库没有问题,但是当应用程序到达 sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK
我期望将数据添加到 DB
中,但事实并非如此,而且我还期望显示一个 UIAlertView
来告诉用户错误或成功。我希望你能看到我看不到的东西。
- (IBAction)addData:(id)sender {
NSString *n = [[NSString alloc] initWithString:[name text]];
NSString *a = [[NSString alloc] initWithString:[age text]];
NSString *c = [[NSString alloc] initWithString:[color text]];
NSLog(@"%@ - %@ - %@",n,a,c);
[self insertDataName:n Age:a Color:c];
}
此 NSLog 按预期返回文本属性。
- (IBAction)hide:(id)sender {
[name resignFirstResponder];
[age resignFirstResponder];
[color resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
DBName = @"mydatabase.sqlite";
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPaths objectAtIndex:0];
DBPath = [documentsDir stringByAppendingPathComponent:DBName];
}
-(void)checkAndCreateDB {
BOOL Success;
NSFileManager *fileManager = [NSFileManager defaultManager];
Success = [fileManager fileExistsAtPath:DBPath];
if(Success) {
NSLog(@"DB Found");
return;
}
NSLog(@"DB Not Found");
NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
[fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
}
此 NSLog
返回 DB Found
-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c {
[self checkAndCreateDB];
sqlite3 *database;
if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) {
NSString *statement;
sqlite3_stmt *compliedstatement;
statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c];
const char *sqlstatement = [statement UTF8String];
NSLog(@"%@",statement);
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {
if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database));
UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[AlertOK show];
}
else {
UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[AlertOK show];
}
}
sqlite3_finalize(compliedstatement);
}
sqlite3_close(database);
}
并且上面的最后一个 NSLog
显示 insert into table1 值 ('n', 'a', 'c ')
正如我所期望的那样。
插入时出现问题,我无法弄清楚。
仅供参考,数据库是使用 SQLiteManager 创建的。下面的 SS:
I am trying to add 3 pieces of data to a SQLite3 DB. With help from a tutorial, I'm almost there. Below is my code.
I am receiving what I would expect from the NSLogs
. I find the DB without problem, I compile the statement to send to the DB without problem, but when the app gets to sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK
I am expecting the data to be added to the DB
, but it isn't, and I'm also expecting to display a UIAlertView
to tell the user ERROR os SUCCESS. I hope you can see what I do not.
- (IBAction)addData:(id)sender {
NSString *n = [[NSString alloc] initWithString:[name text]];
NSString *a = [[NSString alloc] initWithString:[age text]];
NSString *c = [[NSString alloc] initWithString:[color text]];
NSLog(@"%@ - %@ - %@",n,a,c);
[self insertDataName:n Age:a Color:c];
}
This NSLog
returns the text properties as expected.
- (IBAction)hide:(id)sender {
[name resignFirstResponder];
[age resignFirstResponder];
[color resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
DBName = @"mydatabase.sqlite";
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentsPaths objectAtIndex:0];
DBPath = [documentsDir stringByAppendingPathComponent:DBName];
}
-(void)checkAndCreateDB {
BOOL Success;
NSFileManager *fileManager = [NSFileManager defaultManager];
Success = [fileManager fileExistsAtPath:DBPath];
if(Success) {
NSLog(@"DB Found");
return;
}
NSLog(@"DB Not Found");
NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
[fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
}
This NSLog
returns DB Found
-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c {
[self checkAndCreateDB];
sqlite3 *database;
if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) {
NSString *statement;
sqlite3_stmt *compliedstatement;
statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c];
const char *sqlstatement = [statement UTF8String];
NSLog(@"%@",statement);
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {
if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database));
UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[AlertOK show];
}
else {
UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[AlertOK show];
}
}
sqlite3_finalize(compliedstatement);
}
sqlite3_close(database);
}
And the last NSLog
above displays insert into table1 values ('n', 'a', 'c')
as I would expect.
Something is going wrong upon insertion and I can't figure it out.
FYI the DB was created using SQLiteManager. SS below:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您是在模拟器上工作还是在 iDevice 上工作?在 iDevice 中,您无法创建外部 SQL 数据库(至少据我所知)。由于沙箱的原因,您必须动态创建它。 “仅供参考,数据库是使用 SQLiteManager 创建的。”检查一下。
如果这不是问题,请尝试将代码从“准备...”更改为“执行”
从...
到...
记录“错误”,因为这是非常重要的。
如果你想弹出一个 UIAlert 这样做
Ok, Are you working on a simulator or an iDevice? In iDevice, you cannot create an external SQL database (at least to my knowledge). You have to create it dynamically because of the sandboxing. "FYI the DB was created using SQLiteManager." Check on that.
If that was not the problem try changing your code from "prepare ..." to "execute"
From...
To...
Do a log on "error" because that is very essential.
if you wanted to pop a UIAlert do this