python連接mysql數據庫python
pyMsql是在py3中鏈接mysql服務器的一個庫,可是在py2中則使用mysqldbmysql
在django中也能夠使用pymysql來鏈接mysql數據庫sql
pymysql安裝數據庫
pip install pymysql(打開cmd輸入這句話便可)django
注意服務器
要保證有一個mysql數據庫,而且已經啓動ide
有一個能夠鏈接該數據庫的用戶名和密碼fetch
有操做權限的databasespa
具體使用以下圖所示指針
第一步
.
第二步
第三步
第四步
import pymysql
# 鏈接數據庫
conn=pymysql.connect(host="localhost",user="root",password="123",database="db6",charset="utf8")
# 建立遊標
cursor=conn.cursor()
# 寫sql語句
# sql="""
# create table userinfo2(
# id INT auto_increment PRIMARY KEY , #建立表
# NAME VARCHAR(32),
# password VARCHAR (32)
# )
# """
# 執行sql語句
# cursor.execute(sql)
# cursor.executemany("insert into userinfo2 values(%s,%s,%s)",[(1,"desheng","1234"), #往表中插入數據
# (2,"qinxiao","1234"),
# (3,"xiaomei","1234")
#
# ])
ret=cursor.execute("select * from userinfo2 where name ='dengsheng' and password='1234';") #當一切準備就緒之後咱們就能夠開始查詢了
# 提交
print(ret)
conn.commit()
# 關閉指針對象
cursor.close()
# 關閉鏈接對象
conn.close()
增操做
import pymysql # 鏈接 conn=pymysql.connect(host="localhost",user="root",password="123",database="db6",charset="utf8") cursor=conn.cursor() # 寫sql語句 id=20 name="anan" password="123" sql="insert into userinfo (id,name,password) value(%s,%s,%s);" try: cursor.execute(sql,(id,name,password)) conn.commit() except Exception as e: conn.rollback() print(e) cursor.close() conn.close()
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="insert into userinfo(id,name,password) VALUES(%s,%s,%s)" id1=15 user1="tingting" pwd1="12333" id2=16 user2="junjun" pwd2="123444" data=((id1,user1,pwd2),(id2,user2,pwd2)) try: cursor.executemany(sql,data) conn.commit() except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="insert into userinfo(name,password) VALUES(%s,%s)" id1=23 user1="tingting" pwd1="12333" try: cursor.execute(sql,(user1,pwd1)) # 拿到插入的這條的ID print(cursor.lastrowid) last_id=cursor.lastrowid conn.commit() print("ID爲:", last_id) except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
刪操做
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="delete from userinfo where name=%s" name="lanbo" try: cursor.execute(sql,(name)) conn.commit() except Exception as e: conn.rollback() cursor.close() conn.close()
改操做
import pymysql # 獲取用戶輸入 # name=input("用戶名>>>:") # pwd=input("密碼>>>:") # 鏈接數據庫 conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") # 獲取遊標 cursor=conn.cursor() # 執行sql語句 sql= "update userinfo set password=%s where name=%s;" name="deshen" print(sql) pwd="1233333" # 讓pymysql拼接字符串 try: cursor.execute(sql,(pwd,name)) conn.commit() except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
查操做(包括單條多條進階用法)
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() # 寫sql語句 sql="select * from userinfo" # 執行sql語句 ret=cursor.execute(sql) print(ret) # 打印此時又多少條數據 # 一次取一條 print(cursor.fetchone()) print(cursor.fetchone()) #一次取全部 # print(cursor.fetchall()) # 一次取3條 print(cursor.fetchmany(3)) # 進階用法 # 移動取數據的光標 cursor.scroll(-2) #默認是相對移動的 print(cursor.fetchone()) # 按照絕對位置去移動 cursor.scroll(4,mode="absolute") print(cursor.fetchone()) cursor.close() conn.close()