centos写mysql光标移到上一行_python操作mysql——使用pymysql库
生活随笔
收集整理的這篇文章主要介紹了
centos写mysql光标移到上一行_python操作mysql——使用pymysql库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
pymysql是python3連接mysql操作的庫,在python2中使用的是mysqldb
1.基礎(chǔ)操作
創(chuàng)建連接
import pymysqldb_conn = pymysql.connect(host="你的數(shù)據(jù)庫地址",user="用戶名",port=3306,password="密碼",database="數(shù)據(jù)庫名",charset="utf8")# 得到一個可以執(zhí)行SQL語句的光標對象 cursor = conn.cursor() # 執(zhí)行完畢返回的結(jié)果集默認以元組顯示 # 得到一個可以執(zhí)行SQL語句并且將結(jié)果作為字典返回的游標 # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 定義要執(zhí)行的sql語句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; #注意:charset='utf8' 不能寫成utf-8 """# 執(zhí)行 res=cursor.execute(sql) sql = 'select * from user1 where name = %s and age=%d' name = "zhangchen" age = 28 # execute函數(shù)可以加格式化字符參數(shù) res = cursor.execute(sql, [name, age]) print(res)# 執(zhí)行結(jié)束后要關(guān)閉光標和連接 cursor.close() db_conn.close()2.增刪改查
插入多條數(shù)據(jù)
import pymysqlconn = pymysql.connect(host='192.168.0.103',port=3306,user='root',password='123',database='xing',charset='utf8' ) # 獲取一個光標 cursor = conn.cursor()# 定義要執(zhí)行的sql語句 sql = 'insert into userinfo(name,age) values(%s,%d);' data = [('july', 14),('june', 25),('marin', 36) ] # 拼接并執(zhí)行sql語句 cursor.executemany(sql, data)# 涉及寫操作要注意提交 conn.commit()# 關(guān)閉連接 cursor.close() conn.close()插入單條數(shù)據(jù)
sql ='insert into userinfo (user,pwd) values (%s,%d);'name = 'zhangchen' pwd = 28 cursor.execute(sql, [name, pwd]) conn.commit() cursor.close() conn.close()獲取最后一行的數(shù)據(jù)(可以理解為最新插入的數(shù)據(jù))
# 定義將要執(zhí)行的SQL語句 sql = "insert into user1 (name, age) values (%s, %d);" name = "zhangchen" pwd = 28 # 并執(zhí)行SQL語句 cursor.execute(sql, [name, age]) # 涉及寫操作注意要提交 conn.commit()# 獲取最新的那一條數(shù)據(jù)的ID last_id = cursor.lastrowid print("最后一條數(shù)據(jù)的ID是:", last_id)# 關(guān)閉連接 cursor.close() conn.close()刪除操作
# 定義將要執(zhí)行的SQL語句 sql = "delete from user1 where name=%s;" name = "june" # 拼接并執(zhí)行SQL語句 cursor.execute(sql, [name]) # 涉及寫操作注意要提交 conn.commit() # 關(guān)閉連接cursor.close() conn.close()更改數(shù)據(jù)
sql = "update user1 set age=%d where name=%s;" # 拼接并執(zhí)行SQL語句 cursor.execute(sql, [18, "zhangchen"])# 涉及寫操作注意要提交 conn.commit()# 關(guān)閉連接 cursor.close () conn.close ()查詢數(shù)據(jù)
# 獲取一個光標 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回字典數(shù)據(jù)類型# 定義將要執(zhí)行的sql語句 sql = 'select name,age from user1;' # 執(zhí)行sql語句 cursor.execute(sql)# 取到查詢結(jié)果 ret1 = cursor.fetchone() # 取一條 ret2 = cursor.fetchmany(3) # 取三條 ret3 = cursor.fetchone() # 取一條# 光標按絕對位置移動1 cursor.scroll(1, mode="absolute") # 光標按照相對位置(當前位置)移動1 cursor.scroll(1, mode="relative")數(shù)據(jù)回滾
try:# 拼接并執(zhí)行SQL語句cursor.execute(sql1, [name, age])print(sql1)cursor.execute(sql2, [id, hobby]) # 報錯的SQL語句# 涉及寫操作注意要提交conn.commit() except Exception as e:print(str(e))# 有異常就回滾conn.rollback()# 關(guān)閉連接 cursor.close() conn.close()總結(jié)
以上是生活随笔為你收集整理的centos写mysql光标移到上一行_python操作mysql——使用pymysql库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dotnetfx.exe是什么进程
- 下一篇: mysql innodb id_MySQ