Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Opening the way to the introduction of mysql programming with examples

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The following content mainly brings you from the examples to open the introduction to mysql programming, the knowledge mentioned is slightly different from books, are summed up by professional and technical personnel in the process of contact with users, and have a certain value of experience sharing, hoping to bring help to the majority of readers.

Let's start with a few concepts:

MYSQL is automatically submitted by default, that is, if you submit a QUERY, it will be executed directly! We can pass.

Set autocommit=0 forbids automatic submission

Set autocommit=1 enables auto-submission

Transaction processing is supported by INNODB engine in mysql, which is auto-committed by default.

Another commonly used MYISAM engine does not support transactions, and there is no concept of transactions.

From the perspective of the code:

# include

# include

# include

# include

# include

# include

# include

/ / transaction of oracle: transaction initiation flag DML language (oracle default transaction seems to be open)

/ / MYSQL is auto-submitted by default (every sql language executed will be automatically submitted to you)

/ *

What's the difference between MySQL set autocommit = 0 or 1? which one is automatic submission?

To be proficient in using the mysql help documentation, look for autocommit in it, and you will have corresponding comments, which are much more profound than what I'm going to tell you directly here.

Autocoomit is a transaction and commits immediately if the document of mysql equals 1. In transction, however, only commit or rollback is encountered before committing. I hope it works for you.

, /

# define BEGIN_TRAN "START TRANSACTION"

# define SET_TRAN "SET AUTOCOMMIT=0"

# define UNSET_TRAN "SET AUTOCOMMIT=1"

# define COMMIT_TRAN "COMMIT"

# define ROLLBACK_TRAN "ROLLBACK"

Int mysql_BeginTran (MYSQL * mysql)

{

Int ret = 0

/ /-- execute transaction start SQLret = mysql_query (mysql, BEGIN_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} / /-set transaction manual commit ret = mysql_query (mysql, SET_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} return ret

}

Int mysql_Rollback (MYSQL * mysql)

{

Int ret = 0

/ /-- transaction rollback operation ret = mysql_query (mysql, ROLLBACK_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} / /-resume transaction auto commit flag ret = mysql_query (mysql, UNSET_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} return ret

}

Int mysql_Commit (MYSQL * mysql)

{

Int ret = 0

/ /-- execute transaction commit SQLret = mysql_query (mysql, COMMIT_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} /-restore autocommit settings ret = mysql_query (mysql, UNSET_TRAN); if (ret! = 0) {printf ("func mysql_query () err:% d\ n", ret); return ret;} return ret

}

/ / drop table test_table

/ / create table create table test_table (col1 int, col2 varchar (10), col3 varchar (10))

# define sql01 "INSERT INTO test_table (col1,col2,col3) VALUES (10,'10,'1')"

# define sql02 "INSERT INTO test_table (col1,col2,col3) VALUES (20,'20,'2')"

# define sql03 "INSERT INTO test_table (col1,col2,col3) VALUES (30,'30,'3')"

# define sql04 "INSERT INTO test_table (col1,col2,col3) VALUES (40,'40,'4')"

Int main ()

{

Int ret = NULL

MYSQL * mysql;MYSQL_RES * res;MYSQL_ROW row;char * query;mysql = mysql_init (NULL); mysql = mysql_real_connect (mysql, "localhost", "root", "123456", "mydb2", 0, NULL, 0); if (mysql = = NULL) {ret = mysql_errno (mysql); printf ("func mysql_real_connect () err\ n"); return ret } else {printf ("ok.\ n");} ret = mysql_BeginTran (mysql); / / modify the properties of the transaction so that if (ret! = 0) {printf ("mysql_BeginTran () err:%d\ n", ret); return ret;} ret = mysql_query (mysql, sql01); if (ret! = 0) {printf ("mysql_query () err:%d\ n", ret); return ret } ret = mysql_query (mysql, sql02); if (ret! = 0) {printf ("mysql_query () err:%d\ n", ret); return ret;} ret = mysql_Commit (mysql); / / commit the transaction and restore the default properties of the transaction if (ret! = 0) {printf ("mysql_Commit () err:%d\ n", ret); return ret;} ret = mysql_BeginTran (mysql) If (ret! = 0) {printf ("mysql_BeginTran () err:%d\ n", ret); return ret;} ret = mysql_query (mysql, sql03); if (ret! = 0) {printf ("mysql_query () err:%d\ n", ret); return ret;} ret = mysql_query (mysql, sql04); if (ret! = 0) {printf ("mysql_query () err:%d\ n", ret); return ret } ret = mysql_Rollback (mysql); if (ret! = 0) {printf ("mysql_Rollback () err:%d\ n", ret); return ret;} mysql_close (mysql)

}

As a result, it is obvious that sql1 sql2 commit is effective.

Invalid sql3 sql4 rollback rollback

For the above examples to open the way to the introduction of mysql programming, if you need to know more, you can continue to pay attention to the innovation of our industry, if you need to get professional solutions, you can contact the pre-sales and after-sales on the official website. I hope this article can bring you some knowledge updates.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report