尝试创建sqlite数据库“无法创建表" IOS7时出错 [英] getting error while trying to create sqlite database 'Could not create table' IOS7

查看:156
本文介绍了尝试创建sqlite数据库“无法创建表" IOS7时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我试图创建一个本地数据库来存储具有主键自动增量值但其给定错误类似的值.

Hi in my Application I'm trying to create a local database to store values with primary key auto increment value but its giving error like.

2014-06-19 14:34:28.499 brt[2363:80b] *** Assertion failure in -[listviewpoliticalViewController createTable:withField1:withField2:withField3:withField4:], /Users/madhavadudipalli/Desktop/ ios projects/brt/brt/listviewpoliticalViewController.m:37

2014-06-19 14:34:28.501 brt[2363:80b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not create table'

我遇到上述行中的错误,请告诉我如何解决此问题.

I'm getting error like above lines please tell me how to resolve this issue.

我的数据库在.h文件中创建代码.

My database create code in .h file.

    -(NSString *) filePath;
    -(void)openDB;

    -(void) createTable: (NSString *) tableName
        withField1:(NSString *) field1
        withField2:(NSString *) field2
        withField3:(NSString *) field3
        withField4:(NSString *) field4;

.m文件中的数据库代码.

Database code in .m file.

     -(void) createTable: (NSString *) tableName
         withField1:(NSString *) field1
         withField2:(NSString *) field2
         withField3:(NSString *) field3
        withField4:(NSString *) field4;
   {
     char *err;
     NSString *sql =[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ( Id integer PRIMARY KEY,'%@' TEXT, '%@' TEXT, '%@' TEXT);",tableName,field1,field2,field3];
   if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
       sqlite3_close(db);
       NSAssert(0, @"Could not create table");
      }else{
       NSLog(@"Table Created");
      }

    }
    -(NSString *) filePath{
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"bp.sqlite"];
   }
   -(void)openDB{
    if (sqlite3_open([[self filePath] UTF8String], &db)!=SQLITE_OK) {
       sqlite3_close(db);
       NSAssert(0, @"Database failed to open");

    }else{
      NSLog(@"database opened");
    }
  }

我已使用上面的代码在sqlite中创建数据库,请告诉我上面的代码在哪里做错了如何解决此问题,我在这里遇到了很长时间,请帮助我.

I have used the above code to create the database in sqlite please tell me where I'm doing wrong in the above code how to resolve this issue I have been struck here for long time please help me out.

谢谢.

推荐答案

在格式化SQL语句时遇到一些错误.您不应该使用'%@ '.只需使用%@

You have some errors in formatting the SQL statement. You shouldn't use '%@'. just use %@

这是一个工作示例:

@interface SQLiteManager ()

@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic) sqlite3 *myDataBase;

@end

@implementation SQLiteManager

- (void)createDatabase
{
    // get the path to our data base
    NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *doctumentsDirectory = [directories lastObject];
    self.databasePath = [[NSString alloc] initWithString:[doctumentsDirectory stringByAppendingPathComponent:@"/yourDataBaseName.db"]];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // create DB if it does not already exists
    if (![fileManager fileExistsAtPath:self.databasePath]) {

        const char *dbPath = [self.databasePath UTF8String];

        if (sqlite3_open(dbPath, &_myDataBase) == SQLITE_OK) {

            char *errorMsg;
            const char *sql_statement = "CREATE TABLE IF NOT EXISTS SOME_TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT)";

            if (sqlite3_exec(_myDataBase, sql_statement, NULL, NULL, &errorMsg) != SQLITE_OK) {

                [self errorCreatingTable:[NSString stringWithFormat:@"failed creating table. ERROR:%s", errorMsg]];
            }

            sqlite3_close(_myDataBase);

        } else {

            [self errorCreatingTable:@"failed openning / creating table"];
        }
    }
}

- (void)errorCreatingTable:(NSString *)errorMsg
{
    NSLog(@"%@", errorMsg);
}

这篇关于尝试创建sqlite数据库“无法创建表" IOS7时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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