MySQL in Python

Source: Internet
Author: User

##################### #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.