python,一个简单的sqlite 操作类
1,代码
import sqlite3class DB(object):Db='E:/test.db'# 执行sql函数#2018/2/9def Excute(self,sql):conn = sqlite3.connect(self.Db)c = conn.cursor()n=c.execute(sql)connmit()conn.close()return ndef GetData(self,sql):conn = sqlite3.connect(self.Db)c = conn.cursor()cursor = c.execute(sql)list=[]for row in cursor:list.append(row)conn.close()return list
2,测试插入
import SqliteHelp
db=SqliteHelp.DB();
def create_Food():sql="""create table Food(Id int PRIMARY KEY,Name VARCHAR (50),Type VARCHAR (50),Content MAX )"""db.Excute(sql)
def add_Food():sql="""insert into Food(Id,Name,Type,Content)values("2","大白菜水饺","手工面食","")"""db.Excute(sql)create_Food();
add_Food()
3,测试查询
from SqliteHelp import DBclass Food(object):db=DB()def GetAll(self):sql='SELECT * from Food'data=self.db.GetData(sql)return datafoodOper=Food()
print(foodOper.GetAll())
python,一个简单的sqlite 操作类
1,代码
import sqlite3class DB(object):Db='E:/test.db'# 执行sql函数#2018/2/9def Excute(self,sql):conn = sqlite3.connect(self.Db)c = conn.cursor()n=c.execute(sql)connmit()conn.close()return ndef GetData(self,sql):conn = sqlite3.connect(self.Db)c = conn.cursor()cursor = c.execute(sql)list=[]for row in cursor:list.append(row)conn.close()return list
2,测试插入
import SqliteHelp
db=SqliteHelp.DB();
def create_Food():sql="""create table Food(Id int PRIMARY KEY,Name VARCHAR (50),Type VARCHAR (50),Content MAX )"""db.Excute(sql)
def add_Food():sql="""insert into Food(Id,Name,Type,Content)values("2","大白菜水饺","手工面食","")"""db.Excute(sql)create_Food();
add_Food()
3,测试查询
from SqliteHelp import DBclass Food(object):db=DB()def GetAll(self):sql='SELECT * from Food'data=self.db.GetData(sql)return datafoodOper=Food()
print(foodOper.GetAll())