我有一个带有 tableView 的数据库,我在使用终端制作的 sqlite 文件中将数据库分开,还有一个视图允许我创建数据然后将其输入到数据库中,尽管当我单击保存时它没有t 似乎得救了。
这是我的 sqlite 脚本:
-(void) addPatientToDatabase:(Patient *)newPatient {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into patients (firstName, surname, dob, homeNumber, mobileNumber, email, address, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [newPatient.patientName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 2, [newPatient.patientSurname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 3, [newPatient.patientDoB UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 4, [newPatient.patientHomeNumber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 5, [newPatient.patientMobileNumber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 6, [newPatient.patientEmail UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 7, [newPatient.patientAddress UTF8String], -1, SQLITE_TRANSIENT);
NSData *dataForPicture = UIImagePNGRepresentation(newPatient.patientPicture);
sqlite3_bind_blob(compiledStatement, 8, [dataForPicture bytes], [dataForPicture length], SQLITE_TRANSIENT);
}
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
sqlite3_finalize(compiledStatement);
}
}
sqlite3_close(database);
}
和:
Patient *newPatient = [[Patient alloc] init];
newPatient.patientName = firstnameEntry.text;
newPatient.patientSurname = surnameEntry.text;
newPatient.patientDoB = dobEntry.text;
newPatient.patientHomeNumber = homeNumberEntry.text;
newPatient.patientMobileNumber = mobileNumberEntry.text;
newPatient.patientEmail = emailAddressEntry.text;
newPatient.patientAddress = addressEntry.text;
newPatient.patientPicture = patientPicture;
[patients addObject:newPatient];
[self addPatientToDatabase:newPatient];
和:
-(NSString *)copyDatabaseToDocuments {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsPath = [paths objectAtIndex:0];
NSString *filepath = [documentsPath stringByAppendingPathComponent:@"patients.sqlite"];
if (![fileManager fileExistsAtPath:filepath]) {
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"patients.sqlite"];
[fileManager copyItemAtPath:bundlePath toPath:filepath error:nil];
}
return filepath;
}
-(void) readPatientsFromDatabaseWithPath:(NSString *)filePath {
sqlite3 *database;
if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStateMent = "select * from patients";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare(database, sqlStateMent, -1, &compiledStatement, NULL) == SQLITE_OK) {
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *firstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *dob = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *homeNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *mobileNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSString *email = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSString *address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
NSData *patientData = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 8) length:sqlite3_column_bytes(compiledStatement, 8)];
UIImage *patientImage = [UIImage imageWithData:patientData];
Patient *newPatient = [[Patient alloc] init];
newPatient.patientName = firstName;
newPatient.patientSurname = surname;
newPatient.patientDoB = dob;
newPatient.patientHomeNumber = homeNumber;
newPatient.patientMobileNumber = mobileNumber;
newPatient.patientEmail = email;
newPatient.patientAddress = address;
newPatient.patientPicture = (UIImage *)patientImage;
[self.patients addObject:newPatient];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
好像不喜欢if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
我尝试在读取中间放置一个断点,它也没有到达断点
抱歉,代码太多,文字不多。
如果您想要更多代码或详细信息,请说。
提前致谢