MySQL operation two: Fetchone and get Lastrowid

Source: Internet
Author: User

Import MySQLdb

conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 123456 ')
cur = conn.cursor ()

recount = Cur.execute (' select * from admin ') # # #查询

data = Cur.fetchone () #获得数据
Print (data)
###

data = Cur.fetchone ()
Print (data)

Cur.scroll ( -1,mode= ' relative ') # #获取上一次取得的数据 (relative positioning)
data = Cur.fetchone ()
Print (data)
###
Cur.scroll (0,mode= ' absolute ') # #获取本代码里第一次取得的数据 (absolute positioning)
data = Cur.fetchone ()
Print (data)

sql = "INSERT into media (address) values (%s)"
params = ('/data/media/a.txt ',)
recount = Cur.execute (sql.params)
Conn.commit () # #插入数据并提交.
Print (Cur.lastrowid) # #打印出最后一个Id (self-increment ID)
new_id = Cur.lastrowid # # new_id can be used for the association of tables, if the above is an upload file, after obtaining an ID associated with the title table.

Cur.close ()
Conn.close ()

MySQL operation two: Fetchone and get Lastrowid

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.