如何使用NSMutableArray中的列名列表在sqlite3中创建表

use*_*184 3 sqlite iphone sdk objective-c ios

我需要动态创建一个带有列的sqlite3表,实际上是存储在NSMutableArry中的列名,我想从NSMutableArrya值创建一个表.

Anu*_*yal 5

试试这个 :

- (void)viewDidLoad
  {
    NSString *idField = @"ID INTEGER PRIMARY KEY AUTOINCREMENT";
    NSString *nameField = @"NAME TEXT";
    NSString *ageField = @"AGE INTEGER";

   // Make the field array using different attributes in different cases
   NSArray *fieldArray = [NSArray arrayWithObjects:idField,nameField,ageField, nil];

     [self createTable:fieldArray];

   }

- (void)createTable:(NSArray *)fieldArray
  {
    // Put all the code for create table and just change the query as given below
    NSString *queryString = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS CONTACTS (%@)",[fieldArray componentsJoinedByString:@","]];
const char *sql_stmt = [queryString UTF8String];
  }
Run Code Online (Sandbox Code Playgroud)