0

このチュートリアルによく従うと、すべてが正常に機能しているように見えますが、DB に値を挿入する際に問題が発生します。

これは、エントリを挿入するための私のコードです。

-(void)insertOrder:(MyOrderList*)entry
{
    // Create insert statement for the person
    NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO LIST (URL, CODE, EMAIL, FULL) VALUES (\"%@\", \"%@\", \"%@\",\"%@\")", entry.db_url, entry.db_code, entry.db_email,entry.db_full];
    NSLog(@"SQL INSERTION COMMAND:%@",insertStatement);

    char *error;
    if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
    {
            NSLog(@"Order inserted.");
    }
    else
    {
        NSLog(@"Error: %s", error);
    }
}

そして、「Order Inserted」が印刷されているのがわかります。

これは、リストを取得するための私のコードです。

-(NSArray*) getList
{
    NSMutableArray *persons = [[NSMutableArray alloc]init];
    NSString *queryStatement = [NSString stringWithFormat:@"SELECT URL,CODE ,MAIL, FULL FROM LIST"];
    sqlite3_stmt *statement;
    NSLog(@"Query:%@",queryStatement);
    if (sqlite3_prepare_v2(databaseHandle, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
    {
        // Iterate over all returned rows
        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSLog(@"Url of my List is:%@",[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)]);

            MyOrderList *person = [[MyOrderList alloc]initWithUrl:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)] andCode:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)] andEmail:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)] andFull:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)]];
      [persons addObject:person];

            // Release the person because the array takes ownership
            //[person release];

        }
        sqlite3_finalize(statement);
    }
    //return [persons autorelease];
    return persons;

}

これが私の.mファイルでの使用方法です

  DataController *dataController = [[DataController alloc]init];
                [dataController initDatabase];
                //[dataController release];

                 MyOrderList *order  = [[MyOrderList alloc] initWithUrl:url_2 andCode:keycode andEmail:mail andFull:full_2 ];
                [dataController insertOrder:order];
                //data has been added
                NSMutableArray* persons = [dataController getList];

そして、人が0であることがわかります。

ターミナルから私はこれを見る:

Admins-MacBook-Air:~ admin$ sqlite3 Users/admin/Library/Application\ Support/iPhone\ Simulator/5.1/Applications/1A70F53B-133E-46AE-833E-13F205EA96EA/Documents/sqlite.db
SQLite version 3.7.12 2012-04-03 19:43:07
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
Error: unable to open database "Users/admin/Library/Application Support/iPhone Simulator/5.1/Applications/1A70F53B-133E-46AE-833E-13F205EA96EA/Documents/sqlite.db": unable to open database file
Admins-MacBook-Air:~ admin$

私のデータベースも作成されていないようです。

これは、DB を作成するための私のコードです。

-(void)initDatabase
{
    // Create a string containing the full path to the sqlite.db inside the documents folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"sqlite.db"];
    NSLog(@"Path is:%@",databasePath);

    // Check to see if the database file already exists
    bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

    // Open the database and store the handle as a data member
    if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
    {
        // Create the database if it doesn't yet exists in the file system
        if (!databaseAlreadyExists)
        {
            // Create the PERSON table
            const char *sqlStatement = "CREATE TABLE IF NOT EXISTS LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, CODE TEXT, EMAIL TEXT, FULL TEXT)";
            char *error;
            if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
            {
                NSLog(@"Database and tables created.");
            }
            else
            {
                NSLog(@"Error: %s", error);
            }
        }
    }
}
4

0 に答える 0