时间:2021-07-01 10:21:17 帮助过:25人阅读
我建表时将id设为了 auto increment , 后来有我使用时发现id应该是从1开始的,但是结果却是从2开始递增,我是使用的C++,下面的是我部分代码:
sql_->SetAutoCommit(0); if(sql_->InsertUserLoginTable(id,"") == -1) { result = "regist faild1"; } else { std::string ID = sql_->GetLastID(); std::cout << "ID : " << ID << std::endl; if (sql_->InsertLoginInfoTable(ID, "", "", "", IMEI, BTMAC) == -1) { result = "regist faild2"; } else if (sql_->InsertUserInfoTable(ID, FaceID, NickName, "", "", "3") == -1) { result = "regist faild3"; } else if (sql_->InsertUserRecordTable(ID, RegistIP) == -1) { result = "regist faild4"; } else { result = "regist success"; } } if(result == "regist success") { sql_->Commit(0); } else { sql_->Commit(1); } sql_->SetAutoCommit(1);
其中有一些是我自己封装的方法,部分如下:
void SetAutoCommit(int i)
{
sql->AutoCommit(i);
}
int Commit(int i)
{
sql->CommitSQL(i);
}
void AutoCommit(int i)
{
mysql_autocommit(connection_,i);
}
int CommitSQL(int i)
{
if(i == 1)
{
mysql_rollback(connection_);
return -1;
}
else
{
mysql_commit(connection_);
return 0;
}
}