pymysql的使用簡單使用方法

2021-08-28 03:02:26 字數 1593 閱讀 6345

1.安裝方法

pip安裝:pip install pymysql

anaconda安裝:conda install -c anaconda pymysql

2. pymysql執行流程

3.匯入模組

from pymysql import *

4.例項

import pymysql

#連線資料庫

db = pymysql.connect(host = 'localhost', user = 'root', password = 'password', db = 'demo1', port = 3306)

print(db)

cur = db.cursor()

sql = "select * from login where id = {}".format(1)

print(sql)

try :

#查詢cur.execute(sql)

#查詢所有資料

results = cur.fetchall()

#獲取單條資料

# results = cur.fetchone()

for i in results :

id = i[0]

username = i[1]

passwrod = i[2]

print('id:{},username:{},password:{}'.format(id, username, passwrod))

#新增資料

sql_insert = "insert into login(username,password) values ({},{})".format('"xiaowamg33"',666)

print('sql_insrt:{}'.format(sql_insert))

id = cur.execute(sql_insert)

print('id:{}'.format(id))

db.commit()

#修改sql_update = 'update login set username = {} where id = {}'.format('"dagege"',1)

id_update = cur.execute(sql_update)

print('修改的行數:{}'.format(id_update))

db.commit()

#刪除sql_delete = 'delete from login where id={}'.format(2)

id_dedete = cur.execute(sql_delete)

print('刪除的行數:{}'.format(id_dedete))

db.commit()

except exception as e:

print(e)

db.rollback()

finally:

db.close()

pymysql的基本使用

在學爬蟲的過程中,都要和資料庫打交道,我們常用的資料庫是mysql資料庫 或者redis 一對一 mongodb資料庫等 python3為我們提供了簡單的運算元據庫的方法,如下 我們安裝pymysql就基本上就是在命令列模式下,pip3 install pymysql 能翻牆的人用這個安裝吧 不能翻...

pymysql的基本使用

import pymysql 與c s架構中的client一樣使用connect與資料庫建立連線 conn pymysql.connect user root password 123456 host 127.0.0.1 port 3306,charset utf8 database day36 c...

PyMySQL模組的使用

pip3 install pymysqlimport pymysql 建立連線 conn pymysql.connect host 127.0.0.1 port 3306,user root password 123456 db db5 charset utf8 user input user pw...