欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python操作MySQL數(shù)據(jù)庫的簡(jiǎn)單步驟分享

 更新時(shí)間:2021年04月14日 10:22:59   作者:愛吃酸梨  
這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫的簡(jiǎn)單步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

現(xiàn)在Python越來越被大眾所使用,特別是進(jìn)入AI人工智能時(shí)代,對(duì)編程要求更加高效根據(jù)快捷,所以Python也經(jīng)常成為人工智和大數(shù)據(jù)編程的重要語音。既然是編程語言就多多少少會(huì)需求對(duì)數(shù)據(jù)進(jìn)行操作,這一篇我們帶大家使用python對(duì)mysql進(jìn)行的操作。

別的不說,直接上代碼

MySQL 建表

建表的時(shí)候,遇到一些坑,沒有解決,如修改 MySQL 的默認(rèn)引擎,default-storage-engine=InnoDB;執(zhí)行報(bào)錯(cuò) 。。。無奈

use mybatistable;
drop table Test;
-- INNODB  支持事務(wù) 
-- Mysql 默認(rèn)的引擎是 MyISAM ,不支持事務(wù)操作
-- 在創(chuàng)建 mysql 表時(shí),最好指定表使用的引擎 
-- 或者直接修改Mysql 默認(rèn)的數(shù)據(jù)庫引擎為 InnoDB
-- default-storage-engine=InnoDB; 執(zhí)行報(bào)錯(cuò) 。。。無奈


create table Test(
	 id int(10)  not null auto_increment,
	 name varchar(20)  not null,
	 password varchar(30) not null,
	 constraint pk_id primary key(id),
	 constraint uk_name unique(name)
)engine=InnoDB charset=utf8;
-- 查看表的引擎
show create table Test;
-- 更新表的引擎 ,執(zhí)行報(bào)錯(cuò)
-- alter table Test type = InnoDB; 

insert into Test values(default,'小紅',123);
insert into Test values(default,'小李',123);
insert into Test values(default,'小趙',123);
insert into Test values(default,'小軍',123);
insert into Test values(default,'小方',123);

select * from  Test;

python 操作 MySQL

import pymysql

'''
    連接 mysql 數(shù)據(jù)庫的步驟
    fetchall 接受全部的返回結(jié)果行
    PS:只有 innodb 類型的表才可以設(shè)置 autocommit;
'''
def connectMySql():
    host =  '127.0.0.1'
    username = 'root'
    password = 'root'
   # dbName = 'MyBatistable'
    # 獲得數(shù)據(jù)庫連接對(duì)象
    conn = pymysql.connect(host,username,password)
    #關(guān)閉數(shù)據(jù)庫的自動(dòng)提交事務(wù)
    conn.autocommit(False)
    # 選擇要操作的數(shù)據(jù)庫
    conn.select_db('MyBatistable')  #覆蓋之前操作的數(shù)據(jù)庫名
    # 獲得游標(biāo)
    cursor = conn.cursor()
    #定義 SQL 語句
    sql = 'select * from Test'
    sql1 = 'insert into test values(default,"小鍋","120")'
    sql2 = 'update test set name="小庫2" where id = 2'
    sql3 = 'delete from test where id = 2'
    #執(zhí)行 SQL 語句
   # row = cursor._query(sql)
    #執(zhí)行 execute 方法,返回影響的行數(shù)
    row = cursor.execute(sql1)
    print('row type:',type(row))
    print('受影響的行數(shù)為:',row)
    if row > 0:
        conn.commit() # 提交事務(wù)
        print('SUCCESS')
    else:
        conn.rollback() # 回滾事務(wù)
        print('Failure')
    #使用DQL ,返回結(jié)果集,以元組的形式
    nums = cursor.fetchall()
    print('nums Type:',type(nums))
    #處理結(jié)果集
    if nums != () :
        for  num in  nums:
            print('--',num)

if __name__ == '__main__':
    connectMySql()

總結(jié)

Python 操作 MySQL 時(shí),由于MySQL 默認(rèn)使用時(shí) MyISAM 引擎,不支持事務(wù)操作。而在Python操作 Mysql 中關(guān)閉自動(dòng)提交事務(wù),發(fā)現(xiàn)并沒有卵用,然后到網(wǎng)上百度說,Mysql 中 InnoDB 支持事務(wù),然后我查找一哈我自己表的引擎發(fā)現(xiàn)是 MyISAM ,欲哭無淚啊。然后我就重新開始建表,測(cè)試。

到此這篇關(guān)于Python操作MySQL數(shù)據(jù)庫的簡(jiǎn)單步驟的文章就介紹到這了,更多相關(guān)Python操作MySQL數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論