在我的应用程序中,我在sqlite数据库上进行读取,写入和删除(以db格式),它在模拟器中工作正常,但在实际设备中无效(给出错误:No such table found
)。我非常努力寻找解决方案,但找不到或可能是我误解的东西。
我的数据库在
中/ users / My Name / library / developer / Core 模拟器/设备/器件N-ID /数据/应用/ APP-ID /文档/我的 数据库中。
- (NSString *) getWritableDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:mydaatabase];
}
我很困惑在实际的iOS设备上运行时我需要复制数据库的地方和核心问题。对此有任何帮助将非常有帮助
-(void)createEditableCopyOfDatabaseIfNeeded
{
BOOL success;
NSFileManager *fileManager1 = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:mydaatabase];
NSLog(@"=======%@", writableDBPath);
success = [fileManager1 fileExistsAtPath:writableDBPath];
if (success)
return;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:mydaatabase];
NSLog(@"=======%@", [NSBundle mainBundle]);
success = [fileManager1 copyItemAtPath:defaultDBPath
toPath:writableDBPath
error:&error];
if(!success)
{
NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
[error localizedDescription]);
}
}
答案 0 :(得分:0)
以下是使用SQLite数据库的步骤。
在SQLite中创建数据库,添加具有良好结构的必需表。并将其添加到您的应用程序中。
现在,转到您的应用,从左侧面板,中间面板中选择项目 - >一般 - >链接的框架和库(最后一个)添加libsqlite3.0.tbd
(这取决于您的Xcode版本。)
现在,在your-object.h
#import <sqlite3.h>
+ (NSString *)getDBPath;
+ (void)copyDatabaseIfNeeded;
+ (void)openDatase;
在your-object.m
static sqlite3 *database;
#pragma mark - Copy and open databse
+ (NSString *)getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stirngByAppendingPathComponent:@"dbName.sqlite"];
}
+ (void)copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbName.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
+ (void)openDatase
{
NSString *dbPath = [self getDBPath];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"Database Successfully Opened.");
}
else
{
NSLog(@"Error in opening database.");
}
}
然后在AppDelegate.m
application: didFinishLaunchingWithOptions:
中的方法
[DataManager copyDatabaseIfNeeded];
每当您想要使用数据库执行任务时,请打开它并检查。
用于插入数据库
+ (void)insertData
{
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *strSQL = [NSString stringWithFormat:@"INSERT INTO table name………………."];
const char *insertQuery = [strSQL UTF8String];
sqlite3_stmt *insert_stmt;
if (sqlite3_prepare_v2(database, insertQuery, -1, &insert_stmt, NULL) == SQLITE_OK)
{
if (sqlite3_step(insert_stmt) == SQLITE_DONE)
{
// Code
}
else
{
NSLog(@"error");
}
}
sqlite3_finalize(insert_stmt);
sqlite3_close(database);
}
}
7。完成....享受数据库。
答案 1 :(得分:0)
检查您的.sqlite
文件是否已添加到项目Copy Bundle Resources
中的Build Phases
。如果不存在,请将其添加到项目中。
此外,我无法找到打开数据库的代码。因此,在ViewController.h
文件中,声明Sqlite *database;
在ViewController.m
文件中,
-(void)databaseOpen
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *myDBnew=[documentsDirectory stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
database =[[Sqlite alloc]init];
[database open:myDBnew];
NSLog(@"path: %@", myDBnew);
NSLog(@"Database opened");
}
并在[self databaseOpen];
的任意位置调用它,然后编写查询。
另外,-(void)createEditableCopyOfDatabaseIfNeeded
中的AppDelegate.m
看起来应该是这样的:
- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourSqliteFile.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
在-(void)createEditableCopyOfDatabaseIfNeeded
didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self createEditableCopyOfDatabaseIfNeeded];
return YES;
}
这应该让你去。