exists

如何安全地创建嵌套目录?

筅森魡賤 提交于 2019-11-30 21:46:34
检查文件目录是否存在的最优雅方法是什么?如果不存在,则使用Python创建目录? 这是我尝试过的: import os file_path = "/my/directory/filename.txt" directory = os.path.dirname(file_path) try: os.stat(directory) except: os.mkdir(directory) f = file(filename) 不知何故,我错过了 os.path.exists (感谢kanja,Blair和Douglas)。 这就是我现在所拥有的: def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) 是否有“打开”标志,使它自动发生? #1楼 Python 3.5以上版本: import pathlib pathlib.Path('/my/directory').mkdir(parents=True, exist_ok=True) pathlib.Path.mkdir 使用的 pathlib.Path.mkdir 递归创建目录,如果目录已经存在,则不会引发异常。 如果不需要或不希望创建 parents

mysql条件查询

喜夏-厌秋 提交于 2019-11-30 18:18:33
1 )查询学生表中所有数据信息查询出成绩表的所有信息;如能查出数据,则接着查出学生表的所 有信息,通过一个sql语句来实现。 2)、查询出成绩表中学生编号大于1000的所有信息;如不能查询出数据,则接着查询出学生表的所 有信息,观察有没查询结果,通过一个l语句来赛现: 3).从学生表中查询出学生的d,姓名信息前提是假如学生的d在成绩表中stud里有相应的值,通 过一个sql语句来实现。 4).从学生表中查询出学生的过,姓名信息前提是假如学生的d在成绩都及格的成绩表中stu_id里有 相应的值,通过一个sql语句来实现。 5).从学生表中查询出学生成绩等于100的学生的所有信息。 6).从学生表中查询出编号不等于学生成绩等于80的学生的编号的所有信息。 7从学生表中查询出编号属于学生成绩不等于80的学生的编号的所有信息。 SELECT * from stu WHERE EXISTS(SELECT * from stu_score); SELECT * from stu WHERE EXISTS (SELECT * from stu_score where stu_id>1000); SELECT id,name from stu WHERE EXISTS (SELECT id FROM stu_score); SELECT id,name FROM stu WHERE EXISTS

SQL “if exists…” dynamic query

