没有这样的表:即使在初始化之后,sqlite 中的表名 [英] no such table: tablename in sqlite even after initialization

查看:22
本文介绍了没有这样的表:即使在初始化之后,sqlite 中的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ios 开发新手,遇到以下问题:我初始化了我需要的表,当我尝试对任何表执行查询时,我收到没有这样的表"错误,这是步骤和代码:

i am new to ios dev and i am having the following problem: i initialized the tables i needed, and when i try to execute a query to any of the tables i get the "no such table" error, here is the steps and code:

        -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Function called to create a copy of the database if needed.
                [[SQLiteUtilities sharedSQLiteManager] initializationDatabase];
                return YES;
            }

            and here is the implementation:

         -(void)initializationDatabase {
                NSString *path = [self databaseFilePath];
                BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:path];

                if (isExists) return;

                FMDatabase *db = [FMDatabase databaseWithPath:path];
                if ([db open] == NO) {
                    return;
                } 
                NSString *sql =
                @"CREATE TABLE albums("
                "albumid INTEGER PRIMARY KEY AUTOINCREMENT,"
                "directory CHAR(20) NOT NULL,"
                "albumname CHAR(32) NOT NULL,"
                "count INT NOT NULL,"
                "orderid INT NOT NULL"
                ");"

                "CREATE TABLE photos("
                "photoid INTEGER PRIMARY KEY AUTOINCREMENT,"
                "albumid INTEGER NOT NULL DEFAULT 0,"
                "filename CHAR(50) NOT NULL DEFAULT \"\","
                "originalname CHAR(50) DEFAULT \"\","
                "addtime INTEGER NOT NULL DEFAULT 0,"
                "createtime INTEGER NOT NULL DEFAULT 0,"
                "filesize INTEGER NOT NULL DEFAULT 0"
                ");";
                //[db executeQuery:sql1];
                [db executeQuery:sql];
                [db close];
            }

    and when i try to add anything as follows i get the "no such table:albums" error:

- (AlbumsUtilities *)createAlbumWithName:(NSString *)name {
    FMDatabase *db = [FMDatabase databaseWithPath:[self databaseFilePath]];
    if (![db open]) return nil;
    NSString *directory = createRandomAlbumDirectory();

    int maxid = 0;
    BOOL success = [db executeUpdate:@"INSERT INTO albums(directory,albumname,count,orderid) VALUES(?,?,0,?)", directory,name,@(maxid)];
    AlbumsUtilities *album = nil;
    if (success) {
       ....(not getting here of course)
    }
    [db close];
    return album;
}

任何帮助将不胜感激,谢谢.

any help would be appreciated, thanks.

推荐答案

试试这两个函数初始化 db

Try this two function for initialize db .

添加空白文件 .sqlite 扩展名

- (void)createCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSLog(@"%@",[self getDBPath]);

    success = [fileManager fileExistsAtPath:[self getDBPath]];
    if (success){
        return;
    }
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:[self getDBPath] error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}


-(NSString *)getDBPath{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"db.sqlite"];
    return  dbPath;
}

初始化后创建表

-(void)createTable{
    FMDatabase *database = [FMDatabase databaseWithPath:[self getDBPath]];
    [database open];
    [database executeUpdate:"your query"];
    [database close];
}

打电话

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Function called to create a copy of the database if needed.
            [[SQLiteUtilities sharedSQLiteManager] createCopyOfDatabaseIfNeeded];
       [[SQLiteUtilities sharedSQLiteManager] createTable];
}

这篇关于没有这样的表:即使在初始化之后,sqlite 中的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