在iphone中如何获取Sqlite最后插入的行id?
我正在制作一个应用程序,我想在其中使用更新查询?
在我的申请中,我有两个阶段来完成第 1 页中的注册过程
,我有注册表单和一个提交按钮?
单击提交按钮后,所有详细信息都应插入到我的 sqlite 表中。
在第2页中我有确认页面和一个编辑并继续按钮?
用户应在此页面上查看表中保存的所有值,如果他想要进行任何更改,他应该能够做到这一点。一旦用户编辑了一些值并按下继续按钮,所有插入的值都应该更新吗?
但我尝试这样做,但我无法更新最后输入的值?
以下是我的代码页1插入代码:
-(void)submit
{
if( ([UserName.text isEqualToString:@""]) || ([Password.text isEqualToString:@""]) ||
([ConfirmPassword.text isEqualToString:@""]) || ([Name.text isEqualToString:@""]) ||
([Email.text isEqualToString:@""]) || ([ContactNO.text isEqualToString:@""]) ||
([MobileNo.text isEqualToString:@""]) || ([Address.text isEqualToString:@""]) )
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!"
message:@"Please fill in the details." delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
else
{
Confirmation_form *conForm = [[Confirmation_form alloc] initWithNibName:@"Confirmation_form" bundle:nil];
conForm.data = UserName.text;
conForm.data1 = Password.text;
conForm.data2 = ConfirmPassword.text;
conForm.data3 = Name.text;
conForm.data4 = Email.text;
conForm.data5 = ContactNO.text;
conForm.data6 = MobileNo.text;
conForm.data7 = Address.text;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO test(UserName, Password, ConfirmPassword, Name, Email, ContactNO, MobileNo, Address) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",UserName.text, Password.text, ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
//status.text = @"Contact added";
UserName.text = @"";
Password.text = @"";
ConfirmPassword.text = @"";
Name.text = @"";
Email.text = @"";
ContactNO.text = @"";
MobileNo.text = @"";
Address.text = @"";
sqlite3_last_insert_rowid;
rowID = sqlite3_last_insert_rowid(test1DB);
NSLog(@"last inserted rowId = %d",rowID);
sqlite3_reset(statement);
sqlite3_finalize(statement);
sqlite3_close(test1DB);
[self.navigationController pushViewController:conForm animated:YES];
}
}
}
}
从上面的代码我能够获取行ID,但我无法在更新查询中使用该rowid
以下是我的代码页2更新代码:
-(IBAction)Update;
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"UPDATE test SET UserName='%@',Password='%@',ConfirmPassword='%@',Name='%@',Email='%@',ContactNO='%@',MobileNO='%@',Address='%@'WHERE ID='%@'",UserName.text,Password.text,ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text, sqlite3_last_insert_rowid(test1DB)];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
sqlite3_finalize(statement);
sqlite3_close(test1DB);
}
else {
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record notadded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
}
}
I am making an application where i want to use update query?
In my application i have two stages to complete the registration process
in page1 i have registration form and one submit button?
on click of submit button all the details should get insert into my sqlite table.
in page2 i have confirmation page and one edit and continue button?
all the value saved in a table should be view by the user on this page and if he want's to make any change he should be able to do that. once user had edited some value and he press continue button all the value insert should get updated?
but i have try doing this but i am not able to update the last enter value?
following is my code page1 insert code:
-(void)submit
{
if( ([UserName.text isEqualToString:@""]) || ([Password.text isEqualToString:@""]) ||
([ConfirmPassword.text isEqualToString:@""]) || ([Name.text isEqualToString:@""]) ||
([Email.text isEqualToString:@""]) || ([ContactNO.text isEqualToString:@""]) ||
([MobileNo.text isEqualToString:@""]) || ([Address.text isEqualToString:@""]) )
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!"
message:@"Please fill in the details." delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
else
{
Confirmation_form *conForm = [[Confirmation_form alloc] initWithNibName:@"Confirmation_form" bundle:nil];
conForm.data = UserName.text;
conForm.data1 = Password.text;
conForm.data2 = ConfirmPassword.text;
conForm.data3 = Name.text;
conForm.data4 = Email.text;
conForm.data5 = ContactNO.text;
conForm.data6 = MobileNo.text;
conForm.data7 = Address.text;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO test(UserName, Password, ConfirmPassword, Name, Email, ContactNO, MobileNo, Address) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",UserName.text, Password.text, ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
//status.text = @"Contact added";
UserName.text = @"";
Password.text = @"";
ConfirmPassword.text = @"";
Name.text = @"";
Email.text = @"";
ContactNO.text = @"";
MobileNo.text = @"";
Address.text = @"";
sqlite3_last_insert_rowid;
rowID = sqlite3_last_insert_rowid(test1DB);
NSLog(@"last inserted rowId = %d",rowID);
sqlite3_reset(statement);
sqlite3_finalize(statement);
sqlite3_close(test1DB);
[self.navigationController pushViewController:conForm animated:YES];
}
}
}
}
from the above code i am able to get the row id but i am not able to use that rowid in update query
following is my code page2 Update code:
-(IBAction)Update;
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"UPDATE test SET UserName='%@',Password='%@',ConfirmPassword='%@',Name='%@',Email='%@',ContactNO='%@',MobileNO='%@',Address='%@'WHERE ID='%@'",UserName.text,Password.text,ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text, sqlite3_last_insert_rowid(test1DB)];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
sqlite3_finalize(statement);
sqlite3_close(test1DB);
}
else {
sqlite3_step(statement);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record notadded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
http://www.sqlite.org/c3ref/last_insert_rowid.html
编辑:
在调用插入函数后调用该函数,它会返回 id。
另外:
您正在准备语句(编译 SQL),但没有保存它。保存对该语句的引用,并在再次使用它之前对其调用重置。它保存了编译。
编辑:
sqlite3_prepare_v2 返回对语句的引用 (&)。将其保存到成员变量中,并且不要再次调用准备。由于您正在逐步执行结果,因此如果您重新使用该语句,则需要在该语句上调用 sqlite3_reset(),然后调用 step ,它将再次执行它,而无需重新编译(解析)sql 语句。这是一个优化。
您没有检查 sqlite 调用的返回代码。始终检查并至少记录日志 - 但确实应该发生。否则,您会默默地将问题和错误传递给以后更难发现的地方。
编辑:
每个 sqlite 调用都可能失败或出现问题。请参阅下面我引用的关闭函数。几乎每个函数都会返回一个返回码。检查它们、记录它们等等……
最后,您需要变得更加稳健。请参阅此 SO 帖子中的关闭函数:
iOS 内存问题中的 SQLite
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
http://www.sqlite.org/c3ref/last_insert_rowid.html
EDIT:
call that function after you call the insert function, it will return you the id.
Also:
You're preparing your statement (which compiles the sql) but you're not saving it off. save a ref to that statement and call reset on it before using it again. It saves the compile.
EDIT:
sqlite3_prepare_v2 returns a reference (&) to a statement. Save that off to a member variable and don't call prepare again. Since you're stepping through the results, if you re-use the statement, you need to call sqlite3_reset() on the statement, then call step and it will execute it again without re-compiling (parsing) the sql statement. This is an optimization.
You're not checking return codes from you're sqlite calls. Always check and at a minimum log - but should really happen. Otherwise you're silently moving pass a problem and error later where it's harder to discover.
EDIT:
each sqlite call can fail or have issues. See the close function I reference below. almost every function gives you back a return code. Check them, log them etc...
Finally, you're close needs to be more robust. See the close function in this SO post:
Sqlite in iOS memory issues
您需要设置
rowid
而不是ID
。其余的代码很好You need to set
rowid
instead ofID
. the rest of code is fine