mysql_rollback() not working
I had asked this question in another words in my last post and I didn't get the specific answer. So I rephrase. the rollback in the following code is not working. can you please tell me what is wrong:
//Creating and populating a table
C_P_table()
{
MYSQL *conn;
conn = mysql_init(NULL);
if (mysql_real_connect(conn, "localhost", "root",
"password", "testdb", 0, NULL, 0) == NULL) {
printf("Error2 %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))")) {
printf("Error3 %u: %s\n", mysql_errno(conn), mysql_error(conn));
}
mysql_autocommit(conn,0);
if (mysql_query(conn,"START TRANSACTION")) {
printf("Error4 %u: %s\n", mysql_errno(conn), mysql_error(conn));
}
mysql_query(conn,"START TRANSACTION");
mysql_query(conn, "INSERT INTO writers VALUES('Leo Tolstoy')");
mysq开发者_StackOverflowl_query(conn, "INSERT INTO writers VALUES('Jack London')");
mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");
mysql_query(conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')");
mysql_query(conn, "INSERT INTO writers VALUES('Emile Zola')");
if(!mysql_rollback(conn))
printf("Roll Back successful\n");
// mysql_query(conn,"ROLLBACK"); //doesn't work neither
mysql_close(conn);
}
int main(int argc, char **argv)
{
C_P_table();
}
My guess is that you are using MyISAM tables, normaly the default table type in MySQL.
So change the line
if (mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))"))
to
if (mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25)) ENGINE = INNODB"))
and see whats happening.
See: http://dev.mysql.com/doc/refman/5.0/en/storage-engines.html
精彩评论