##################### #mysql ########################
1. Installing the Software
Yum Install Mariadb-server mysql-python-y
Systemctl Start mariadb
Systemctl Enable MARIADB
2. Create a database
Mysql_secure_installation # #修改mysql密码, password set to 123
Mysql-uroot-p123
Create DATABASE Westos
3. Open the Ipython and import the MYSQLDB
Import MySQLdb # #导入模块
4. Connect to the database
Conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' 123 ', db= ' Westos ')
Cur=conn.cursor () # #创建数据库游标
Cur.execute (' CREATE TABLE userinfo (username varchar (6), passwd varchar (6) # #创建表
5. Inserting data
Cur.execute (' INSERT into UserInfo value ("Zhang", "ABCD"); # #插入一个数据
sqli= ' INSERT into UserInfo value (%s,%s) '
Cur.executemany (sqli,[(' AA ', ' 123 '), (' BBB ', ' 123 ')] # #多条数据插入
6. Save your data
Commit () # #如果数据库表进行了修改, commit to save the current data.
7. Querying data
Cur.fetchone () # #逐个查询
Cur.fetchall () # #全部查询
Cur.scroll (0, ' absolute ') # #恢复游标
Cur.fetchmany (3) # #查询3个数据
8. Move the cursor
Cur.scroll (0, ' absolute ')
#cur. Scroll (Value,mode)
# mode= ' relative ', indicating that the value bar data is moved backwards from the current cursor
# mode= ' absolute ', indicating that the first row of the result set moves backward through the value bar data
9. Close the connection
Cur.close () # #关闭游标
Conn.close () # #关闭ipython与数据库的连接
MySQL in Python