Development Database Program Flow:
1. Create a Connection object to get the cursor
2. Execute SQL using the cursor
3. Use the cursor to get the data, judge the execution status
4. Committing a transaction or rolling back a transaction
Import
Database Connection object: Connection
Database Interaction object: Cursor
- #sql查询语句
- Sql=' select * from user '
- #sql语句执行
- Cursor.execute (SQL)
- #获取返回数据总条数
- Rs=cursor.rowcount
- Print RS
- #获取返回的第一条数据, when the cursor rownumber=0, gets =1
- Rs=cursor.fetchone ()
- Print RS
- #获取返回数据的接下来3条数据, when the cursor rownumber=1, gets =4
- Rs=cursor.fetchmany (3)
- Print RS
- #获取剩下的所有返回数据, at which time the cursor rownumber=4, gets after = Last bar
- Rs=cursor.fetchall ()
- Print RS
- #循环获取, process each piece of data
- For row in RS:
- print ' userid=%s username=%s '% row
- Cursor.close ()
- Conn.close ()
Python connection operation MySQL Database