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