導(dǎo)入數(shù)據(jù)庫import sqlit3
連接數(shù)據(jù)庫conn = sqlite3.connent("test.db")
創(chuàng)建游標(biāo)cursor = connn.cursor()
創(chuàng)建表sql = "create table login(id varchar(20) primary key, name varchar(30), password varchar(30))"
cursor.execute(sql) 常用增刪改查# 查看表結(jié)構(gòu)cur.execute("PRAGMA table_info(jobs)")
# 表中增加字段cur.execute("alter table WeakManager add release_time text")
# 重命名表alter table tema rename to team2;
# 刪除表中字段password(不支持刪除列)alter table wx_userInfo drop column password;
# 刪除表cur.execute("drop table WeakManager")
# 刪除數(shù)據(jù)delete from wx_userInfo where id=3;
# 添加數(shù)據(jù)insert into table_name(v1, v2) values (v1, v2);
cur.execute("insert into people values (?, ?)", (who, age))
cur.execute("select * from people where name=:who and age=:age",{"who": who, "age": age})
-- 修改數(shù)據(jù)update wx_userInfo set name='bbb',nickName='bbb' where openID='xxxxxx';
-- 在原有數(shù)據(jù)上增加數(shù)據(jù)update wx_userInfo set control_order=control_order||'on_off=0' where _id=40;
-- 查詢所以數(shù)據(jù)select * from wx_userInfo;-- 查詢指定數(shù)量條數(shù)select * from wx_userInfo limit 3;
-- 查詢升序輸出記錄select * from wx_userInfo order by id asc;
-- 查詢降序輸出記錄select * from wx_userInfo order by id desc;
-- 條件查詢select * from wx_userInfo where id in('3');select * from wx_userInfo where id between 0 and 2;
-- 查詢指定字段記錄數(shù)量統(tǒng)計(jì)select count(nickName) from wx_userInfo;select count(unionID) from wx_userInfo;
-- 查詢指定字段的數(shù)據(jù)(將指定字段可能重復(fù)的數(shù)據(jù)去掉,列出所有不同的數(shù)據(jù))select distinct unionID from wx_userInfo;select distinct id from wx_userInfo;
-- 查詢最后一條數(shù)據(jù),,order by id desc 倒序,然后查找最后一條
limit 0,1select id, room_id,control_order,date_time from control_table order by id desc limit 0,1; 結(jié)束操作# 1.提交事務(wù)conn.commit()# 2.關(guān)閉游標(biāo)cursor.close()# 3.關(guān)閉連接conn.close()
|