不羁岁月 提交于 2019-11-30 17:59:25
Suppose I have a query stored in a variable like this (it's actually dynamically populated and more complex, but this is for demonstration purposes): DECLARE @Query VARCHAR(1000) = 'SELECT * FROM dbo.MyTable' Is there a way to check if the query would return any results? Something like this, but this doesn't work: IF EXISTS (@Query) BEGIN -- do something END The only way that I can think of to do this is to put the results in a temp table and then query from that, but that is not ideal because the columns in the dynamic query can vary and I really don't need the temp table at all for any

Shorthand function for checking whether a property exists [duplicate]

血红的双手。 提交于 2019-11-30 17:35:18
问题 This question already has answers here : Test for existence of nested JavaScript object key (55 answers) Closed 4 years ago . Can you guys help me make a shorthand function determining whether an object property exists? In 99% of the cases I want to use it to check whether the returned json object contains the specified property or not. Note that there is no guarantee that any of the parent properties or even the json object itself must be defined. I was thinking of something in this manner:

Hibernate中No row with the given identifier exists

痴心易碎 提交于 2019-11-30 16:08:33
产生此问题的原因: 有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique="true">(特殊的多对一映射,实际就是一对一)来关联table2.当hibernate查找的时候,table2里的数据没有与table1相匹配的,这样就会报 No row with the given identifier exists这个错.(一句话,就是数据的问题!) 假如说,table1里有自身的主键id1,还有table2的主键id2,这两个字段. 如果hibenrate设置的单项关联,即使table1中的id2为null值,table2中id2中有值,查询都不会出错.但是如果table1中的id2字段有值,但是这个值在table2中主键值里并没有,就会报上面的错! 如果hibernate是双向关联,那么table1中的id2为null值,但是table2中如果有值,就会报这个错.这种情况目前的解决办法就是改成单项关联,或者把不对应的数据改对! 这就是报这个错的原因了,知道原因了就相应的改就行了.或许还有些人迷惑hibernate关联都配好了,怎么会出现这样的错?其实这是编程的时候出现的问题,假如说我在添加信息的时候

SqlAlchemy Core and bare exists query

半腔热情 提交于 2019-11-30 15:47:18
问题 I have faced a problem where I have to find if the data exists in the table using SqlAlchemy Core . I think the best way to do this query is to use exists method, which stops searching as soon as the first item is found. So, I crafted this version of query: conn = self.db.connect() query = exists().where(cookie_table.c.cookie_id == cookie_id) result = conn.execute(query) But it produces this error: StatementError: Not an executable clause (original cause: ArgumentError: Not an executable

项目中常用的MySQL 优化

自古美人都是妖i 提交于 2019-11-30 14:29:50
一、EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划。 下面来个简单的示例,标注(1、2、3、4、5)我们要重点关注的数据: type列,连接类型。一个好的SQL语句至少要达到range级别。杜绝出现all级别。 key列,使用到的索引名。如果没有选择索引,值是NULL。可以采取强制索引方式 key_len列,索引长度。 rows列,扫描行数。该值是个预估值。 extra列,详细说明。注意,常见的不太友好的值,如下:Using filesort,Using temporary。 二、SQL 语句中 IN 包含的值不应过多 MySQL对于IN做了相应的优化,即将IN中的常量全部存储在一个数组里面,而且这个数组是排好序的。但是如果数值较多,产生的消耗也是比较大的。再例如:select id from t where num in(1,2,3) 对于连续的数值,能用between就不要用in了;再或者使用连接来替换。 三、SELECT语句务必指明字段名称 SELECT*增加很多不必要的消耗(CPU、IO、内存、网络带宽);增加了使用覆盖索引的可能性;当表结构发生改变时,前断也需要更新。所以要求直接在select后面接上字段名。 四、当只需要一条数据的时候,使用limit 1 这是为了使EXPLAIN中type列达到const类型 五、如果排序字段没有用到索引

mysq exists 和 in 的性能 差别

只愿长相守 提交于 2019-11-30 13:00:28
如果查询的两个表大小相当,那么用in和exists差别不大 。 如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in: 例如:表A(小表),表B(大表) 1: select * from A where cc in (select cc from B) 效率低 ,用到了A表上cc列的索引; select * from A where exists(select cc from B where cc=A.cc) 效率高 ,用到了B表上cc列的索引。 相反的 2: select * from B where cc in (select cc from A) 效率高 ,用到了B表上cc列的索引; select * from B where exists(select cc from A where cc=B.cc) 效率低 ,用到了A表上cc列的索引。 not in 和not exists如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。 所以无论那个表大,用not exists都比not in要快 。 in 与 =的区别 select name from student where name in ('zhang','wang','li','zhao'); 与 select

Mysql 基础学习(八)-- 数据定义语言,库和表的管里

六眼飞鱼酱① 提交于 2019-11-30 12:11:41
数据定义语言 库的管理 创建的数据库会在 MYSQL的安装目录下有个 data 文件夹 创建 语法: create database [ IF NOT EXISTS ] 库名 ; 案例1: 创建books 数据库 CREATE DATABASE IF NOT EXISTS BOOKS ; 判断是否存在该数据库,如果存在不创建。如果不存在,那么久创建 修改 RENAME DATABASE books TO 新库名 // 已经报废 修改库的字符集 ALTER DATABASE book CHARACTER set gbk ; 删除 DROP DATABASE IF EXISTS books1 表的管理 **创建: create ** CREATE TABLE 表名 ( 列名 列的类型 [ ( 长度 ) 约束 ] , 列名 列的类型 [ ( 长度 ) 约束 ] , 列名 列的类型 [ ( 长度 ) 约束 ] , 列名 列的类型 [ ( 长度 ) 约束 ] . . . ) 案例1: 创建表 book CREATE TABLE book ( id INT , bname VARCHAR ( 20 ) , price DOUBLE , authorId VARCHAR ( 20 ) , publishDate DATETIME , ) 案例2: 创建表 author CREATE TABLE

Mysql How to only select from a column if the column exists

为君一笑 提交于 2019-11-30 09:51:50
I need to be able to check if a column exists and if it does then I want to SELECT from it. I am trying lots of different variations but I'm not even sure if this is possible. Here's my latest attempt: SELECT IF (EXISTS (SELECT `Period` AS `Period` FROM myview), `PERIOD`, IF (EXISTS (SELECT `Country` AS `COUNTRY` FROM myview),`COUNTRY` FROM myview ; Any ideas? EDIT I had seen the other question on here: MySQL, Check if a column exists in a table with SQL But I still struggle with the if statement. I can check to see if the column exists using the answer in the question above. But my question