Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/class/db.py

277 lines
8.2 KiB

7 years ago
# coding: utf-8
7 years ago
import sqlite3
import os
7 years ago
7 years ago
class Sql():
#------------------------------
# 数据库操作类 For sqlite3
#------------------------------
7 years ago
__DB_FILE = None # 数据库文件
__DB_CONN = None # 数据库连接对象
__DB_TABLE = "" # 被操作的表名称
__OPT_WHERE = "" # where条件
__OPT_LIMIT = "" # limit条件
__OPT_ORDER = "" # order条件
__OPT_FIELD = "*" # field条件
__OPT_PARAM = () # where值
7 years ago
def __init__(self):
self.__DB_FILE = 'data/default.db'
7 years ago
def __GetConn(self):
# 取数据库对象
7 years ago
try:
if self.__DB_CONN == None:
self.__DB_CONN = sqlite3.connect(self.__DB_FILE)
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def dbfile(self, name):
7 years ago
self.__DB_FILE = 'data/' + name + '.db'
return self
7 years ago
def table(self, table):
# 设置表名
7 years ago
self.__DB_TABLE = table
return self
7 years ago
def where(self, where, param):
# WHERE条件
7 years ago
if where:
self.__OPT_WHERE = " WHERE " + where
self.__OPT_PARAM = param
return self
7 years ago
def order(self, order):
# ORDER条件
7 years ago
if len(order):
7 years ago
self.__OPT_ORDER = " ORDER BY " + order
7 years ago
return self
7 years ago
def limit(self, limit):
# LIMIT条件
7 years ago
if len(limit):
7 years ago
self.__OPT_LIMIT = " LIMIT " + limit
7 years ago
return self
7 years ago
def field(self, field):
# FIELD条件
7 years ago
if len(field):
self.__OPT_FIELD = field
return self
7 years ago
7 years ago
def select(self):
7 years ago
# 查询数据集
7 years ago
self.__GetConn()
try:
7 years ago
sql = "SELECT " + self.__OPT_FIELD + " FROM " + self.__DB_TABLE + \
self.__OPT_WHERE + self.__OPT_ORDER + self.__OPT_LIMIT
result = self.__DB_CONN.execute(sql, self.__OPT_PARAM)
7 years ago
data = result.fetchall()
7 years ago
# 构造字曲系列
7 years ago
if self.__OPT_FIELD != "*":
field = self.__OPT_FIELD.split(',')
tmp = []
for row in data:
7 years ago
i = 0
7 years ago
tmp1 = {}
for key in field:
tmp1[key] = row[i]
i += 1
tmp.append(tmp1)
del(tmp1)
data = tmp
del(tmp)
else:
7 years ago
# 将元组转换成列表
tmp = map(list, data)
7 years ago
data = tmp
del(tmp)
self.__close()
return data
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def getField(self, keyName):
# 取回指定字段
result = self.field(keyName).select()
7 years ago
if len(result) == 1:
return result[0][keyName]
return result
7 years ago
def setField(self, keyName, keyValue):
# 更新指定字段
return self.save(keyName, (keyValue,))
7 years ago
def find(self):
7 years ago
# 取一行数据
7 years ago
result = self.limit("1").select()
if len(result) == 1:
return result[0]
return result
7 years ago
7 years ago
def count(self):
7 years ago
# 取行数
key = "COUNT(*)"
7 years ago
data = self.field(key).select()
try:
return int(data[0][key])
except:
return 0
7 years ago
def add(self, keys, param):
# 插入数据
7 years ago
self.__GetConn()
self.__DB_CONN.text_factory = str
try:
7 years ago
values = ""
7 years ago
for key in keys.split(','):
values += "?,"
7 years ago
values = self.checkInput(values[0:len(values) - 1])
sql = "INSERT INTO " + self.__DB_TABLE + \
"(" + keys + ") " + "VALUES(" + values + ")"
result = self.__DB_CONN.execute(sql, param)
7 years ago
id = result.lastrowid
self.__close()
self.__DB_CONN.commit()
return id
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def checkInput(self, data):
if not data:
return data
if type(data) != str:
return data
checkList = [
{'d': '<', 'r': ''},
{'d': '>', 'r': ''},
{'d': '\'', 'r': ''},
{'d': '"', 'r': ''},
{'d': '&', 'r': ''},
{'d': '#', 'r': ''},
{'d': '<', 'r': ''}
]
for v in checkList:
data = data.replace(v['d'], v['r'])
return data
def addAll(self, keys, param):
# 插入数据
7 years ago
self.__GetConn()
self.__DB_CONN.text_factory = str
try:
7 years ago
values = ""
7 years ago
for key in keys.split(','):
values += "?,"
7 years ago
values = values[0:len(values) - 1]
sql = "INSERT INTO " + self.__DB_TABLE + \
"(" + keys + ") " + "VALUES(" + values + ")"
result = self.__DB_CONN.execute(sql, param)
7 years ago
return True
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
7 years ago
def commit(self):
self.__close()
self.__DB_CONN.commit()
7 years ago
def save(self, keys, param):
# 更新数据
7 years ago
self.__GetConn()
self.__DB_CONN.text_factory = str
try:
opt = ""
for key in keys.split(','):
opt += key + "=?,"
7 years ago
opt = opt[0:len(opt) - 1]
sql = "UPDATE " + self.__DB_TABLE + " SET " + opt + self.__OPT_WHERE
7 years ago
import public
7 years ago
public.writeFile('/tmp/test.pl', sql)
# 处理拼接WHERE与UPDATE参数
7 years ago
tmp = list(param)
for arg in self.__OPT_PARAM:
tmp.append(arg)
self.__OPT_PARAM = tuple(tmp)
7 years ago
result = self.__DB_CONN.execute(sql, self.__OPT_PARAM)
7 years ago
self.__close()
self.__DB_CONN.commit()
return result.rowcount
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def delete(self, id=None):
# 删除数据
7 years ago
self.__GetConn()
try:
if id:
self.__OPT_WHERE = " WHERE id=?"
self.__OPT_PARAM = (id,)
sql = "DELETE FROM " + self.__DB_TABLE + self.__OPT_WHERE
7 years ago
result = self.__DB_CONN.execute(sql, self.__OPT_PARAM)
7 years ago
self.__close()
self.__DB_CONN.commit()
return result.rowcount
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def execute(self, sql, param):
# 执行SQL语句返回受影响行
7 years ago
self.__GetConn()
try:
7 years ago
result = self.__DB_CONN.execute(sql, param)
7 years ago
self.__DB_CONN.commit()
return result.rowcount
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def query(self, sql, param):
# 执行SQL语句返回数据集
7 years ago
self.__GetConn()
try:
7 years ago
result = self.__DB_CONN.execute(sql, param)
# 将元组转换成列表
data = map(list, result)
7 years ago
return data
7 years ago
except Exception, ex:
7 years ago
return "error: " + str(ex)
7 years ago
def create(self, name):
# 创建数据表
7 years ago
self.__GetConn()
import public
script = public.readFile('data/' + name + '.sql')
result = self.__DB_CONN.executescript(script)
self.__DB_CONN.commit()
return result.rowcount
7 years ago
def fofile(self, filename):
# 执行脚本
7 years ago
self.__GetConn()
import public
script = public.readFile(filename)
result = self.__DB_CONN.executescript(script)
self.__DB_CONN.commit()
return result.rowcount
7 years ago
7 years ago
def __close(self):
7 years ago
# 清理条件属性
7 years ago
self.__OPT_WHERE = ""
self.__OPT_FIELD = "*"
self.__OPT_ORDER = ""
self.__OPT_LIMIT = ""
self.__OPT_PARAM = ()
7 years ago
7 years ago
def close(self):
7 years ago
# 释放资源
7 years ago
try:
self.__DB_CONN.close()
self.__DB_CONN = None
except:
pass