iPhone 上的 SQLite 表无效?
我一直在尝试通过我的 iPhone 访问 SQLite3 数据库,但我不断收到“no such table: user_info”错误。
因此,以下是我所经历的步骤:
通过命令行创建数据库:
- sqlite3 users.sqlite
- 创建表user_info(名称文本,信息文本);
- 插入 user_info value('Name1', '这是 Name1 的信息');
- 从用户信息中选择*;
- [结果]:Name1|这是 Name1 的信息
- 从sqlite_master中选择*;
- [结果]:表|user_info|user_info|3|创建表user_info(名称文本,信息文本)
将其复制到 XCode 中的资源文件夹中,并可以选择将其复制到适当的目录。
尝试访问它并收到错误“no such table: user_info”。
好吧,那我#3 做得怎么样?好吧,我已经更新了一些,所以现在我尝试创建不存在的表。这是我当前的代码:
static NSString *dbname = @"users.sqlite";
-(NSString *) dbFilePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES
);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
fprintf(stderr, "Database is not writeable!\n");
}
return dbPath;
}
- (void)createEditableCopyOfDatabaseIfNeeded {
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *writeableDBPath = [self dbFilePath];
success = [fileManager fileExistsAtPath:writeableDBPath];
if (success) {
return;
}
NSString *defaultDBPath =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:dbname];
success = [fileManager
copyItemAtPath:defaultDBPath
toPath:writeableDBPath
error:&error];
if (!success) {
fprintf(stderr, "Failed to create writable database file\n");
}
}
-(void) openDB {
if (sqlite3_open([[self dbFilePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
fprintf(stderr, "Database failed to open.\n");
}
}
-(void) createTableIfNeeded {
NSString *result = @"";
sqlite3_stmt *statement;
char *sql = sqlite3_mprintf(
"CREATE TABLE IF NOT EXISTS user_info (name text info text);"
);
dbresult = sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL);
if (SQLITE_OK != dbresult) {
NSAssert1(0, "no user_info table!", nil);
fprintf(
stderr,
"Error in preparation of query: %s\n",
sqlite3_errmsg(db)
);
sqlite3_close(db);
return;
}
sqlite3_finalize(statement);
sqlite3_free(sql);
}
- (NSString *)getDatabaseEntry:(NSString *)i_name {
NSString *result = @"";
sqlite3_stmt *statement;
char *sql = sqlite3_mprintf(
"SELECT 'info' FROM 'user_info' WHERE name='%q'",
[i_name cStringUsingEncoding:NSASCIIStringEncoding]
);
dbresult = sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL);
if (SQLITE_OK != dbresult) {
fprintf(
stderr,
"Error in preparation of query: %s\n",
sqlite3_errmsg(db)
);
sqlite3_close(db);
return result;
}
dbresult = sqlite3_step(statement);
if (SQLITE_ROW == dbresult) {
char *nfo = (char *)sqlite3_column_text(statement, 0);
result = [NSString stringWithUTF8String:nfo];
}
sqlite3_finalize(statement);
sqlite3_free(sql);
return result;
}
// viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
[self createEditableCopyOfDatabaseIfNeeded];
[self openDB];
[self createTableIfNeeded];
}
// Later, when I have a valid user:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
...
NSString *nfo = [self getDatabaseEntry:name.text];
...
}
我已经靠在这堵墙上好几天了,所以非常感谢任何帮助。
I've been trying to access an SQLite3 database via my iphone, but I keep on getting "no such table: user_info" as an error.
So, here are the steps I've gone through:
Create the database via command line:
- sqlite3 users.sqlite
- create table user_info (name text, info text);
- insert into user_info value('Name1', 'This is info for Name1');
- select * from user_info;
- [result]: Name1|This is info for Name1
- select * from sqlite_master;
- [result]: table|user_info|user_info|3|CREATE TABLE user_info (name text, info text)
Copy this into the resources folder in XCode, with the option to copy it to the appropriate directory.
Attempt to access it and get the error "no such table: user_info".
Okay, so how am I doing #3? Well, I've updated a bit, so now I try to create the table if it's non-existent. Here is my current code:
static NSString *dbname = @"users.sqlite";
-(NSString *) dbFilePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES
);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
fprintf(stderr, "Database is not writeable!\n");
}
return dbPath;
}
- (void)createEditableCopyOfDatabaseIfNeeded {
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *writeableDBPath = [self dbFilePath];
success = [fileManager fileExistsAtPath:writeableDBPath];
if (success) {
return;
}
NSString *defaultDBPath =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:dbname];
success = [fileManager
copyItemAtPath:defaultDBPath
toPath:writeableDBPath
error:&error];
if (!success) {
fprintf(stderr, "Failed to create writable database file\n");
}
}
-(void) openDB {
if (sqlite3_open([[self dbFilePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
fprintf(stderr, "Database failed to open.\n");
}
}
-(void) createTableIfNeeded {
NSString *result = @"";
sqlite3_stmt *statement;
char *sql = sqlite3_mprintf(
"CREATE TABLE IF NOT EXISTS user_info (name text info text);"
);
dbresult = sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL);
if (SQLITE_OK != dbresult) {
NSAssert1(0, "no user_info table!", nil);
fprintf(
stderr,
"Error in preparation of query: %s\n",
sqlite3_errmsg(db)
);
sqlite3_close(db);
return;
}
sqlite3_finalize(statement);
sqlite3_free(sql);
}
- (NSString *)getDatabaseEntry:(NSString *)i_name {
NSString *result = @"";
sqlite3_stmt *statement;
char *sql = sqlite3_mprintf(
"SELECT 'info' FROM 'user_info' WHERE name='%q'",
[i_name cStringUsingEncoding:NSASCIIStringEncoding]
);
dbresult = sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL);
if (SQLITE_OK != dbresult) {
fprintf(
stderr,
"Error in preparation of query: %s\n",
sqlite3_errmsg(db)
);
sqlite3_close(db);
return result;
}
dbresult = sqlite3_step(statement);
if (SQLITE_ROW == dbresult) {
char *nfo = (char *)sqlite3_column_text(statement, 0);
result = [NSString stringWithUTF8String:nfo];
}
sqlite3_finalize(statement);
sqlite3_free(sql);
return result;
}
// viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
[self createEditableCopyOfDatabaseIfNeeded];
[self openDB];
[self createTableIfNeeded];
}
// Later, when I have a valid user:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
...
NSString *nfo = [self getDatabaseEntry:name.text];
...
}
I've been against this wall for a couple of days now, so any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我修复了它,但我不确定如何修复。
我创建了一个空白项目并按照此处的教程进行操作:
http://www.raywenderlich.com/913 /sqlite-101-for-iphone-developers-making-our-app
并确保这有效。 (确实如此。)
然后我使用了这里的位:
http://www.raywenderlich.com /725/如何读取和写入-xml-documents-with-gdataxml
将数据库复制到可写的位置。我以前有过非常相似的代码。
所以现在可以了。
我专门使用“sqlite3”扩展构建了一个新表。我不知道这是否真的有所作为。
我还使用了:
与: 相对
,同样,不知道这是否重要。
希望这可以帮助其他人!
Well, I fixed it, but I'm not certain on how.
I created a blank project and followed the tutorial here:
http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app
And made sure that worked. (It did.)
I then used the bit from here:
http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml
To copy the database into a location where it's writeable. I had very similar code before.
So now it works.
I built a new table specifically with the "sqlite3" extensions. I don't know if this actually makes a difference or not.
I also used:
As opposed to:
Again, no idea if this matters or not.
Hopefully this helps someone else out!