我正在尝试创建一个SQLite数据库,但遇到的问题似乎是从表中从未创建的CONTACTS(我收到错误消息'插入时出错'没有这样的表:CONTACTS' “)。
从下面的代码中,任何人都可以看到错误以及如何纠正错误?这个例子直接来自本指南,这就是为什么我感到惊讶它不起作用: http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_6_iPhone_Application
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
//Buid the path for database file
_databasePath = [[NSString alloc]
initWithString:[docsDir stringByAppendingPathComponent:@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *db_path = [_databasePath UTF8String];
if(sqlite3_open(db_path, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_text.text = @"Failed to create table";
}
sqlite3_close(_contactDB);
} else {
_text.text = @"Failed to open/create database";
}
}
}
- (IBAction)saveName:(id)sender {
NSString *currentName = self.text.text;
NSString *nameSaved = [[NSString alloc] initWithFormat:currentName];
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSLog(@"pass");
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (name) VALUES (\"%@\")",
nameSaved];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"pass");
} else {
NSLog( @"Error while inserting '%s'", sqlite3_errmsg(_contactDB));
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
答案 0 :(得分:0)
使用objectAtIndex
:
sqlite3* _contactDB;
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
//Buid the path for database file
NSString * _databasePath = [[NSString alloc]
initWithString:[docsDir stringByAppendingPathComponent:@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *db_path = [_databasePath UTF8String];
if(sqlite3_open(db_path, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table") ;
}
sqlite3_close(_contactDB);
} else {
NSLog( @"Failed to open/create database");
}
}
答案 1 :(得分:0)
该代码没有任何问题 - 它可以在Mac OS X和iOS模拟器中正常运行。当您尝试将数据插入表中时,错误是否会出现?在尝试向其中插入数据之前,您是在关闭文件吗?
在模拟器中运行它,逐步执行代码,然后在模拟器的Documents目录中找到该文件。使用终端查看文件中的内容:
sqlite3 path-to-file .dump
您的计算机上的确切路径会有所不同,但在我的iOS模拟器中,我得到:
sqlite3 "/Users/my-login-name/Library/Application Support/iPhone Simulator/6.1/Applications/DAD0BB47-2136-4367-8DAF-AA578D0C00A5/Documents/contacts.db" .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT);
COMMIT;
您可以看到该表存在正确的列。