我遵循了有关创建 SQLite 数据库、存储内容和查找它的教程。我没有做的是创建界面,因为控制台日志记录可以快得多。(这也不需要其他人来构建交互,但不要忘记导入库!:))
这是重要的教程: http ://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_6_iPhone_Application
我在查找数据时遇到问题,有人知道它可能是什么吗?
- (void) findContact {
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:
@"SELECT adress, phone FROM contacts WHERE name=\"%@\"", @"DoekeW"];
const char *query_stmt = [querySQL UTF8String];
if(sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
if(sqlite3_step(statement) == SQLITE_ROW) {
NSString *adressField = [[NSString alloc] initWithUTF8String:
(const char*) sqlite3_column_text(statement, 0)];
NSString *phoneField = [[NSString alloc] initWithUTF8String:
(const char*) sqlite3_column_text(statement, 1)];
NSLog(@"adressField: %@ phoneField: %@", adressField, phoneField);
}
else {
NSLog(@"no match");
}
sqlite3_finalize(statement);
}
else {
// 1, whatever that is
NSLog(@"hmm %d", sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL));
}
sqlite3_close(_contactDB);
}
}
这是完整的代码:
.h 文件:
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface IGViewController : UIViewController
@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic) sqlite3 *contactDB;
@end
.m 文件
#import "IGViewController.h"
@interface IGViewController ()
@end
@implementation IGViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]];
NSLog(@"%@", _databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath] == NO) {
const char *dbpath = [_databasePath UTF8String];
if(sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
//_status.text = @"Failed to create table";
NSLog(@"Failed to create table");
}
sqlite3_close(_contactDB);
} else {
// _status.text = @"Failed to open/create database";
NSLog(@"Failed to open/create database");
}
}
else {
NSLog(@"database allready existed");
}
[self saveData];
[self findContact];
}
- (void) saveData {
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",
@"DoekeW", @"Molenstraat", @"483577"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"saved data!");
}
else {
NSLog(@"something wrong, no data saved");
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
- (void) findContact {
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:
@"SELECT adress, phone FROM contacts WHERE name=\"%@\"", @"DoekeW"];
const char *query_stmt = [querySQL UTF8String];
if(sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
if(sqlite3_step(statement) == SQLITE_ROW) {
NSString *adressField = [[NSString alloc] initWithUTF8String:
(const char*) sqlite3_column_text(statement, 0)];
NSString *phoneField = [[NSString alloc] initWithUTF8String:
(const char*) sqlite3_column_text(statement, 1)];
NSLog(@"adressField: %@ phoneField: %@", adressField, phoneField);
}
else {
NSLog(@"no match");
}
sqlite3_finalize(statement);
}
else {
// 1, whatever that is
NSLog(@"hmm %d", sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL));
}
sqlite3_close(_contactDB);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end