国外开源代码网站,百度下载安装免费,域名查询 阿里云,专业网站建设代理文章目录 一、安装依赖二、配置文件三、实现类 一、安装依赖
pip install numpy二、配置文件
utils.config.py ############### 233 SQLITE Configuration ###############
SQLITE_PATH ./mysqlite.db三、实现类
utils.PostGreOp.py
# encoding: utf-8import json
import … 文章目录 一、安装依赖二、配置文件三、实现类 一、安装依赖
pip install numpy二、配置文件
utils.config.py ############### 233 SQLITE Configuration ###############
SQLITE_PATH ./mysqlite.db 三、实现类
utils.PostGreOp.py
# encoding: utf-8import json
import sqlite3
import numpy as np
from utils import config as cfclass SqliteOp(object):def __init__(self, db_pathcf.SQLITE_PATH):print(fdb_path: {db_path})self.db_path db_path# conn sqlite3.connect(./test.db)# 增删改def operate(self, sql):db sqlite3.connect(self.db_path)cur db.cursor()try:# 执行sql语句cur.execute(sql)op_id cur.lastrowidcur.close()# 提交到数据库执行db.commit()except Exception as e:print(e)op_id Nonecur.close()# Rollback in case there is any errordb.rollback()# 关闭数据库连接db.close()return op_id# 查def select(self, sql):db sqlite3.connect(self.db_path)cur db.cursor()results Nonetry:# 执行sql语句cur.execute(sql)# 获取所有记录列表results cur.fetchall()# print(results)except Exception as e:print(e)# 关闭数据库连接db.close()return resultsclassmethoddef escape_str(cls, text):string类型数据导入时有可能出现单双引号等需要转义的字段也可能出现nan这样的字段需要先处理一下:return:if cls.isNaNo(text):return nullelse:return json.dumps(str(text), ensure_asciiFalse)[1:-1] classmethoddef escape_num(cls, text):转一下整数报错的话说明传入的不是数字有sql注入风险:return:if str(text) 0:return 0elif cls.isNaNo(text):return nullelse:# return json.dumps(str(text), ensure_asciiFalse)try:int(text)return str(text)except Exception as e:raise Exception(传入不是数字有sql注入风险)# if cls.isNaNo(text):# return null# else:# return json.dumps(str(text), ensure_asciiFalse)classmethoddef isNaNo(cls, sth):NaN、None或者空字符串返回True其他情况返回Falseif not sth:return Trueif isinstance(sth, float):if np.isnan(sth):return Truereturn Falseif __name__ __main__:# 指定目录下没有数据库的话会自动创建sqlt SqliteOp(db_path./mysqlite.db)# 查询当前数据库下的所有表res sqlt.select(fSELECT name FROM sqlite_master WHERE typetable;)print(res)# []