Creating an SQLite3 database file through Objective-C

后端 未结 6 1614
攒了一身酷
攒了一身酷 2020-12-01 15:19

I\'m trying to create an SQLite3 database file through Objective-C at run time. I am trying to create a table called \"tblStore\". I want the field names to be called \"st

相关标签:
6条回答
  • 2020-12-01 15:51

    You can use following code to get the database created at in Documents folder. Just pass a path in documents folder and the function will copy the sqlite database in Documents folder at the given path if required. You can then use this path to create and query database tables.

    + (NSString*) createDatabaseIfRequiredAtPath:(NSString*)databasePath {
    
        if (databasePath == nil)
           return nil;
    
    
       NSString *path = [NSString stringWithFormat:@"%@/%@", databasePath, kMainDBName];
       NSFileManager *fileManager = [NSFileManager defaultManager];
       NSError *error = nil;
    
       if ([fileManager fileExistsAtPath:path] == NO) 
       {
        // The writable database does not exist, so copy the default to the appropriate location.
          NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:kMainDBName
                                                                  ofType:nil];
          BOOL success = [fileManager copyItemAtPath:defaultDBPath 
                                           toPath:path
                                            error:&error];
          if (!success)
          {
            NSCAssert1(0, @"Failed to create writable database file with message '%@'.", [  error localizedDescription]);
            return nil;
        }
    }
    
    return path;
    
    0 讨论(0)
  • 2020-12-01 15:52

    Is there a specific reason you'd like to use SQLLite directly, as opposed to using CoreData? CoreData uses an SQLLite database but is itself a higher-level API, and particularly with table views and such, you get a lot of functionality and template methods that are already set up for it in Xcode. Defining data models is trivial, you get tons of boilerplate code, and it's all optimized.

    http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started

    CoreData is sometimes described as having a steep learning curve. I disagree. If you're considering writing SQL yourself in your app, you're not going to have any trouble with CoreData.

    The term "sandbox" is an abstract term for the portion of the device's filesystem that your application has read/write access to. The "Documents" directory is a specific directory within your application's sandbox. There are other files in your sandbox than just the documents directory, but most applications that save data to the filesystem in iOS do so in the documents directory.

    0 讨论(0)
  • 2020-12-01 15:56

    maybe you forgot Table Insertions. Created Database and Table but its empty table . you trying to read the records from Table which is empty.

    0 讨论(0)
  • 2020-12-01 15:58

    sqlite is a pain if you dont know what you're doing. I also had some problems with sqlite c functions but then i decided to use sqlite wrapper.

    FMDB and BWDB are good and easy to use sqlite wrappers for objective c. I suggest you use one of those .

    Note that BWDB is in a lynda.com tutorial (this one) and if you don't find it on the web...leave a comment and i'll upload it somewhere.

    edit: the only place you can write stuff in your app is in your documents directory...so..it plain terms...if the db is not in your documents directory..is read-only..also..when you read/write to your db..the OS copies the db in the documents directory..and does all the reading and writing there so you can have a db in your app bundle but you can't edit that one...so you'll end up with 2 db.I had the same problem myself..and i fixed it by merging the 2 db when i updated the app

    edit2: i uploaded BWDB final project ( you have your wrapper there and project to see how it works)

    0 讨论(0)
  • 2020-12-01 16:05

    // Create DB

    -(NSString *) filePath
    {
     NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentDirectory=[paths objectAtIndex:0];
     return [documentDirectory stringByAppendingPathComponent:@"LoginDatabase.sql"];
    }
    

    // Open DB

    -(void)openDB
    {
     if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK)
     {
       sqlite3_close(db);
       NSAssert(0, @"Database failed to Open");
     }
    }
    

    // Create Table

    -(void) createTableNamed:(NSString*)tableName withField1:(NSString*) field1 withField2:(NSString*) field2
    
    {
       char *err;
       NSString *sql=[NSString stringWithFormat:@" CREATE TABLE IF NOT EXISTS '%@'('%@' TEXT PRIMARY KEY,'%@' TEXT);",tableName,field1,field2];
    
       if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) !=SQLITE_OK)
       {
        sqlite3_close(db);
        NSAssert(0, @"Table failed to create");
       }
    }
    

    // Inserting records

    -(void)insertrecordIntoTable:(NSString*) tableName withField1:(NSString*) field1 field1Value:(NSString*)field1Vaue andField2:(NSString*)field2 field2Value:(NSString*)field2Value
    {
    
      NSString *sqlStr=[NSString stringWithFormat:@"INSERT INTO '%@'('%@','%@')VALUES(?,?)",tableName,field1,field2];
     const char *sql=[sqlStr UTF8String];
    
     sqlite3_stmt *statement1;
    
     if(sqlite3_prepare_v2(db, sql, -1, &statement1, nil)==SQLITE_OK)
     {
      sqlite3_bind_text(statement1, 1, [field1Vaue UTF8String], -1, nil);
      sqlite3_bind_text(statement1, 2, [field2Value UTF8String], -1, nil);
     }
     if(sqlite3_step(statement1) !=SQLITE_DONE)
        NSAssert(0, @"Error upadating table");
     sqlite3_finalize(statement1);
    }
    

    // Retrieve data from table

    -(void)getAllRowsFromTableNamed:(NSString *)tableName
    {
      NSString *field1Str,*field2Str;
    
      NSString *qsql=[NSString stringWithFormat:@"SELECT * FROM %@",tableName];
      sqlite3_stmt *statement;
      if(sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, nil)==SQLITE_OK)
      {
       while(sqlite3_step(statement) ==SQLITE_ROW)
       {
         char *field1=(char *) sqlite3_column_text(statement, 0);
         char *field2=(char *) sqlite3_column_text(statement, 1);
    
         field1Str=[[NSString alloc]initWithUTF8String:field1];
         field2Str=[[NSString alloc] initWithUTF8String:field2];
    
         NSString *str=[NSString stringWithFormat:@"%@ - %@",field1Str,field2Str];
         NSLog(@"%@",str);
       }
    }
    

    }

    In viewDidLoad call the methods

    - (void)viewDidLoad
    {
    
      [self openDB];
      [self createTableNamed:@"Login" withField1:@"USERNAME" withField2:@"PASSWORD"];
      [self insertrecordIntoTable:@"Login" withField1:@"USERNAME" field1Value:username andField2:@"PASSWORD" field2Value:password];
    
    }
    

    Where username and password are NSString values;

    0 讨论(0)
  • 2020-12-01 16:10

    you can open terminal and cd to /Users/Matt**/Library/Application Support/iPhone Simulator/5.1/Applications/5DB7A218-A0F6-485F-B366-91FD2F9BC062/Documents/

    then sqlite3 tblStore.sqlite

    then use .schema tblStore should show your table schema and you can see if it was built correctly.

    Your database needs to be in the documents directory for write access. If your database was only going to be read, never written to it could be in your application bundle. One way to accomplishing this is to create your database.sqlite file and add it to the bundle and copy it to the documents directory (if it doesn't already exist there) on launch.

    0 讨论(0)
提交回复
热议问题