python在mysql中插入null空值
sql = 「insert into mrodata (mmeues1apid) values (%s)」%『null'
%s沒有引號,可以將「"中null寫進資料庫,達到null值效果。
%s加引號 values就是字串,導致型別錯誤,插入錯誤。
sql = 「insert into mrodata (mmeues1apid) values (『%s')」%『null'
補充:資料庫中的空值與null的區別以及python中的nan和none
資料庫裡面的」空值」有兩種:空字元(「」)、空值(null)。
兩種儲存方式在資料庫中都很常見,實際中根據業務或者個人習慣可以用這兩種方式來儲存「空值」。
-- 建立表test
create table `test` (
`id` int not null ,
`name` varchar(255) null ,
`date` timestamp null ,
`class` varchar(255) null
);insert into test (id,name,date,class) values (1,'張三','2017-03-01','a班');
insert into test (id,name,date,class) values (2,'李四','2017-03-02','');
insert into test (id,name,class) values (3,'王五','c班');
select * from test;
select count(date),count(class) from test;
看到這裡應該明白了,直**空字元和null的mskxb區別在於,在做count計算的時候,空字元也會被計算在裡面,而null不會。有些同學在使用where is null 和is not null 的時候也要注意資料庫中的「mskxb空值」是空字元還是null。
不然統計結果可能並不是你想要的。
平時有些資料是需要借助python 來處理的,我們來看看python獲取資料的時候有哪些需要注意的。
1. 一種是把資料從mysql 中匯出到txt或者csv,然後本地讀取;
2. 另一種是python直接鏈結資料庫,讀取資料;
先看第一種:匯出到csv,python 讀取
第二種:
1、第一種把資料從mysql匯出後,python讀取時,空值即為null;
2、第二種鏈結資料庫後,python能讀取表結構,資料庫的null對應列表中的non程式設計客棧e以及pandas中的nan(如果字段型別是時間,則為nat)。而資料庫中的空字元,則被識別為空字元。
null(資料庫)=none(python列表)=nan(pandas)
空字元(資料庫)=空字元(python列表)=空字元(pandas)
從csv中獲取資料時:空值(csv)=null(資料庫)=nan(pandas)
轉為csv資料時:資料庫中的null\空字元和pandas中的nan\空字元,都變成csv中的空值
在python處理完資料後,往資料庫寫資料的時候也一樣。注意注意!
本文標題: python 在mysql中插入null空值的操作
本文位址:
MySql在表中插入字段
最近在修改 時用到了一些資料庫的操作,平時很少用,記個筆記 1 在表中插入新的字段 命令 alter table 表名 add 要插入的字段 int default 0 例項 cstring str alter table tableinfo add num int defualt 0 db.ope...
Python 在mysql資料庫中插入空值
python中沒有null,只有none,操作mysql資料庫時,當某個值為空,不能使用下列插入語句 錯誤案例 a none cursor db.cursor sql insert into 表名 values s a cursor.execute sql db.commit 應該使用下列這種方式操...
python插入mysql中的時間格式
cursor.execute insert into users set email s,pwd s,nicheng s,createtime s email,pwd,nicheng,time.strftime y m d h m s time.localtime time.time python中...