0

私はアプリケーションを持っており、テーブルを作成し、開始直後にいくつかの値を挿入したいと考えています。

データベーステーブルを作成する私のコード:

- (void) crearTablaConf {
sqlite3 *turutaDB;
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *databasePath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"turuta.db"];

NSLog(@"path de la bbdd: %@",  databasePath);
if(sqlite3_open([databasePath UTF8String], &turutaDB)==SQLITE_OK) { 
    NSLog(@"2 Base de datos creada y abierta con exito");
} else {
    NSLog(@"Ha fallado la apertura de la bbdd");
}


sqlite3_stmt *sentenciaTablaConf; 

NSString *queryTablaConf = @"CREATE TABLE IF NOT EXISTS configuraciones (id int(11) NOT NULL, idioma int(1),fecha date,PRIMARY KEY (id));";

if(sqlite3_prepare_v2(turutaDB, [queryTablaConf UTF8String], -1, &sentenciaTablaConf, NULL)==SQLITE_OK){
    NSLog(@"Consulta preparada ok"); //NO ERROR HERE
} else {
    NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB));
}

sqlite3_finalize(sentenciaTablaConf);

sqlite3_stmt *sentenciaIniciar;
NSString *queryIniciar = @"insert into configuraciones (id, idioma, fecha) values ('1','0',NULL);";

if(sqlite3_prepare_v2(turutaDB, [queryIniciar UTF8String], -1, &sentenciaIniciar, NULL)==SQLITE_OK){
    NSLog(@"Consulta preparada ok");
} else {
    NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB));
}     //HERE, SAYS "NO SUCH TABLE: CONFIGURACIONES"    

sqlite3_finalize(sentenciaIniciar);
}    

自動コミットをチェックしてオンにしたので、エラーの場所がわかりません。

4

4 に答える 4

2

このコードを試してください

   NSString *docsDir;
   NSArray *dirPaths;
   sqlite3 *contactDB;
   dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

   docsDir = [dirPaths objectAtIndex:0];

   databasePath =[docsDir stringByAppendingPathComponent:@"yourdatabaseName.sqlite"];

   NSFileManager *filemgr = [NSFileManager defaultManager];

   if ([filemgr fileExistsAtPath: databasePath ] == NO)
   {
       const char *dbpath = [databasePath UTF8String];

       if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
       {
           char *errMsg;
           const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, LOCATION TEXT, CONDITION TEXT,HUMIDITY TEXT, WINDCONDI TEXT,PRESSURE TEXT, DEW TEXT, TEMP TEXT,LASTUP TEXT)";

           if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
           {
               NSLog(@"Failed to create table");
           }
           else{
               //Do anything here
           }

           sqlite3_close(contactDB);

       } else {
           NSLog(@"Failed to open/create database");
       }
   }
    else
    {
       // Do anything here
    }
于 2013-06-27T10:14:19.073 に答える
0
NSString *queryTablaConf = @"CREATE TABLE IF NOT EXISTS configuraciones (id int(11) NOT NULL, idioma int(1),fecha date,PRIMARY KEY (id));";

この行の後のコードで、テーブルを正常に作成して一度チェックするために、このブロックを追加しました。

if (sqlite3_exec(turutaDB, queryTablaConf, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"Failed to create table");
            }
于 2013-06-27T09:57:13.760 に答える