【问题标题】:Insert Values In SQLITE Database在 SQLITE 数据库中插入值
【发布时间】:2012-09-09 06:55:54
【问题描述】:

我尝试在 sqlite3 数据库中插入值,但它无法在我的应用程序上运行。 我遵循这段代码,其中表中的所有这些值都是 varchar。

sqlite3_stmt *statement;

NSString *docsDir;
NSArray *dirPaths;
int ID;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"weather.sqlite"]];


const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO satish VALUES ('jodhpur','hazy1','68','7','700','30','28','10sept')"];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
    {
        NSLog(@"%@",statement);
        sqlite3_bind_text(statement, 1, insert_stmt, -1, SQLITE_TRANSIENT );

        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            ID = sqlite3_last_insert_rowid(contactDB);

            NSLog(@"Data inserted successfully ");

        } 
        else{             
            NSLog(@"Failed to add contact");
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
}

【问题讨论】:

    标签: iphone ios database ios5 sqlite


    【解决方案1】:
    //tyr this code    
    // you must give the column name to insert values in DB   
    
         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
                NSString *documentsDir = [paths objectAtIndex:0];
    
                NSFileManager *fileManager = [NSFileManager defaultManager];
                NSError *error;
                NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"register.sqlite"];
                BOOL success = [fileManager fileExistsAtPath:dbPath]; 
                sqlite3_stmt *selectstmt;
                if(!success)
                {
                    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"register.sqlite"];
                    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    
                    if (!success) 
                        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
                }
    
                if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
                    //*************** insert value in database******************************\\ 
    
                    const char *sql = "insert into satish (firstname,lastname,email,company,phone) VALUES ('sdcbb','bbbb','bbbb','bbbb',1111122)";
                    sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL); 
                    if(sqlite3_step(selectstmt)==SQLITE_DONE)
                    {
                        NSLog(@"insert successfully");
                    }
                    else
                    {
                        NSLog(@"insert not successfully");
    
                    }
                sqlite3_finalize(selectstmt);
                    sqlite3_close(database);
                }
    

    【讨论】:

    • 对不起,我没有更改数据库名称,register.sqlite 是我的数据库名称
    • 你将 register.sqlite 更改为 weather.sqlite
    • 并更改“插入 satish(您的列名)VALUES(您的值)”;
    • 在你的代码中,它在数据库附近给出了一些错误,说用于未声明的标识符'datadase'
    • 在“@implementation 类名”之后添加“static sqlite3 *database = nil;”
    【解决方案2】:

    我看不到您将数据库从捆绑包复制到文档目录的位置。如果您不复制预制的 SQLite 数据库,那么您的公开调用将创建一个新的空数据库。由于您尚未在空数据库中创建表,因此 Insert 调用将失败。解决方案 1:编写文件例程以将带有名为 satish 的表的预制 SQLite 数据库复制到您的文档目录中。解决方案2:如果不想预先制作sqlite数据库,请先在SQLite中使用create语句创建satish表。

    【讨论】:

      猜你喜欢
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2017-04-03
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多