Python操作MySQL資料庫

2021-10-08 02:07:28 字數 4875 閱讀 2800

import mysql.connector

con=mysql.connector.connect(

host=

"localhost"

,port=

"3306"

, user=

"root"

,password=

"password"

, database=

"demo"

)

import mysql.connector

config=

con=mysql.connector.connect(

**config)

import mysql.connector

con=mysql.connector.connect(

host=

"localhost"

,port=

"3306"

, user=

"root"

,password=

"password"

, database=

"demo"

)cursor=con.cursor(

)sql=

"select empno,job,sal from t_bonus;"

cursor.execute(sql)

print

(type

(cursor)

)for i in cursor:

print

(i)con.close(

)result:

<

class

'mysql.connector.cursor_cext.cmysqlcursor'

>

(7369

,'clerk'

, decimal(

'8000.00'))

(7499

,'salesman'

, decimal(

'1600.00'))

(7521

,'salesman'

, decimal(

'1250.00'))

(7566

,'manager'

, decimal(

'2975.00'))

(7654

,'salesman'

, decimal(

'1250.00'))

(7698

,'manager'

, decimal(

'2850.00'))

(7782

,'manager'

, decimal(

'2450.00'))

(7788

,'analyst'

, decimal(

'3000.00'))

(7839

,'president'

, decimal(

'5000.00'))

(7844

,'salesman'

, decimal(

'1500.00'))

(7900

,'clerk'

, decimal(

'950.00'))

(7902

,'analyst'

, decimal(

'3000.00'))

(7934

,'clerk'

, decimal(

'1300.00'

))

username=1 or 1=1 password=1 or 1=1

在使用字串直接拼接時or之前不管對錯,與or結合都為true

解決方法——預編譯(也可以提高速度)

username=

"1 or 1=1"

password=

"1 or 1=1"

sql=

"select count(*) from t_user where username=%s and aes_decrypt(unhex(password),'helloworld')=%s"

cursor.execute(sql,

(username,password)

)print

(cursor.fetchone()[

0])

sql連線和使用要異常處理異常

import mysql.connector

try:

con=mysql.connector.connect(

host=

"localhost"

,port=

"3306"

, user=

"root"

,password=

"password"

, database=

"demo"

) con.start_transaction(

) cursor=con.cursor(

) sql=

"insert into t_dept(deptno,dname,loc) values(%s,%s,%s);"

cursor.execute(sql,(60

,"sales"

,"hubai"))

con.commit(

)except exception as e:

if"con"

indir()

: con.rollback(

)print

(e)finally:if

"con"

indir()

: con.close(

)

import mysql.connector,mysql.connector.pooling

config=

try:

pool=mysql.connector.pooling.mysqlconnectionpool(

**config,pool_size=5)

con=pool.get_connection(

) con.start_transaction(

) cursor = con.cursor(

) sql =

"delete from t_dept where deptno=%s"

cursor.execute(sql,(70

,)) con.commit(

)except exception as e:

if"con"

indir()

: con.rollback(

)print

(e)# do not need to close con

executemany()反覆執行一條sql語句

import mysql.connector,mysql.connector.pooling

config=

try:

pool=mysql.connector.pooling.mysqlconnectionpool(

**config,pool_size=5)

con=pool.get_connection(

) con.start_transaction(

) cursor = con.cursor(

) sql =

"insert into t_dept(deptno,dname,loc) values(%s,%s,%s);"

date=[[

70,"sales"

,"beijing"],

[80,"actor"

,"shanghai"]]

cursor.executemany(sql, date)

con.commit(

)except exception as e:

if"con"

indir()

: con.rollback(

)print

(e)# do not need to close con

資料庫的連線是昂貴的,乙個連線要經過tcp三次握手,四次揮手,而且一台計算機的最大執行緒數也是有限的

資料庫連線池技術就是先建立好連線,再直接拿出來使用

import mysql.connector,mysql.connector.pooling

config=

try:

pool=mysql.connector.pooling.mysqlconnectionpool(

**config,pool_size=5)

con=pool.get_connection(

) con.start_transaction(

) cursor = con.cursor(

) sql =

"insert into t_dept(deptno,dname,loc) values(%s,%s,%s);"

cursor.execute(sql,(70

,"sales"

,"hubai"))

con.commit(

)except exception as e:

if"con"

indir()

: con.rollback(

)print

(e)# do not need to close con

python操作mysql查詢資料

首先需要連線資料庫,然後才查詢出資料。例如下表名字為 sinauser iduse id use name11 db12 2db233 db3class database def init self self.conn mysqldb.connect 連線資料庫 host 連線你要取出資料庫的ip,...

python操作MySQL資料庫

堅持每天學一點,每天積累一點點,作為自己每天的業餘收穫,這個文章是我在吃飯的期間寫的,利用自己零散的時間學了一下python操作mysql,所以整理一下。我採用的是mysqldb操作的mysql資料庫。先來乙個簡單的例子吧 import mysqldb try conn mysqldb.connec...

Python操作Mysql資料庫

coding utf8 author yangjing import mysqldb 查詢。def select host user root password port 3306,db sql connect mysqldb.connect host host,user user,passwd p...