无法向sqlite DB iOS插入值

时间:2012-09-16 13:22:41

标签: ios sqlite

在本教程之后,我在向数据库插入值时遇到了一些问题,尽管一切似乎都正常。

这是我输入条目的代码。

-(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);
    }
}

我可以看到“已插入订单”正在打印出来。

这是我检索列表的代码。

-(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$

就像我的数据库甚至没有创建一样。

这是我创建数据库的代码。

-(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);
            }
        }
    }
}

0 个答案:

没有答案