最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

python,一个简单的sqlite 操作类

IT圈 admin 47浏览 0评论

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())

 

发布评论

评论列表 (0)

  1. 暂无评论