我刚开始学习SQLite
与 iPhone 应用程序的接口,我没有遇到错误,但我无法使用以下代码将值插入数据库。
我无法弄清楚错误在哪里蔓延,为了看到一个插入,我已经 NSlogged 一个字符串显示在调试器中,但代码还没有到达那里,你能帮我解决这个问题吗?
我尝试查看其他帖子,但无法从中获得太多信息,我发现我的语法和语句是正确的,并且在看到 SO 上的特定帖子后,我意识到我在某些地方犯了错误,然后更正了。
但它仍然无法正常工作。我在大学拥有的系统上工作,我无法访问 mac 磁盘文件是否看到我的数据库正在填充甚至创建..是否有设置创建数据库的权限的东西。(我真的怀疑),如果是这样,那么我如何在文件系统的其他地方创建一个数据库以供访问,或者下面的代码有什么问题,如果您需要任何其他文件,请发表评论,以便我可以将它们放在这里...
vc.m
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrayOfPerson;
sqlite3 *personDB;
NSString *dbPathString;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfPerson = [[NSMutableArray alloc]init];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
[self createOrOpenDB];
}
-(void)createOrOpenDB{
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath=[path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"persons.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath=[dbPathString UTF8String];
//creating DB
if (sqlite3_open(dbPath, &personDB)==SQLITE_OK) {
const char *sql_stmt="CREATE TABLE IF NOT EXISTS PERSONS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT,AGE INTEGER";
sqlite3_exec(personDB, sql_stmt, NULL, NULL, &error);
NSLog(@"Created !");
sqlite3_close(personDB);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)addPersonButton:(id)sender {
char *error;
if (sqlite3_open([dbPathString UTF8String], &personDB)==SQLITE_OK) {
NSString *insertStmt = [NSString stringWithFormat:@"INSERT INTO PERSONS(NAME,AGE) VALUES ('%s','%d')",[self.nameField.text UTF8String],[self.ageField.text intValue]];
const char *insert_stmt = [insertStmt UTF8String];
if (sqlite3_exec(personDB, insert_stmt, NULL
, NULL, &error)==SQLITE_OK) {
NSLog(@"Person Added");
Person *person = [[Person alloc]init];
[person setName:self.nameField.text];
[person setAge:[self.ageField.text intValue]];
[arrayOfPerson addObject:person];
}
sqlite3_close(personDB);
}
}
- (IBAction)displayPersonButton:(id)sender {
sqlite3_stmt *statement;
if(sqlite3_open([dbPathString UTF8String], &personDB)==SQLITE_OK){
[arrayOfPerson removeAllObjects];
NSString *querySql=[NSString stringWithFormat:@"SELECT *FROM PERSONS"];
const char *query_sql=[querySql UTF8String];
if(sqlite3_prepare(personDB, query_sql, -1, &statement, NULL)==SQLITE_OK){
while(sqlite3_step(statement)==SQLITE_ROW){
NSString *name=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement,1)];
NSString *ageString=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];//used sqlite3_column_int here before ! Mistake
Person *person = [[Person alloc] init];
[person setName:name];
[person setAge:[ageString intValue]];
[arrayOfPerson addObject:person];
}
}
}
[[self myTableView] reloadData];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[[self ageField]resignFirstResponder ];
[[self nameField]resignFirstResponder];}
- (IBAction)deletePersonButton:(id)sender {
}
@end
vc.h
#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "Person.h"
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *ageField;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (IBAction)addPersonButton:(id)sender;
- (IBAction)displayPersonButton:(id)sender;
- (IBAction)deletePersonButton:(id)sender;
@end
中间是否有任何错误,我尽力检查了它,但在语法上找不到任何错误