# -*- coding:utf-8 -*- import sqlite3 import os def get_conn(db_path): if os.path.exists(db_path) and os.path.isfile(db_path): os.remove(db_path) return sqlite3.connect(db_path) def get_cursor(conn): if conn is not None: return conn.cursor() def close_all(conn,cu): cu.close() conn.commit() conn.close() def create_table(conn): sql = """CREATE TABLE jxcList ( ‘id‘ integer PRIMARY KEY AUTOINCREMENT, ‘物品名称‘ varchar(40) NOT NULL, ‘物品编号‘ varchar(40) , ‘日期‘ varchar(40) , ‘进货价格‘ int(40) , ‘进货数量‘ int(40) , ‘进货厂商‘ varchar(40) , ‘出货价格‘ int(40) , ‘出货数量‘ int(40) , ‘出货厂商‘ varchar(40) )""" cu = get_cursor(conn) cu.execute(sql) #close_all(conn,cu) def save_data(conn,dataList,INorOutFlag): cu = get_cursor(conn) if len(dataList)>0: if INorOutFlag: sql = """INSERT INTO jxcList (物品名称,物品编号,日期,进货价格,进货数量,进货厂商) VALUES(?,?,?,?,?,?) """ print(type(sql)) else: sql = """INSERT INTO jxcList (物品名称,物品编号,日期,出货价格,出货数量,出货厂商) VALUES(?,?,?,?,?,?) """ for data in dataList: cu.execute(sql,data) #close_all(conn,cu) def search_fun(conn,itemList,InorOutFlag): sql = "SELECT * FROM jxcList WHERE " newList = [] if itemList[0] != None: sql += ‘物品名称=? and ‘ newList.append(itemList[0]) if itemList[1] != None: sql += ‘物品编号=? and ‘ newList.append(itemList[1]) if itemList[2] != None: sql += ‘日期>=? and ‘ newList.append(itemList[2]) if itemList[3] != None: sql += ‘日期<=? and ‘ newList.append(itemList[3]) if InorOutFlag: if itemList[4] != None: sql += ‘进货价格=? and ‘ newList.append(itemList[4]) if itemList[5] != None: sql += ‘进货数量=? and ‘ newList.append(itemList[5]) if itemList[6] != None: sql += ‘进货厂商=? and ‘ newList.append(itemList[6]) else: if itemList[4] != None: sql += ‘出货价格=? and ‘ newList.append(itemList[4]) if itemList[5] != None: sql += ‘出货数量=? and ‘ newList.append(itemList[5]) if itemList[6] != None: sql += ‘出货厂商=? and ‘ newList.append(itemList[6]) searchItemLen = len(newList) sql = sql+‘id>=1 ‘ cu = get_cursor(conn) cu.execute(sql,tuple(newList)) rows = cu.fetchall() show_result(rows) def show_result(rows): if rows is not None: for row in rows: print(row) def show_db(conn): cu = get_cursor(conn) cu.execute(‘select * from jxcList‘) show_result(cu.fetchall()) def main(): db_file_path = r‘/home/jie/进销存/testdb.db‘ conn = get_conn(db_file_path) create_table(conn) dataList = [(‘物品1‘,‘编号1‘,‘20170602‘,‘19.22‘,‘10‘,‘蓝翔技校‘), (‘物品2‘, ‘编号2‘, ‘20170719‘, ‘19.22‘, ‘10‘, ‘新东方‘), (‘物品3‘, ‘编号3‘, ‘20170603‘, ‘18.22‘, ‘12‘, ‘蓝翔技校‘), (‘物品4‘, ‘编号4‘, ‘20170602‘, ‘1.22‘, ‘10‘, ‘新东方‘), (‘物品5‘, ‘编号5‘,‘20180102‘,‘19.22‘,‘10‘,‘新东方‘), ] dataList2 = [(‘物品1‘,‘编号1‘,‘20170602‘,‘19.22‘,‘8‘,‘蓝翔技校‘), (‘物品2‘, ‘编号2‘, ‘20170719‘, ‘19.22‘, ‘8‘, ‘新东方‘), (‘物品3‘, ‘编号3‘, ‘20170603‘, ‘18.22‘, ‘8‘, ‘蓝翔技校‘), (‘物品4‘, ‘编号4‘, ‘20170602‘, ‘1.22‘, ‘10‘, ‘新东方‘), (‘物品5‘, ‘编号5‘,‘20180102‘,‘20.22‘,‘10‘,‘新东方‘), ] save_data(conn,dataList,True) save_data(conn,dataList2, False) show_db(conn) print(‘*‘*50) #itemList = [‘物品1‘,‘编号1‘,‘20170602‘,‘20170603‘,‘19.22‘,‘10‘,‘蓝翔技校‘] itemList = [‘物品1‘,None,None,None,None,None,None] search_fun(conn,itemList,True) if __name__==‘__main__‘: main()