插入sqlite语句成功但没有数据添加到数据库
insert sqlite statement successful but no data added to database
我有一个使用 sqlite 数据库的应用程序。我成功地从数据库中检索了所有数据,但是当我尝试将数据插入数据库时,代码是正确的并且调试没有问题。但是,当我检查数据库时,没有添加新行。即使我使用 (sqlite3_last_insert_rowid) 方法,我也总是得到 0!
我已经尝试过这里提供的许多解决方案来解决与我类似的问题,但没有成功。大多数解决方案都提到数据库目录路径应该是可写的,我使用了他们提供的代码片段,但问题仍然存在。
这是我插入数据库的代码:
-(void)openConnection
{
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [paths objectAtIndex:0];
dbPath = [documentsDir stringByAppendingPathComponent:@"databaseFile.sqlite"];
NSLog(@"%@",dbPath);
fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (success)
NSLog(@"we have the database");
else
{
NSLog(@"we have no database");
defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TLCWorkshopsApp.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved)
NSLog(@"database copied");
}
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"OPENED DB");
void *v = NULL;
char *errmsg;
const char *sq = "PRAGMA foreign_keys = NO";
if (sqlite3_exec(database, sq , 0, v, &errmsg)!= SQLITE_OK) {
NSLog(@"faild to set the foreign_keys pargam");
}
}
else
NSLog(@"error in database");
}
-(void)addWorksopWithTitle:(NSString *)title presenters:(NSMutableArray *)presenters date:(NSString *)date time:(NSString *)time bld:(NSString *)bld floor:(int)floor room:(NSString *)room manadatory:(BOOL)manadatory
{
[self openConnection];
const char *encodeSqlStatement = "insert into Workshop (wid,title,manadatory,date,time,bld,floor,room) values(null,?,?,?,?,?,?,?)";
//end of edited code
sqlite3_stmt *compileStatement1;
int success;
if (sqlite3_prepare_v2(database, encodeSqlStatement, -1, &compileStatement1, NULL) == SQLITE_OK)
{
NSLog(@"insert WS prepair");
sqlite3_bind_text(compileStatement1, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 2, manadatory);
sqlite3_bind_text(compileStatement1, 3, [date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 4, [time UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 5, [bld UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 6, floor);
sqlite3_bind_text(compileStatement1, 7, [room UTF8String], -1, SQLITE_TRANSIENT);
success = sqlite3_step(compileStatement1);
}else
{
NSLog(@"insert WS NOT success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'. ", sqlite3_errmsg(database));
}
sqlite3_finalize(compileStatement1);
if (success == SQLITE_ERROR)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
[self closeConnection];
}
经过几天的工作和搜索,我终于找到了问题的答案并想与您分享。
我没有注意从 sqlite3_step
语句收到的消息。我收到 (19),这意味着 SQLITE_CONSTRAINT
,这意味着 约束问题 在我试图插入数据的 table 中。 问题是我在 table 中的主键是两个外键的组合。出于某种原因,我需要一个独立的主键。所以,我再次创建了 table 并添加了一个主键列。
这就是全部:)
我有一个使用 sqlite 数据库的应用程序。我成功地从数据库中检索了所有数据,但是当我尝试将数据插入数据库时,代码是正确的并且调试没有问题。但是,当我检查数据库时,没有添加新行。即使我使用 (sqlite3_last_insert_rowid) 方法,我也总是得到 0! 我已经尝试过这里提供的许多解决方案来解决与我类似的问题,但没有成功。大多数解决方案都提到数据库目录路径应该是可写的,我使用了他们提供的代码片段,但问题仍然存在。
这是我插入数据库的代码:
-(void)openConnection
{
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [paths objectAtIndex:0];
dbPath = [documentsDir stringByAppendingPathComponent:@"databaseFile.sqlite"];
NSLog(@"%@",dbPath);
fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (success)
NSLog(@"we have the database");
else
{
NSLog(@"we have no database");
defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TLCWorkshopsApp.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved)
NSLog(@"database copied");
}
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"OPENED DB");
void *v = NULL;
char *errmsg;
const char *sq = "PRAGMA foreign_keys = NO";
if (sqlite3_exec(database, sq , 0, v, &errmsg)!= SQLITE_OK) {
NSLog(@"faild to set the foreign_keys pargam");
}
}
else
NSLog(@"error in database");
}
-(void)addWorksopWithTitle:(NSString *)title presenters:(NSMutableArray *)presenters date:(NSString *)date time:(NSString *)time bld:(NSString *)bld floor:(int)floor room:(NSString *)room manadatory:(BOOL)manadatory
{
[self openConnection];
const char *encodeSqlStatement = "insert into Workshop (wid,title,manadatory,date,time,bld,floor,room) values(null,?,?,?,?,?,?,?)";
//end of edited code
sqlite3_stmt *compileStatement1;
int success;
if (sqlite3_prepare_v2(database, encodeSqlStatement, -1, &compileStatement1, NULL) == SQLITE_OK)
{
NSLog(@"insert WS prepair");
sqlite3_bind_text(compileStatement1, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 2, manadatory);
sqlite3_bind_text(compileStatement1, 3, [date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 4, [time UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 5, [bld UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 6, floor);
sqlite3_bind_text(compileStatement1, 7, [room UTF8String], -1, SQLITE_TRANSIENT);
success = sqlite3_step(compileStatement1);
}else
{
NSLog(@"insert WS NOT success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'. ", sqlite3_errmsg(database));
}
sqlite3_finalize(compileStatement1);
if (success == SQLITE_ERROR)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
[self closeConnection];
}
经过几天的工作和搜索,我终于找到了问题的答案并想与您分享。
我没有注意从 sqlite3_step
语句收到的消息。我收到 (19),这意味着 SQLITE_CONSTRAINT
,这意味着 约束问题 在我试图插入数据的 table 中。 问题是我在 table 中的主键是两个外键的组合。出于某种原因,我需要一个独立的主键。所以,我再次创建了 table 并添加了一个主键列。
这就是全部:)