pymysql

一,IDE工具介紹
生產環境仍是推薦使用mysql命令行,但爲了方便咱們測試,能夠使用IDE工具
下載連接:https://pan.baidu.com/s/1bpo5mqjmysql

掌握:
#1. 測試+連接數據庫
#2. 新建庫
#3. 新建表,新增字段+類型+約束
#4. 設計表:外鍵
#5. 新建查詢
#6. 備份庫/表

#注意:
批量加註釋:ctrl+?鍵
批量去註釋:ctrl+shift+?鍵

二,pymysql模塊sql

先安裝 pip3 install pymysql  才能導入這個模塊

import pymysql

conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    database='db42',
    charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
# rows=cursor.execute('show tables;')
rows=cursor.execute('select * from class;')
print(rows)
#
# print(cursor.fetchone())
# print(cursor.fetchmany(2))
# print(cursor.fetchone())
# print(cursor.fetchall())
#
# print(cursor.fetchall())
# cursor.scroll(3,'absolute')
# print(cursor.fetchone())
#
# print(cursor.fetchone())
# print(cursor.fetchone())
# cursor.scroll(1,'relative')
# print(cursor.fetchone())
#
# cursor.close()
# conn.close()

import pymysql #pip3 install pymysql

conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    database='db42',
    charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)


inp_user=input('用戶名>>:').strip() #inp_user=""
inp_pwd=input('密碼>>:').strip() #inp_pwd=""
sql="select * from user where name='%s' and password='%s'" %(inp_user,inp_pwd)
print(sql)


rows=cursor.execute(sql)
if rows:
    print('登陸成功')
else:
    print('登陸失敗')

cursor.close()
conn.close()



直接改變了sql  繞過了密碼和用戶名

#在服務器端防止sql注入問題:不要本身拼接字符串,讓pymysql模塊去拼接
import pymysql #pip3 install pymysql

conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    database='db42',
    charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)


inp_user=input('用戶名>>:').strip() #inp_user=""
inp_pwd=input('密碼>>:').strip() #inp_pwd=""
sql="select * from user where name=%s and password=%s"
print(sql)


rows=cursor.execute(sql,(inp_user,inp_pwd))
if rows:
    print('登陸成功')
else:
    print('登陸失敗')

cursor.close()
conn.close()

三,、增、刪、改:conn.commit()數據庫

import pymysql #pip3 install pymysql

conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    database='db42',
    charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)

# sql='insert into user(username,password) values(%s,%s)'
# rows=cursor.execute(sql,('EGON','123456'))
# print(rows)
# print(cursor.lastrowid)

# rows=cursor.execute('update user set username="alexSB" where id=2')
# print(rows)

# 一次插入多行記錄
sql='insert into user(username,password) values(%s,%s)'
rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')])
print(rows)
print(cursor.lastrowid)

conn.commit() # 只有commit提交纔會完成真正的修改
cursor.close()
conn.close()

四,查:fetchone,fetchmany,fetchall服務器

fetchone(查詢單條)
 fetchmany(查詢多條)
 fetchall(查詢全部)

import pymysql

conn=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    database='db42',
    charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
# rows=cursor.execute('show tables;')
rows=cursor.execute('select * from class;')
print(rows)
#
# print(cursor.fetchone())
# print(cursor.fetchmany(2))
# print(cursor.fetchone())
# print(cursor.fetchall())
#
# print(cursor.fetchall())
# cursor.scroll(3,'absolute')
# print(cursor.fetchone())
#
# print(cursor.fetchone())
# print(cursor.fetchone())
# cursor.scroll(1,'relative')
# print(cursor.fetchone())
#
# cursor.close()
# conn.close()

五,獲取最後工具

------查看錶中最後一行的iD
import pymysql
conn=pymysql.connect(host='localhost',user='root',password='123456',
             database='day47',charset='utf8')
cursor=conn.cursor()


sql='insert into user1(user,password) values(%s,%s);'
rows=cursor.execute(sql,('alex','123'))
# rows=cursor.executemany(sql,[('yuanhao','123'),('laowu','123'),('kgf','12323')])
conn.commit()
print(cursor.lastrowid)  #查看錶中最後一行的iD

cursor.close()
conn.close()
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息